Created
August 29, 2018 12:22
-
-
Save shinaisan/68a654cb69fd42add6850b95d3b0ce44 to your computer and use it in GitHub Desktop.
Winston simple format example
This file contains hidden or 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 { createLogger, format, transports } = require('winston'); | |
let test = {}; | |
test.simple = () => { | |
const logger = createLogger({ | |
format: format.simple(), | |
transports: [new transports.Console()] | |
}); | |
logger.info('Hello world!'); | |
}; | |
test.splat = (arg) => { | |
const logger = createLogger({ | |
format: format.combine( | |
format.splat(), | |
format.simple() | |
), | |
transports: [new transports.Console()] | |
}); | |
logger.info('int argument: %d, memoryUsage: %o', parseInt(arg), process.memoryUsage()); | |
}; | |
test.pretty = (arg) => { | |
const logger = createLogger({ | |
format: format.combine( | |
format.splat(), | |
format.prettyPrint() | |
), | |
transports: [new transports.Console()] | |
}); | |
logger.info('memoryUsage: %o', process.memoryUsage()); | |
}; | |
if (require.main === module) { | |
const name = process.argv[2]; | |
const args = process.argv.slice(3); | |
if (!name) { | |
test.simple(); | |
} else { | |
test[name].apply(null, args); | |
} | |
} |
This file contains hidden or 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
$ node index.js simple | |
info: Hello world! | |
$ node index.js splat 123 | |
info: int argument: 123, memoryUsage: { rss: 24399872, | |
heapTotal: 11378688, | |
heapUsed: 7998560, | |
external: 154088 } | |
$ node index.js pretty 123 | |
{ rss: 24231936, | |
heapTotal: 11378688, | |
heapUsed: 7992544, | |
external: 154088, | |
level: 'info', | |
message: 'memoryUsage: { rss: 24231936,\n heapTotal: 11378688,\n heapUsed: 7992544,\n external: 154088 }', | |
[Symbol(level)]: 'info', | |
[Symbol(splat)]: | |
[ { rss: 24231936, | |
heapTotal: 11378688, | |
heapUsed: 7992544, | |
external: 154088 } ] } |
This file contains hidden or 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
{ | |
"name": "winston-simple-example", | |
"version": "1.0.0", | |
"description": "Winston simple format example", | |
"main": "index.js", | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1" | |
}, | |
"author": "[email protected]", | |
"license": "ISC", | |
"dependencies": { | |
"winston": "^3.0.0" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment