Created
September 1, 2018 13:05
-
-
Save shinaisan/58407ac3a73c6f670f3372cc04a8f22f to your computer and use it in GitHub Desktop.
Winston JSON Format Example
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 { createLogger, format, transports } = require('winston'); | |
let test = {}; | |
test.json = (args) => { | |
const logger = createLogger({ | |
format: format.combine( | |
format.timestamp(), | |
format.json() | |
), | |
transports: [ | |
new transports.File({ | |
filename: './info.log', | |
level: 'info' | |
}) | |
] | |
}); | |
logger.info({ | |
args: args, | |
...(process.memoryUsage()) | |
}); | |
}; | |
if (require.main === module) { | |
const name = process.argv[2]; | |
const args = process.argv.slice(3); | |
if (!name) { | |
test.json(args); | |
} else { | |
test[name](args); | |
} | |
} |
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
{"message":{"args":[],"rss":27086848,"heapTotal":11902976,"heapUsed":7414104,"external":42479},"level":"info","timestamp":"2018-08-30T04:55:10.898Z"} | |
{"message":{"args":["hello"],"rss":27086848,"heapTotal":11902976,"heapUsed":7412448,"external":42479},"level":"info","timestamp":"2018-08-30T04:55:13.859Z"} | |
{"message":{"args":["hello","world"],"rss":27070464,"heapTotal":11902976,"heapUsed":7415048,"external":42479},"level":"info","timestamp":"2018-08-30T04:55:16.202Z"} | |
{"message":{"args":["bye"],"rss":27107328,"heapTotal":11902976,"heapUsed":7414056,"external":42479},"level":"info","timestamp":"2018-08-30T04:55:20.380Z"} |
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
{ | |
"name": "winston-json-format-example", | |
"version": "1.0.0", | |
"description": "Winston JSON 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