Skip to content

Instantly share code, notes, and snippets.

@shinaisan
Created September 1, 2018 13:05
Show Gist options
  • Save shinaisan/58407ac3a73c6f670f3372cc04a8f22f to your computer and use it in GitHub Desktop.
Save shinaisan/58407ac3a73c6f670f3372cc04a8f22f to your computer and use it in GitHub Desktop.
Winston JSON Format Example
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);
}
}
{"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"}
{
"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