condition check lambda
This commit is contained in:
61
lambdas/shared/utils/logger.js
Normal file
61
lambdas/shared/utils/logger.js
Normal file
@@ -0,0 +1,61 @@
|
||||
/**
|
||||
* CloudWatch-compatible structured logger for Lambda functions.
|
||||
* Outputs JSON logs that CloudWatch Logs Insights can parse and query.
|
||||
*/
|
||||
|
||||
const LOG_LEVELS = {
|
||||
debug: 0,
|
||||
info: 1,
|
||||
warn: 2,
|
||||
error: 3,
|
||||
};
|
||||
|
||||
// Default to 'info' in production, 'debug' in development
|
||||
const currentLevel = LOG_LEVELS[process.env.LOG_LEVEL?.toLowerCase()] ?? LOG_LEVELS.info;
|
||||
|
||||
/**
|
||||
* Create a log entry in CloudWatch-compatible JSON format.
|
||||
* @param {string} level - Log level (debug, info, warn, error)
|
||||
* @param {string} message - Log message
|
||||
* @param {Object} meta - Additional metadata to include
|
||||
*/
|
||||
function log(level, message, meta = {}) {
|
||||
if (LOG_LEVELS[level] < currentLevel) {
|
||||
return;
|
||||
}
|
||||
|
||||
const entry = {
|
||||
timestamp: new Date().toISOString(),
|
||||
level: level.toUpperCase(),
|
||||
message,
|
||||
...meta,
|
||||
};
|
||||
|
||||
// Add Lambda context if available
|
||||
if (process.env.AWS_LAMBDA_FUNCTION_NAME) {
|
||||
entry.function = process.env.AWS_LAMBDA_FUNCTION_NAME;
|
||||
}
|
||||
if (process.env.AWS_LAMBDA_LOG_STREAM_NAME) {
|
||||
entry.logStream = process.env.AWS_LAMBDA_LOG_STREAM_NAME;
|
||||
}
|
||||
|
||||
const output = JSON.stringify(entry);
|
||||
|
||||
switch (level) {
|
||||
case "error":
|
||||
console.error(output);
|
||||
break;
|
||||
case "warn":
|
||||
console.warn(output);
|
||||
break;
|
||||
default:
|
||||
console.log(output);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
debug: (message, meta) => log("debug", message, meta),
|
||||
info: (message, meta) => log("info", message, meta),
|
||||
warn: (message, meta) => log("warn", message, meta),
|
||||
error: (message, meta) => log("error", message, meta),
|
||||
};
|
||||
Reference in New Issue
Block a user