Complete refactor of Winston logging

This commit is contained in:
emad-salah 2021-07-22 11:36:54 +01:00
parent bbd90b5424
commit cdfb7f7311
2 changed files with 48 additions and 51 deletions

View file

@ -1,58 +1,58 @@
// config/log.js /** @prettier */
const winston = require("winston"); const { createLogger, transports, format } = require('winston')
const util = require("util") const util = require('util')
require("winston-daily-rotate-file"); require('winston-daily-rotate-file')
const winstonAttached = new Map(); // @ts-ignore
const transform = info => {
const transform = (info) => { const args = info[Symbol.for('splat')]
const args = info[Symbol.for('splat')];
if (args) { if (args) {
return {...info, message: util.format(info.message, ...args)}; return { ...info, message: util.format(info.message, ...args) }
} }
return info; return info
} }
const logFormatter = () => ({ transform }) const logFormatter = () => ({ transform })
/** const formatter = format.combine(
* @param {string} logFileName format.colorize(),
* @param {string} logLevel format.errors({ stack: true }),
* @returns {import("winston").Logger} logFormatter(),
*/ format.prettyPrint(),
module.exports = (logFileName, logLevel) => { format.timestamp(),
if (!winstonAttached.has(logFileName)) { format.simple(),
winston.add(new (winston.transports.DailyRotateFile)({ format.align(),
filename: logFileName, format.printf(info => {
datePattern: "yyyy-MM-DD", const { timestamp, level, message, stack, exception } = info
const ts = timestamp.slice(0, 19).replace('T', ' ')
const isObject = typeof message === 'object'
const formattedJson = isObject ? JSON.stringify(message, null, 2) : message
const formattedException = exception ? exception.stack : ''
const errorMessage = stack || formattedException
const formattedMessage = errorMessage ? errorMessage : formattedJson
return `${ts} [${level}]: ${formattedMessage}`
})
)
const Logger = createLogger({
format: formatter,
transports: [
new transports.DailyRotateFile({
filename: 'shockapi.log',
datePattern: 'yyyy-MM-DD',
// https://github.com/winstonjs/winston-daily-rotate-file/issues/188 // https://github.com/winstonjs/winston-daily-rotate-file/issues/188
json: true, json: false,
maxSize: 1000000, maxSize: 1000000,
maxFiles: 7, maxFiles: 7,
level: logLevel handleExceptions: true
}))
winston.add(new winston.transports.Console({
format: winston.format.combine(
winston.format.colorize(),
logFormatter(),
winston.format.prettyPrint(),
winston.format.timestamp(),
winston.format.simple(),
winston.format.align(),
winston.format.printf((info) => {
const {
timestamp, level, message
} = info;
const ts = timestamp.slice(0, 19).replace('T', ' ');
return `${ts} [${level}]: ${typeof message === "object" ? JSON.stringify(message, null, 2) : message}`;
}), }),
) new transports.Console({
})) handleExceptions: true
winston.level = logLevel })
winstonAttached.set(logFileName, winston) ]
} })
return winstonAttached.get(logFileName) module.exports = Logger
}

View file

@ -47,10 +47,7 @@ const server = program => {
const tunnelHost = process.env.LOCAL_TUNNEL_SERVER || defaults.localtunnelHost const tunnelHost = process.env.LOCAL_TUNNEL_SERVER || defaults.localtunnelHost
// setup winston logging ========== // setup winston logging ==========
const logger = require('../config/log')( const logger = require('../config/log')
program.logfile || defaults.logfile,
program.loglevel || defaults.loglevel
)
CommonLogger.setLogger(logger) CommonLogger.setLogger(logger)