-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.js
36 lines (31 loc) · 1.02 KB
/
logger.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
const winston = require('winston');
const { ElasticsearchTransport } = require('winston-elasticsearch');
const esTransportOpts = {
level: 'info',
index: 'an-index',
clientOpts: {
node: 'http://elasticsearch:9200', // Replace with your Elasticsearch node URL or you can place it to the .env file
},
transformer: logData => {
return {
"@timestamp": (new Date()).getTime(),
severity: logData.level,
message: `[${logData.level}] LOG Message: ${logData.message}`,
fields: {}
}
}
};
const logger = winston.createLogger({
level: 'info',
format: winston.format.json(),
transports: [
new winston.transports.File({ filename: 'logfile.log', level: 'error' }), // Save errors to file
new ElasticsearchTransport(esTransportOpts) // Use ElasticsearchTransport instead of Elasticsearch
]
});
if (process.env.NODE_ENV !== 'production') {
logger.add(new winston.transports.Console({ // Log to console if not in production
format: winston.format.simple()
}));
}
module.exports = logger;