Skip to main content
Rate this page

Logging

Creating a Logger

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

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 which 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'); //set log level all logger 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.

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

Rate this page