id0Sch / log4js-json-layout

provides a slim and easy to use json-layout for log4js-node
MIT License
17 stars 8 forks source link

log4js-json-layout

A slim and easy to use JSON layout for log4js-node.

NPM

Build Status

Installation

npm install log4js-json-layout --save

Example Output

Output:

{"startTime":"2017-07-27T00:01:05.175Z","categoryName":"/path/to/file/redis-client.js","level":"DEBUG","data":"Connection to Redis successful!"}

Usage

Set the layout type to json.

Each log object contains the following properties:

Additional properties can be specified as extra arguments of object type during invocation of log function. Static or constant properties can be specified using the static config option.

Options

Example Config

const log4js = require('log4js');
const jsonLayout = require('log4js-json-layout');

log4js.addLayout('json', jsonLayout);

Minimal:

appenders = [{
  type: 'console',
  layout: {
    type: 'json',
  }
}];

More options:

appenders = [{
  type: 'console',
  messageParam: 'msg',
  layout: {
    type: 'json',
    source: 'development',
    static: {
      appName: 'mysuperbapp'
    },
    dynamic: {
      transactionId: function() {
        return 'tx-id'; // string
      },
      isAdmin: function() {
        return 1>0; // boolean
      },
      countLogin: function() {
        return 20; // number
      }
    },
    include: ['startTime', 'categoryName'],
  }
}];

const logger = log4js.getLogger('info');

logger.info('Test log');

Output:

{"startTime":"2017-07-27T00:01:05.175Z","categoryName":"/path/to/file/redis-client.js","level":"DEBUG","transactionId": "tx-id","isAdmin": true,"countLogin": 20,"data":"Test log",}