ExamplesCommon patterns and examples using LambdaLog.
To help jump start development with LambdaLog and some of the features/patterns, there are a handful of different examples that have been used in the real-world.
Tag log messages per file
A good use case for global tags is by labeling your log messages with the file the log is coming from. You will need to create a new instance of LambdaLog for each of your files instead of using the default instance.
1// index.js 2 3const { LambdaLog } = require('lambda-log'); 4 5exports.handler = async function handler(event) { 6 // Require other files within the handler to prevent caching issues. 7 const api = require('./api'); 8 const log = new LambdaLog({ 9 tags: ['index']10 });11 12 log.info('Starting process'); // => { "_logLevel": "info", "msg": "Starting process", "_tags": ["index"] } 13 14 // ...15 16 try {17 if(api.isValidId(event.id)) {18 const result = await api.getData(event);19 20 return result;21 }22 23 return false;24 } catch(error) {25 log.error(error).throw; // => { "_logLevel": "error", "msg": "...", "name": "Error", "stack": "...", "_tags": ["index"] } 26 }27};
All logs will contain ["index"]
as their tag.
1// api.js 2 3const { LambdaLog } = require('lambda-log'); 4const log = new LambdaLog({ 5 tags: ['api'] 6}); 7 8module.exports = { 9 getData: async function getData(event) {10 log.info('Calling API with event data', { event }); // => { "_logLevel": "info", "msg": "Calling API with event data", "event": { ... }, "_tags": ["api"] } 11 12 // ..get data13 14 return data;15 },16 17 isValidId(id) {18 if(/^[0-9a-z]{10}/.test(id)) {19 return true;20 }21 22 log.warn(`Invalid ID provided`, { id }, ['isValidId']); // => { "_logLevel": "warn", "msg": "Invalid ID provided", "id": "...", "_tags": ["api", "isValidId"] } 23 return false;24 }25};
All logs will contain ["api"]
as their tag. The last log.warn()
also has local tags as well which will yield ["api", "isValidId"]
as its tags.
Include Lambda Function Name/Invocation ID in the log messages
You can add additional information from the Lambda Context Object into the global metadata of your logs.
1// index.js 2 3exports.handler = async function handler(event, context) { 4 // Require inside the handler to prevent caching issues on subsequent executions 5 const log = require('lambda-log'); 6 log.options.meta = { 7 functionName: context.functionName, 8 requestId: context.awsRequestId 9 };10 11 log.info('Starting process'); // => { "_logLevel": "info", "msg": "Starting process", "functionName": "my-lambda-function", "requestId": "xxx", "_tags": [] } 12 13 // ...14 15 return true;16};
1// index.js 2 3const { LambdaLog } = require('lambda-log'); 4 5exports.handler = async function handler(event, context) { 6 const log = new LambdaLog({ 7 meta: { 8 functionName: context.functionName, 9 requestId: context.awsRequestId10 }11 });12 13 log.info('Starting process'); // => { "_logLevel": "info", "msg": "Starting process", "functionName": "my-lambda-function", "requestId": "xxx", "_tags": [] } 14 15 // ...16 17 return true;18};