Skip to content

Instantly share code, notes, and snippets.

@shinaisan
Created August 30, 2018 00:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shinaisan/5e8062f4646d5b8fad358b9bf0a679df to your computer and use it in GitHub Desktop.
Save shinaisan/5e8062f4646d5b8fad358b9bf0a679df to your computer and use it in GitHub Desktop.
Winston Custom Format Example
const { createLogger, format, transports } = require('winston');
let test = {};
const myFormat = (info) => (
JSON.stringify(info)
);
test.custom = (args) => {
const logger = createLogger({
format: format.combine(
format.splat(),
format.timestamp(),
format.printf(myFormat)
),
transports: [
new transports.File({
filename: './info.log',
level: 'info'
})
]
});
logger.info('arguments: %o', args);
};
if (require.main === module) {
const name = process.argv[2];
const args = process.argv.slice(3);
if (!name) {
test.custom();
} else {
test[name](args);
}
}
{"level":"info","message":"arguments: undefined","timestamp":"2018-08-30T00:26:07.483Z"}
{"0":"a","1":"b","2":"c","level":"info","message":"arguments: [ 'a', 'b', 'c', [length]: 3 ]","timestamp":"2018-08-30T00:26:41.984Z"}
{"0":"hello","1":"world","level":"info","message":"arguments: [ 'hello', 'world', [length]: 2 ]","timestamp":"2018-08-30T00:28:22.218Z"}
{
"name": "winston-custom-format-example",
"version": "1.0.0",
"description": "Winston Custom Format Example",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "shinaisan@users.noreply.github.com",
"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