Merge pull request #341 from shocknet/bug/logger
Replicated console.log API in Winston
This commit is contained in:
commit
c03d90e079
1 changed files with 16 additions and 2 deletions
|
|
@ -1,10 +1,21 @@
|
||||||
// config/log.js
|
// config/log.js
|
||||||
|
|
||||||
const winston = require("winston");
|
const winston = require("winston");
|
||||||
|
const util = require("util")
|
||||||
require("winston-daily-rotate-file");
|
require("winston-daily-rotate-file");
|
||||||
|
|
||||||
const winstonAttached = new Map();
|
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} logFileName
|
||||||
* @param {string} logLevel
|
* @param {string} logLevel
|
||||||
|
|
@ -24,15 +35,18 @@ module.exports = (logFileName, logLevel) => {
|
||||||
winston.add(new winston.transports.Console({
|
winston.add(new winston.transports.Console({
|
||||||
format: winston.format.combine(
|
format: winston.format.combine(
|
||||||
winston.format.colorize(),
|
winston.format.colorize(),
|
||||||
|
logFormatter(),
|
||||||
|
winston.format.prettyPrint(),
|
||||||
winston.format.timestamp(),
|
winston.format.timestamp(),
|
||||||
|
winston.format.simple(),
|
||||||
winston.format.align(),
|
winston.format.align(),
|
||||||
winston.format.printf((info) => {
|
winston.format.printf((info) => {
|
||||||
const {
|
const {
|
||||||
timestamp, level, message, ...args
|
timestamp, level, message
|
||||||
} = info;
|
} = info;
|
||||||
|
|
||||||
const ts = timestamp.slice(0, 19).replace('T', ' ');
|
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