Logger module

The logger module ensures a common access to logging features for the application. The default implementation is based on winston

Configuration

The log level is configured in the logger:level configuration key:

{
  "logger": {
    "level": "info"
  }
}

Usage

base.logger.info(`Order ${orderNumber} created.`)

Output:

2018-05-21T05:59:59.999Z - info: [ae188fe44.local] [cart:v1] Order 12345 created.

The default format for the output is:

`${isoDate} - ${level}: [${hostname}] [${serviceName}:${serviceVersion} ${data}]`

The microbase own logs have the following format:

`${isoDate} - ${level}: [${hostname}] [${serviceName}:${serviceVersion} [${logger}] ${data}]`

Where logger is a dedicated logger for a microbase module:

2018-05-21T05:59:59.999Z - info: [ae188fe44.local] [cart:v1] [serv] added service [cart:v1:micro.ping]

You can achieve the same creating your own dedicated logger instance:

base.logger.getLogger('MyModule');

or better:

base.getLogger('MyModule');

HTTP requests

The log level is configured in the transports:http:loglevel configuration key. By default it's the same as the logger:level.

{
  "transports": {
    "http": {
      "loglevel": "info"
    }
  }
}

The http requests have a custom format for the data part, and can be changed in the 'transports:http:logpattern' configuration key:

'{{req.method}} {{req.url}} {{res.statusCode}} {{res.responseTime}}ms'

If you are inside an service call you will also see a correlation string:

2018-05-21T05:59:59.999Z - info: [ae188fe44.local] [cart:v1] [http] [BkDbvU2Z3Cz] POST /services/cart/v1/cart.setShippingAddress 200 6ms

That correlation string is unique for each call and is transfered to other services if the current service being executed makes a call.

Debug level check

To check if the log level is debug, use the isDebugEnabled method:

if (base.logger.isDebugEnabled()) {
  base.logger.debug('Initialization...');
}