Replicated console.log API in Winston
This commit is contained in:
parent
58f3e343f4
commit
9dbac7136c
1 changed files with 16 additions and 2 deletions
|
|
@ -1,10 +1,21 @@
|
|||
// config/log.js
|
||||
|
||||
const winston = require("winston");
|
||||
const util = require("util")
|
||||
require("winston-daily-rotate-file");
|
||||
|
||||
const winstonAttached = new Map();
|
||||
|
||||
const transform = (info) => {
|
||||
const args = info[Symbol.for('splat')];
|
||||
if (args) {
|
||||
return {...info, message: util.format(info.message, ...args)};
|
||||
}
|
||||
return info;
|
||||
}
|
||||
|
||||
const logFormatter = () => ({ transform })
|
||||
|
||||
/**
|
||||
* @param {string} logFileName
|
||||
* @param {string} logLevel
|
||||
|
|
@ -24,15 +35,18 @@ module.exports = (logFileName, logLevel) => {
|
|||
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, ...args
|
||||
timestamp, level, message
|
||||
} = info;
|
||||
|
||||
const ts = timestamp.slice(0, 19).replace('T', ' ');
|
||||
return `${ts} [${level}]: ${message} ${Object.keys(args).length ? JSON.stringify(args, null, 2) : ''}`;
|
||||
return `${ts} [${level}]: ${typeof message === "object" ? JSON.stringify(message, null, 2) : message}`;
|
||||
}),
|
||||
)
|
||||
}))
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue