Logging Contexts

Table of contents
  1. Concept
  2. Global Context
  3. Request context
  4. Custom context

Concept

Logging contexts are an essential concept of this library to extend the metadata attached to each log message. It offers correlation of log messages by enriching them with shared properties, such as the correlation_id of a request, class names or even details on the method being executed. While all log messages contain various information on the runtime environment or various request/response details for request logs, logging contexts are more flexible and allow extension at runtime.

You can create Child Loggers to inherit or create new logging contexts.

There are three kinds of contexts:

  • global context
  • request context
  • custom context

Global Context

As messages written in global context are considered uncorrelated, it is always empty and immutable. Use the imported log object to log messages in global context:

var log = require("cf-nodejs-logging-support");
...
log.info("Message logged in global context"); 

Adding context properties is not possible, which is indicated by the return value of the setter methods:

var log = require("cf-nodejs-logging-support");
...
var result = log.setContextProperty("my-property", "my-value")
// result: false

Request context

The library adds context bound functions to request objects in case it has been attached as middleware. Use the provided req.logger object to log messages in request contexts:

app.get('/', function (req, res) {
    var reqLogger = req.logger; // reqLogger logs in request context
    ...
    reqLogger.info("Message logged in request context"); 
});

By default, request contexts provide following additional request related fields to logs:

  • correlation_id
  • request_id
  • tenant_id
  • tenant_subdomain

Custom context

Child loggers can also be equipped with a newly created context, including an auto-generated correlation_id. This can be useful when log messages should be correlated without being in context of an HTTP request. Switch over to the Child loggers with custom context section to learn more about this topic.