Skip to main content.

Custom Log LevelsCustomize the built-in log levels to fit your needs.

3.1.0

The built-in log levels may not always fit the needs of your application so LambdaLog provides instrumentation to create your own custom log levels. Custom Log Levels can be defined in two different ways: Creating an instance of the LambdaLog class or, new in version 3, by calling addLevel() on an existing instance of a LambdaLog class.

Any custom log levels added to LambdaLog will automatically create a dynamic method on the LambdaLog class. For example, if you create a new log level called fatal, you'll be able to use the shorthand method log.fatal().

In order to add new features, this feature is DEPRECATED. If you are currently using custom log levels, expect this feature to be removed in the next major version of LambdaLog.

How do I add Custom Log Levels?

Easy! Either create a new instance of LambdaLog and pass in your log levels as the second argument or use the addLevel() method on an existing instance of the LambdaLog class. You can even overwrite an existing log level to add custom functionality.

1const log = require('lambda-log');
2 
3// With string handler
4log.addLevel('fatal', 'error');
5 
6// With function handler
7log.addLevel('fatal', function (message) {
8 // custom functionality...
9 return 'error';
10});
1const { LambdaLog } = require('lambda-log');
2const log = new LambdaLog({}, {
3 // Log Levels object...
4 fatal: 'error'
5});

Log Levels Configuration

As seen in the examples above, you can pass in an object containing log levels into a new instance of LambdaLog or add new levels to an existing instance of LambdaLog with addLevel(). Both share the same functionality, but have one distinct difference: By creating a new instance, you can create many new levels at once whereas addLevel() only adds a single log level at a time.

When passing an log level configuration object into the constructor of the LambdaLog class, the object must be formed with the key being the name of the log level and the value being either a string method name from console — such as log, info, warn, error, debug — or a function that returns a string method name from console. The same goes for addLevel() where the first argument is the name of the log level and the second argument is the handler — string method name or function.

Now you know how to set log levels, let's look at the signature of a log level handler function.

function(message)

Each custom log level handler function is called for every log that is created for the given level. The function is called in the scope of the current LambdaLog instance with a single parameter — message. This function must return either a String method name from console to use for logging the message or false to prevent the message from being logged.

ScopeLambdaLogThe LambdaLog instance
ParameterTypeDescription
messageLogMessageThe LogMessage instance for the given log.

Examples

With addLevel():

1const log = require('lambda-log');
2 
3// fatal log level that logs to `console.error`
4log.addLevel('fatal', 'error');
5 
6// unicorn log level that modifies the message
7log.addLevel('unicorn', function (message) {
8 // prepend an emoji to every message
9 message.msg = `🦄 ${message.msg}`;
10 
11 return 'log';
12});
13 
14// log level that uses custom options
15log.addLevel('trace', function () {
16 // if `log.options.trace` is false, don't log the message
17 if(!this.options.trace) return false;
18 
19 // Otherwise, log the message
20 return 'debug';
21});

With a new LambdaLog Instance:

1const { LambdaLog } = require('lambda-log');
2const log = new LambdaLog({
3 // example of custom option
4 trace: true
5}, {
6 // fatal log level that logs to `console.error`
7 fatal: 'error',
8 
9 // unicorn log level that modifies the message
10 unicorn(message) {
11 // prepend an emoji to every message
12 message.msg = `🦄 ${message.msg}`;
13 
14 return 'log';
15 },
16 
17 // log level that uses custom options
18 trace() {
19 // if `log.options.trace` is false, don't log the message
20 if(!this.options.trace) return false;
21 
22 // Otherwise, log the message
23 return 'debug';
24 }
25});