traxxx/src/logger.js

40 lines
1.1 KiB
JavaScript
Raw Normal View History

'use strict';
const util = require('util');
const path = require('path');
const winston = require('winston');
2020-01-13 22:45:09 +00:00
require('winston-daily-rotate-file');
const args = require('./argv');
function logger(filepath) {
const root = filepath.match(/src[/\\]|dist[/\\]/);
const filename = filepath.slice(root.index + root[0].length)
.replace(path.extname(filepath), '');
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 || filename}] ${message}`),
),
transports: [
new winston.transports.Console({
level: args.level,
timestamp: true,
}),
new winston.transports.DailyRotateFile({
datePattern: 'YYYY-MM-DD',
filename: path.join('log', '%DATE%.log'),
level: 'silly',
}),
],
});
}
module.exports = logger;