Enhanced TagsCustomize tags on your logs with variables and functions.
New in version 3.0.0, log tags can be further customized to fit your needs. There are two new methods of enhancing the tags; with tag variables and functions.
Tag Variables
LambdaLog now has the ability to interpret tag variables for each log message. Variables are surrounded with double-angle brackets (<<>>
). As of currently, the following variables are available:
Variable | Description | Example |
---|---|---|
level | The log level for the given log message. | <<level>> |
Additional variable tags will be added in future versions of LambdaLog.
Let's see an example of using variable tags:
1const log = require('lambda-log'); 2 3log.options.tags = ['<<level>>']; 4 5log.info('Info message'); 6// => { "_logLevel": "info", "msg": "Info message", "_tags": ["info"] } 7 8log.warn('Warn message'); 9// => { "_logLevel": "warn", "msg": "Warn message", "_tags": ["warn"] }10 11log.error('Error message');12// => { "_logLevel": "error", "msg": "Info message", "_tags": ["error"] }13 14// Even with a custom log level:15log.fatal('Fatal message');16// => { "_logLevel": "fatal", "msg": "Fatal message", "_tags": ["fatal"] }
Tag Variables can be used anywhere tags can be specified; in the global tags and log-specific tags.
Tag Functions
Tag Functions allow you to add tag(s) to a log message at log time dynamically. You may include functions as items within a tag array that are executed when a log is generated. Tag Functions can be included in the global tags and log-specific tags.
function({ level, meta, options })
Each Tag Function is called with an object as the only parameter. This function must return a String
or null
. If this function returns null
, a tag will not be added to the log message.
Parameter | Type | Description |
---|---|---|
obj | Object | Object containing access to useful values. |
obj.level | String | The log level for the given log message. |
obj.meta | Object | The compiled metadata for the given log message. |
obj.options | Object | The options object from the LambdaLog instance. |
Examples
Include "debug" tag when debug mode is enabled:
1const log = require('lambda-log');2 3log.options.debug = true;4log.options.tags = [function ({ options }) {5 return options.debug ? 'debug' : null;6}];7 8log.info('Test log');9// => { "_logLevel": "info", "msg": "Test log", "_tags": ["debug"] }
Include tag from metadata:
1const log = require('lambda-log');2 3log.options.tags = [function ({ meta }) {4 return meta.namespace ? meta.namespace : null;5}];6 7log.info('Test log', { namespace: 'my-module' });8// => { "_logLevel": "info", "msg": "Test log", "_tags": ["my-module"] }
Custom log level tag:
1const log = require('lambda-log');2 3log.options.tags = [function ({ level }) {4 return `level-${level}`;5}];6 7log.info('Test log');8// => { "_logLevel": "info", "msg": "Test log", "_tags": ["level-info"] }
Generate a UUID for each error log:
1const log = require('lambda-log');2const uuid = require('uuid/v4');3 4log.options.tags = [function ({ level }) {5 return level === 'error' ? uuid() : null;6}];7 8log.error('Test log');9// => { "_logLevel": "error", "msg": "Test log", "_tags": ["60afb76c-d87d-46c0-9bd4-38f429085ba8"] }