36 lines
1.0 KiB
JavaScript
Executable File
36 lines
1.0 KiB
JavaScript
Executable File
const util = require('util');
|
|
const path = require('path');
|
|
const winston = require('winston');
|
|
|
|
require('winston-daily-rotate-file');
|
|
|
|
// import args from './args';
|
|
|
|
module.exports = function initLogger(filepath) {
|
|
const contextLabel = path.basename(filepath, '.js');
|
|
|
|
return winston.createLogger({
|
|
format: winston.format.combine(
|
|
winston.format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }),
|
|
winston.format((info) => (info instanceof Error
|
|
? { ...info, message: info.stack }
|
|
: { ...info, message: typeof info.message === 'string' ? info.message : util.inspect(info.message) }))(),
|
|
winston.format.colorize(),
|
|
winston.format.printf(({
|
|
level, timestamp, label, message,
|
|
}) => `${timestamp} ${level} [${label || contextLabel}] ${message}`),
|
|
),
|
|
transports: [
|
|
new winston.transports.Console({
|
|
level: 'silly',
|
|
timestamp: true,
|
|
}),
|
|
new winston.transports.DailyRotateFile({
|
|
datePattern: 'YYYY-MM-DD',
|
|
filename: path.join('log', '%DATE%.log'),
|
|
level: 'silly',
|
|
}),
|
|
],
|
|
});
|
|
};
|