Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
146 lines (115 sloc) 4.14 KB

Software License Maintainability Build Status Coverage Status

node-json-logger

Simply, output logs to STDOUT in JSON

Install

npm i node-json-logger

Usage

const Logger = require('node-json-logger');
const logger = new Logger();

logger.info();
logger.info('message');
logger.info('message','message','message');
logger.info({ data: 'data' });
logger.info({ data1: 'data' }, { data2: 'data' });
logger.info('message', { data1: 'data' }, 'message', { data2: 'data' }, 'message');

Outputs:

{"timestamp":"2001-03-14T01:00:00.000Z","level":"info"}
{"timestamp":"2001-03-14T01:00:00.000Z","level":"info","message":"message"}
{"timestamp":"2001-03-14T01:00:00.000Z","level":"info","message":"message","message1":"message","message2":"message"}
{"timestamp":"2001-03-14T01:00:00.000Z","level":"info","data":"data"}
{"timestamp":"2001-03-14T01:00:00.000Z","level":"info","data1":"data","data2":"data"}
{"timestamp":"2001-03-14T01:00:00.000Z","level":"info","message":"message","data1":"data","message1":"message","data2":"data","message2":"message"}

API

logger.xxx(message?, ...)

Note:

Arguments:

  • message: Specify a message as string or object.

Levels

  • trace
  • debug
  • info
  • warn
  • error
  • fatal

Configuration

level

Specify output level. (optional, default is debug)

const Logger = require('node-json-logger');
const logger = new Logger({ level: 'error'});

logger.trace('message');
logger.debug('message');
logger.info('message');
logger.warn('message');
logger.error('message');
logger.fatal('message');

Output:

{"timestamp":"2001-03-14T01:00:00.000Z","level":"error","message":"message"}
{"timestamp":"2001-03-14T01:00:00.000Z","level":"fatal","message":"message"}

Level and output:

trace debug info warn error fatal
{ level: 'trace' } O O O O O O
{ level: 'debug' } - O O O O O
{ level: 'info' } - - O O O O
{ level: 'warn' } - - - O O O
{ level: 'error' } - - - - O O
{ level: 'fatal' } - - - - - O
{ level: 'none' } - - - - - -

loggerName

Specify the name of the logger. Useful when logging from different files.

const Logger = require('node-json-logger');
const logger = new Logger({ loggerName: 'server/index.js' });

logger.info('message');

Output:

{"timestamp":"2001-03-14T01:00:00.000Z","level":"info","loggerName":"server/index.js","message":"message"}

timestamp

Specify enable or disable timestamp. (optional, default is true)

const Logger = require('node-json-logger');
const logger = new Logger({ timestamp: false });

logger.info('message');

Output with disabled timestamp:

{"level":"info","message":"message"}

timezone

Specify locale timezone. (optional, default is UTC)

const Logger = require('node-json-logger');
const logger = new Logger({timezone: 'America/Sao_Paulo'});

logger.info('message');

Output with specific timezone:

{"timestamp":"2018-10-25T15:32:43.318Z","level":"info","message":"message"}

Same output with default timezone (UTC):

{"timestamp":"2018-10-25T18:32:43.318Z","level":"info","message":"message"}

Click here to see the list with all available timezones.

License

MIT

Copyright ©2018 Hiroaki SHIBUKI a.k.a. hidori

You can’t perform that action at this time.