58 lines
1.6 KiB
JavaScript
58 lines
1.6 KiB
JavaScript
const winston = require('winston');
|
|
|
|
const { createLogger, format, transports } = require('winston');
|
|
const { combine, timestamp, label, printf, numsession } = format;
|
|
|
|
const myFormatFile = printf(({ level, message, label, timestamp, numsession }) => {
|
|
return `${timestamp} ${level} [${label}] SID[${numsession}] ${message}`;
|
|
});
|
|
|
|
const myFormatConsole = printf(({ level, message, label, timestamp, numsession }) => {
|
|
return `${timestamp} ${level} [${label}] SID[${numsession}] ${message}`;
|
|
});
|
|
|
|
|
|
const DailyRotateFile = require('winston-daily-rotate-file');
|
|
|
|
|
|
const logger = winston.createLogger({
|
|
//level: 'info',
|
|
format: combine(
|
|
//label({ label: '' }),
|
|
timestamp({format: 'YYYY-MM-DD HH:mm:ss'}),
|
|
myFormatFile
|
|
),
|
|
defaultMeta: { service: 'user-service' },
|
|
transports: [
|
|
new DailyRotateFile({ filename: 'logs/combined.log' }),
|
|
],
|
|
exceptionHandlers: [
|
|
new DailyRotateFile({ filename: 'logs/combined.log' }),
|
|
new winston.transports.Console()
|
|
],
|
|
rejectionHandlers: [
|
|
new DailyRotateFile({ filename: 'logs/combined.log' }),
|
|
new winston.transports.Console()
|
|
]
|
|
});
|
|
|
|
|
|
|
|
|
|
if (process.env.ENV !== 'production' ) {
|
|
logger.add(new winston.transports.Console(
|
|
{
|
|
format: combine(
|
|
//label({ label: '' }),
|
|
timestamp({format: 'YYYY-MM-DD HH:mm:ss'}),
|
|
myFormatConsole
|
|
),
|
|
handleExceptions: true //pour afficher également les exceptions dans la console
|
|
})
|
|
);
|
|
}
|
|
|
|
/////////////////////////////////////////////////
|
|
// EXPORT DE MODULE POUR QU'IL SOIT LU DANS LES AUTRES FICHIERS JS
|
|
/////////////////////////////////////////////////
|
|
module.exports = logger; |