-
-
Save tbrannam/b3fc5e1211c6431ba6d489c1687528c0 to your computer and use it in GitHub Desktop.
[Winston Log to file and console just like NodeJS] #NodeJS #Winston #console #log
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const util = require('util') | |
const path = require( 'path' ) | |
const winston = require( 'winston' ); | |
const { createLogger, transports, format } = winston | |
const { Console, File } = transports | |
const { timestamp } = format | |
// Adapted from https://github.com/winstonjs/winston/issues/1427 | |
const commonConfig = { | |
handleExceptions: true, | |
format: format.combine( | |
timestamp(), | |
{ | |
transform (info) { | |
const { timestamp, message } = info; | |
const level = info[Symbol.for('level')]; | |
const args = [message, ...(info[Symbol.for('splat')] || [])]; // join the args back into one arr | |
info.message = args; // for all custom transports (mainly the console ones) | |
let msg = args.map(e => { | |
if (e.toString() === '[object FileList]') | |
return util.inspect(e.toArray(), true, 10); | |
else if (e.toString() === '[object File]') | |
return util.inspect(e.toObject(), true, 10); | |
else if (e.toString() === '[object Object]') { | |
return util.inspect(e, true, 5); | |
} | |
else if (e instanceof Error) | |
return e.stack | |
else | |
return `${e}`; | |
}).join(' '); | |
info[Symbol.for('message')] = `${timestamp}|${level}|${msg}`; // for inbuild transport / file-transport | |
return info; | |
}, | |
}, | |
), | |
} | |
const logger = createLogger( | |
{ | |
level: 'debug', | |
levels: { | |
error: 0, | |
warn: 1, | |
info: 2, | |
debug: 3 | |
}, | |
transports: [ | |
new Console({ | |
...commonConfig, | |
format: commonConfig.format, | |
}), | |
new File( { | |
...commonConfig, | |
format: commonConfig.format, | |
dirname: path.join( __dirname ), | |
filename: 'audit.log', | |
} ) | |
], | |
} | |
) | |
console.error = function () { | |
return logger.error.apply( logger, [...arguments] ) | |
} | |
console.warn = function () { | |
return logger.warn.apply( logger, [...arguments] ) | |
} | |
console.info = function () { | |
return logger.info.apply( logger, [...arguments] ) | |
} | |
console.log = function () { | |
return logger.info.apply( logger, [...arguments] ) | |
} | |
console.debug = function () { | |
return logger.debug.apply( logger, [...arguments] ) | |
} | |
console.log( 'log: some stuff =', JSON.stringify( { msg: 'hello' }, null, 4 ), 'more', { nothing: 'here' }) | |
console.info( 'info: some stuff =', JSON.stringify( { msg: 'hello' }, null, 4 ), 'more', { nothing: 'here' }) | |
console.error( 'error: some stuff =', JSON.stringify( { msg: 'hello' }, null, 4 ), 'more', { nothing: 'here' }) | |
console.warn( 'warn: some stuff =', JSON.stringify( { msg: 'hello' }, null, 4 ), 'more', { nothing: 'here' }) | |
console.debug( 'debug: some stuff =', JSON.stringify( { msg: 'hello' }, null, 4 ), 'more', { nothing: 'here' }) | |
console.debug( 'debug: some stuff =', new Error('boom')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment