Skip to content

Instantly share code, notes, and snippets.

@shinaisan
Created August 29, 2018 12:27
Show Gist options
  • Save shinaisan/af0492ed7a7ad35a81a6e40e85d0f9ba to your computer and use it in GitHub Desktop.
Save shinaisan/af0492ed7a7ad35a81a6e40e85d0f9ba to your computer and use it in GitHub Desktop.
Winston Timestamp Example

Winston Timestamp Example

See fecha for details of timestamp formats.

Output

$ node index.js simple a b c
info: arguments: [ 'a', 'b', 'c', [length]: 3 ] {"0":"a","1":"b","2":"c","timestamp":"2018-08-29T12:26:14.844Z"}

$ node index.js pretty a b c
{ '0': 'a',
  '1': 'b',
  '2': 'c',
  level: 'info',
  message: 'arguments: [ \'a\', \'b\', \'c\', [length]: 3 ]',
  timestamp: '2018-08-29T12:26:21.764Z',
  [Symbol(level)]: 'info',
  [Symbol(splat)]: [ [ 'a', 'b', 'c' ] ] }
const { createLogger, format, transports } = require('winston');
let test = {};
test.simple = (args) => {
const logger = createLogger({
format: format.combine(
format.splat(),
format.timestamp(),
format.simple()
),
transports: [new transports.Console()]
});
logger.info('arguments: %o', args);
};
test.pretty = (args) => {
const logger = createLogger({
format: format.combine(
format.splat(),
format.timestamp(),
format.prettyPrint()
),
transports: [new transports.Console()]
});
logger.info('arguments: %o', args);
};
if (require.main === module) {
const name = process.argv[2];
const args = process.argv.slice(3);
if (!name) {
test.simple();
} else {
test[name](args);
}
}
{
"name": "winston-timestamp-example",
"version": "1.0.0",
"description": "Winston Timestamp 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