Logging

Oecloud provides a logging API based on Bunyan Logger through a module called oe-logger: oe-logger module

Initializing a logger

Defining an oe-logger is done as follows: As oe-logger module is already installed with oecloud, all you should do in your .js file:

var log = require(oe-logger)($NAME);

Wherein: $NAME: the name of the logger you’ve just configured - it’s recommended to use the name of the module the logger’s configured in.

Writing log messages

The log statement looks like: log.Level(ctx, "Some error", ...);

ctx - The context in which the log was created. This allows to log modelName, remoteUser, tenantId and requestId related to the request.

more parameters - second parameter onward can be String or Object as they will be printed as a concatenated string.

Level - the level of log should reflect the influence of the issue being reported over the operational status of the system, as follows:

fatal

A server error occurred. The system can’t continue its normal operation

log.fatal(ctx, “couldn’t load configuration”);

error

The system runs into an abnormal situation but can resume normal operation

log.error(ctx, “Error handling current request”);

warn

The system is working near its operational boundaries, but there is no impact on functionality

log.warn(ctx, "attribute not present. Using default value", DEFAULT_VALUE);

info

A message reporting normal operation

log.info(ctx, "All validation rules executed");

debug

Verbose message containing information to ease debugging

log.debug(ctx, "About to create a request");

Logging Levels###

According with the purpose of the logging session, it’s possible to define the required level of logging.
By default logging is set to error , but it can be changed.

Configuring logging

The logger requires a configuration to be passed as environment variable LOGGER_CONFIG.

In case, no value is passed, then it uses default config, which is:

{"logStreams": [{"type": "pretty"}],"levels": {"default": "info"}, "enableContextLogging": false}

Log Streams

Support for three log streams is provided, which are:

  • Standard Output: Prints log as JSON on the console
  • Pretty Standard Output: Prints log in a pretty format on the console
  • UDP stream: Streams log to a udp stream using gelf

Following are examples for configuring them for different log streams (standard output, pretty output, udp stream) respectively,

LOGGER_CONFIG={"logStreams":[{"type":"out"}],"levels":{"default":"debug"},"enableContextLogging":1}

LOGGER_CONFIG={"logStreams":[{"type":"pretty"}],"levels":{"default":"debug"},"enableContextLogging":1}

LOGGER_CONFIG={"logStreams":[{"type":"udp", "host":"127.0.0.1", "port":"1234"}],"levels":{"default":"debug"},"enableContextLogging":1}

Following are error log statement log.error({}, "sample log"); the output for out and pretty streams are as follows respectively:

{"name":"oe-logger","hostname":"HOSTNAME","pid":9940,"__name__":"test","level":50,"msg":"sample log","time":"2016-01-01T11:28:50.643Z","v":0}

[2016-01-01T11:28:29.126Z] ERROR: oe-logger on HOSTNAME: sample log (test)