Skip to main content.

TaggingLabel your log messages for better filtering.

Tags are additional pieces of context you may add to your log messages that help improve filtering your log messages so you can see the information that matters the most. Similar to metadata, tags can be applied globally and locally. For each log message, the global and local tags are combined and added to an array as part of the log message. For more advanced usage, you may use certain variables or functions to generate your tags. See Enhanced Tags for more information.

Global and individual/local tags are merged together when generating a log message, allowing you to utilize both options.

Tags must be a String or a Function (for enhanced tags). Other types will not be allowed anymore as tag values.

Global Tags

Tags set globally will be included in every log message generated by LambdaLog. This is useful for setting generic categories if you have processes that are running multiple instances or are sharing a single log group in CloudWatch between multiple processes. Some examples include, hostname, process ID, invocation ID, and process name.

Global tags may also be used for segmenting different aspects of your process/application. You can create a new instance of LambdaLog for each file and set the global tags to include the file name or some identifier for each one. Any logs generated within the file using the created instance of LambdaLog will include the tags.

Global tags are set using the configuration option tags within LambdaLog.

1const log = require('lambda-log');
2 
3// Set tags as an array with many tags:
4// Note: this will overwrite any existing global tags you've added.
5log.options.tags = ['some-tag', 'another-tag'];
6 
7// Or you may push the tags directly without overwriting existing global tags:
8log.options.tags.push('some-tag', 'another-tag');
9 
10// Example log message:
11log.info('Example with global tags');
12/* => {
13 "_logLevel": "info",
14 "msg": "Example with global tags",
15 "_tags": ["some-tag", "another-tag"]
16 } */
1const { LambdaLog } = require('lambda-log');
2 
3// Create new instance of the LambdaLog class
4const log = new LambdaLog({
5 // set global tags
6 tags: [
7 'some-tag',
8 'another-tag'
9 ]
10});
11 
12// You can still add additional global tags later on
13log.options.tags.push('cool-tag');
14 
15// Example log message:
16log.info('Example with global tags');
17/* => {
18 "_logLevel": "info",
19 "msg": "Example with global metadata",
20 "_tags": ["some-tag", "another-tag", "cool-tag"]
21 } */

Local Tags

You may also add tags at an individual/local scale to a single log message, such as tags that would make it easier to locate the log message later on, like a function name. Each of the log methods in LambdaLog provides a parameter which you can provide tags for the specific log message.

1const log = require('lambda-log');
2 
3// Add global tags (optional)
4log.options.tags.push('test');
5 
6// Example log message:
7log.info('Example with local tags', null, ['example-tag', 'local-tag-example']);
8/* => {
9 "_logLevel": "info",
10 "msg": "Example with local tags",
11 "_tags": ["test", "example-tag", "local-tag-example"]
12 } */