Message Logs

In addition to request logging this library also supports logging of application messages. Message logs contain at least some message and also CF metadata.

Table of contents
  1. Logging levels
  2. Writing message logs
  3. Checking log severity levels
  4. Get the logging level threshold

Logging levels

Following common logging levels are supported:

  • error
  • warn
  • info
  • verbose
  • debug
  • silly

Set the minimum logging level for logs as follows:

log.setLoggingLevel("info");

In addition there is an off logging level available to disable log output completely.

Writing message logs

There are so called convenience methods available for all supported logging levels. These can be called to log a message using the corresponding level. It is also possible to use standard format placeholders equivalent to the util.format method.

You can find several usage examples below demonstrating options to be specified when calling a log method. All methods get called on a logger object, which provides a so called logging context. You can find more information about logging contexts in the Logging Contexts chapter. In the simplest case, logger is an instance of imported log module.

  • Simple message:

    logger.info("Hello World"); 
    // ... "msg":"Hello World" ...
    
  • Message with additional numeric value:

    logger.info("Listening on port %d", 5000); 
    // ... "msg":"Listening on port 5000" ...
    
  • Message with additional string values:

    logger.info("This %s a %s", "is", "test"); 
    // ... "msg":"This is a test" ...
    
  • Message with additional json object to be embedded in to the message:

    logger.info("Test data %j", {"field" :"value"}, {}); 
    // ... "msg":"Test data {\"field\": \"value\"}" ...
    
  • In case you want to set the actual logging level from a variable, you can use following method, which also supports format features described above:

    var level = "debug";
    logger.logMessage(level, "Hello World"); 
    // ... "msg":"Hello World" ...
    

Checking log severity levels

It can be useful to check if messages with a specific severity level would be logged. You can check if a logging level is active as follows:

var isInfoActive = log.isLoggingLevel("info");
if (isInfoActive) {
 log.info("message logged with severity 'info'");
}

There are convenience methods available for this feature:

var isDebugActive = log.isDebug();

Get the logging level threshold

For local testing purposes you can also get the current level of a logger instance by calling the getLoggingLevel method:

log.getLoggingLevel();