Using the
— node.js, javascript, typescript — 1 min read
When you're developing and debugging a Node.js Lambda function, it's important to be able to easily identify the source of any logs that are generated. This can be especially helpful if you're using a third-party library or framework, and you're not sure where the log is coming from.
One way to identify the source of a log is to use the __filename
directive. This directive returns the absolute path to the file that the code is currently executing in. So, if you log the value of __filename
to the console, you'll be able to see the file name and line number of the code that generated the log.
For example, the following code will log the source of the log to the console:
console.info(`${__filename}:handler `, 'getting all users...');
This code will output the following log to the console:
/dist/get-users/index.js:12 getting all users...
This log tells us that the log was generated in the /dist/get-users/index.js
file. This information can be very helpful for debugging and troubleshooting issues with your Lambda function.
require('dotenv').config()import { statusCodes } from '../common/constants/status'import services from '../common/services'
export const handler = async (ev: any) => { console.info(`${__filename}:handler `, 'getting all users...')
const [users, status, error] = await services.getUsers()
return services.response.success(statusCodes.OK, users)}