Skip to main content

Logging

Creating a Logger

The SAP Cloud SDK provides an easy way to create a logger:

import { createLogger } from '@sap-cloud-sdk/util';

const myLogger = createLogger('my-message-context');

The string argument in the createLogger is the identifier for the logger and you should use a separate message context for each logical part of your application. Since it is used to group messages or set the log level per context.

Use a Logger

The logger provides the usual log methods:

myLogger.debug('Here is some debug.');
myLogger.info('Here is some info.');
myLogger.warn('Here is a warning.');
myLogger.error('Here is a error.');

The default value for the log level is info. In the example above the first line would not appear in the logs. You can set the log level either on creation:

const myLogger = createLogger({
messageContext: 'my-message-context',
level: 'info'
});

or later in you application via the setLogLevel() method:

setLogLevel('error', 'my-message-context');

Logging Exceptions

In the example above a string was passed to the logging method:

myLogger.info('Here is some info.');

and in the logs, you would find a related entry

TimeStamp [INFO] my-message-context The message you provided.

However, you can also pass an error object also known as an exception to the log statement. In this case, the log statement will contain only the message of the exception for all log functions except for error. If myLogger.error(err) is called with an error object it will also log the stack trace:

myLogger.error(new Error('Log this message and stack.')

Exception Logger

The SAP Cloud SDK enables an exception logger once you create a logger instance somewhere in your project. The exception logger logs unhandled exception as if you would log the error manually. In other words, the exception logger does the following for you:

try {
someMethodThrowing();
} catch (err) {
logger.error(err);
throw err;
}

You can disable this functionality with the disableExceptionLogger() method.

What Happens Under the Hood

The SAP Cloud SDK logger instances are based on winston. A logger instance is created from a central container that knows all existing loggers. Via the container the SAP Cloud SDK provides methods per message context:

getLogger('my-message-context'); // get logger if present
setLogLevel('error', 'my-message-context'); // set log level

or global methods for all loggers:

setGlobalLogLevel('warn'); // the global log level to warn
muteLoggers(); // mute all loggers completely
unmuteLogger(); // unmute all loggers

Besides a log level, each logger contains a format and transports. The SAP Cloud SDK sets default values for these parts.

The transport is set to console and the format is set to local or kibana. If VCAP_SERVICE variables are present in the environment variables the kibana format is used, because the SAP Cloud SDK assumes your application is running on Cloud Foundry.

The SAP Cloud SDK sets the log level during the creation of a logger based on the following order:

  1. log level for a specific message context or logger - setLogLevel('info', messageContextOrLogger)
  2. level field in createLogger - createLogger({..., level: "info"})
  3. global log level - setGlobalLogLevel('info')
  4. default log level - info
note

Please notice that setGlobalLogLevel will not only affect the creation of the next logger, but also the log level of all existing loggers.

In case you see a need for higher flexibility of the logging instance feel free to create an issue in our repository, switch to version 2, or make a contribution.