Skip to main content.

MetadataEnrich your logs with additional data.

The metadata feature of LambdaLog allows you to enrich your log messages with additional properties either at a global or individual/local scale. All properties provided in the metadata is injected into the top-level of the generated JSON object that is logged, making it easier to query in log managers, such as CloudWatch Logs. Metadata is typically statically set, but for more advanced usage, you may also set Dynamic Metadata.

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

Typically it is best to provide an object for your metadata, although you may pass in any primitive value as metadata as well, such as, String, Number, and Boolean.

Global Metadata

Global metadata is defined properties that will be included in every log message generated by LambdaLog. This is useful for injecting runtime information, such as function name or invocation ID, into all of your log messages automatically. Global metadata is set as the configuration option meta within LambdaLog.

1const log = require('lambda-log');
2 
3// Set metadata as an object with many properties:
4// Note: this will overwrite any existing global metadata you've added.
5log.options.meta = {
6 prop1: 'test',
7 prop2: 123,
8 prop3: true,
9 prop4: new Error('It even handles errors!')
10};
11 
12// Or you may set the properties directly without overwriting existing global metadata:
13log.options.meta.prop1 = 'test';
14log.options.meta.prop2 = 123;
15log.options.meta.prop3 = true;
16log.options.meta.prop4 = new Error('It even handles errors!');
17 
18// Example log message:
19log.info('Example with global metadata');
20/* => {
21 "_logLevel": "info",
22 "msg": "Example with global metadata",
23 "prop1": "test",
24 "prop2": 123,
25 "prop3": true,
26 "prop4": {
27 "name": "Error",
28 "message": "It even handles errors!",
29 "stack": "..."
30 },
31 "_tags": []
32 } */
1const { LambdaLog } = require('lambda-log');
2 
3// Create new instance of the LambdaLog class
4const log = new LambdaLog({
5 // set global metadata
6 meta: {
7 prop1: 'test',
8 prop2: 123,
9 prop3: true,
10 prop4: new Error('It even handles errors!')
11 }
12});
13 
14// You can still add additional global metadata later on
15log.options.meta.prop5 = ['more', 'data'];
16 
17// Example log message:
18log.info('Example with global metadata');
19/* => {
20 "_logLevel": "info",
21 "msg": "Example with global metadata",
22 "prop1": "test",
23 "prop2": 123,
24 "prop3": true,
25 "prop4": {
26 "name": "Error",
27 "message": "It even handles errors!",
28 "stack": "..."
29 },
30 "prop5": ["more", "data"],
31 "_tags": []
32 } */

Local Metadata

You may also add metadata at an individual/local scale to a single log message, such as information that could be important to the log message or debugging. Each of the log methods in LambdaLog provides a parameter which you can provide metadata for the specific log message.

1const log = require('lambda-log');
2 
3// Add global metadata (optional)
4log.options.meta.prop1 = 'test';
5 
6// Example log message:
7log.info('Example with local metadata', { hello: 'world' });
8/* => {
9 "_logLevel": "info",
10 "msg": "Example with local metadata",
11 "prop1": "test",
12 "hello": "world",
13 "_tags": []
14 } */
15 
16// With primitive types:
17log.info('Example with local metadata', 'This is a string as metadata');
18/* => {
19 "_logLevel": "info",
20 "msg": "Example with local metadata",
21 "prop1": "test",
22 "meta": "This is a string as metadata",
23 "_tags": []
24 } */
25 
26// Pass in a variable, such as an error:
27const err = new Error('Something bad happened')
28log.info('Example with local metadata', err);
29/* => {
30 "_logLevel": "info",
31 "msg": "Example with local metadata",
32 "prop1": "test",
33 "meta": {
34 "name": "Error",
35 "message": "Something bad happened",
36 "stack": "..."
37 },
38 "_tags": []
39 } */
40 
41// Use shorthand object syntax for better labeling:
42const err = new Error('Something bad happened')
43log.info('Example with local metadata', { err });
44/* => {
45 "_logLevel": "info",
46 "msg": "Example with local metadata",
47 "prop1": "test",
48 "err": {
49 "name": "Error",
50 "message": "Something bad happened",
51 "stack": "..."
52 },
53 "_tags": []
54 } */