NodeJS-Winston
3386 ワード
const winston = require('winston');
let initLoggerConfig = function (config) {
/**
* Create logger
*/
let logger = new (winston.Logger)(config.logger),
jobLogger = new(winston.Logger)(config.jobLogger);
config.log = function () {
logger.log(arguments);
};
config.info = function () {
logger.info(arguments);
};
config.error = function () {
logger.error(arguments);
};
config.debug = function () {
logger.debug(arguments);
};
config.jobInfo = function () {
jobLogger.info(arguments);
};
config.jobError = function () {
jobLogger.error(arguments);
};
};
const path = require('path'),
winston = require('winston'),
DailyRotateFile = require('winston-daily-rotate-file');
module.exports = {
env: 'production',
logger: {
transports: [
new winston.transports.Console({
colorize: true
}),
new DailyRotateFile({
level: 'silly',
filename: path.resolve('./logs/access-'),
datePattern: 'yyyy-MM-dd.log',
maxsize: 5242880 /* 5MB */
})
]
},
jobLogger: {
transports: [
new winston.transports.Console({
colorize: true
}),
new DailyRotateFile({
level: 'silly',
filename: path.resolve('./logs/jobs-'),
datePattern: 'yyyy-MM-dd.log',
maxsize: 5242880 /* 5MB */
})
]
},
showErrorStack: process.env.SHOW_ERROR_STACK === 'true',
livereload: false
};
let logger = new (winston.Logger)(config.logger)
let logger = new (winston.Logger)(
{
transports: [
new winston.transports.Console({
colorize: true
}),
new DailyRotateFile({
level: 'silly', // 。0:error 1:warn 2:info 3:verbose 4:debug 5:silly
filename: path.resolve('./logs/access-'), // 。
datePattern: 'yyyy-MM-dd.log',
maxsize: 5242880 /* 5MB */
})
]
}
)
config.info(' 2。');
config.error(' 3。');
config.debug(' 4。');
{"level":"info","message":"{ '0': ' 2。' }","timestamp":"2018-11-28T09:57:18.841Z"}
{"level":"error","message":"{ '0': ' 3。' }","timestamp":"2018-11-28T09:57:18.843Z"}
{"level":"debug","message":"{ '0': ' 4。' }","timestamp":"2018-11-28T09:57:18.843Z"}