2020-01-08 22:33:24 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const util = require('util');
|
2020-02-08 01:49:39 +00:00
|
|
|
const path = require('path');
|
2020-01-08 22:33:24 +00:00
|
|
|
const winston = require('winston');
|
2020-02-08 01:49:39 +00:00
|
|
|
|
2020-01-13 22:45:09 +00:00
|
|
|
require('winston-daily-rotate-file');
|
2020-01-08 22:33:24 +00:00
|
|
|
|
2020-02-08 01:49:39 +00:00
|
|
|
const args = require('./argv');
|
|
|
|
|
|
|
|
function logger(filepath) {
|
2020-05-14 02:26:05 +00:00
|
|
|
const root = filepath.match(/src\/|dist\//);
|
|
|
|
const filename = filepath.slice(root.index + root[0].length)
|
|
|
|
.replace(path.extname(filepath), '');
|
2020-02-08 01:49:39 +00:00
|
|
|
|
2020-05-14 02:26:05 +00:00
|
|
|
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: 'log/%DATE%.log',
|
|
|
|
level: 'silly',
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
});
|
2020-02-07 18:53:16 +00:00
|
|
|
}
|
2020-01-08 22:33:24 +00:00
|
|
|
|
|
|
|
module.exports = logger;
|