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-09-08 13:44:55 +00:00
|
|
|
const root = filepath.match(/src[/\\]|dist[/\\]/);
|
2020-05-14 02:26:05 +00:00
|
|
|
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',
|
2020-09-08 13:44:55 +00:00
|
|
|
filename: path.join('log', '%DATE%.log'),
|
2020-05-14 02:26:05 +00:00
|
|
|
level: 'silly',
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
});
|
2020-02-07 18:53:16 +00:00
|
|
|
}
|
2020-01-08 22:33:24 +00:00
|
|
|
|
|
|
|
module.exports = logger;
|