Skip to content

Commit

Permalink
feat: introduce logger class
Browse files Browse the repository at this point in the history
  • Loading branch information
tschoffelen committed Sep 7, 2024
1 parent ea1178d commit aacff02
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
17 changes: 10 additions & 7 deletions packages/api/src/events/auto-trace.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@ import {
GetApisCommand,
} from "@aws-sdk/client-apigatewayv2";
import { acquireLock, releaseLock } from "../lib/locks";
import Logger from "../lib/logger";

const supportedRuntimes = ["nodejs16.x", "nodejs18.x", "nodejs20.x"];
const lambdaExecWrapper = "/opt/nodejs/tracer_wrapper";

const logger = new Logger("auto-trace");

const getAccountLambdas = async () => {
const lambdaClient = new LambdaClient();

Expand All @@ -21,7 +24,7 @@ const getAccountLambdas = async () => {
do {
const listFunctionsCommand = new ListFunctionsCommand({
Marker: nextToken,
MaxItems: 50
MaxItems: 50,
});
const { Functions, NextMarker } =
await lambdaClient.send(listFunctionsCommand);
Expand Down Expand Up @@ -62,7 +65,7 @@ export const autoTrace = async () => {
// Make sure we lock so that only one process is updating lambdas
const lockAcquired = await acquireLock("auto-trace");
if (!lockAcquired) {
console.log("Lock not acquired, skipping");
logger.info("Lock not acquired, skipping");
return;
}

Expand Down Expand Up @@ -96,7 +99,7 @@ export const autoTrace = async () => {
);
});

console.log(`Found ${lambdasWithoutLayer.length} lambdas to update`);
logger.info(`Found ${lambdasWithoutLayer.length} lambdas to update`);

for (const lambda of lambdasWithoutLayer) {
try {
Expand All @@ -122,13 +125,13 @@ export const autoTrace = async () => {
const res = await new LambdaClient().send(
updateFunctionConfigurationCommand,
);
console.log(res);
logger.info(res);

console.log(`✓ Updated ${lambda.FunctionName}`);
logger.info(`✓ Updated ${lambda.FunctionName}`);
// TODO: save function info in DynamoDB
} catch (e) {
console.log(`✗ Failed to update ${lambda.FunctionName}`);
console.error(e);
logger.warn(`✗ Failed to update ${lambda.FunctionName}`);
logger.warn(e);
}
}

Expand Down
19 changes: 19 additions & 0 deletions packages/api/src/lib/logger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Logger {
constructor(prefix) {
this.prefix = `[${prefix}]`;
}

info(...message) {
console.log(this.prefix, ...message);
}

error(...message) {
console.error(this.prefix, ...message);
}

warn(...message) {
console.warn(this.prefix, ...message);
}
}

export default Logger;

0 comments on commit aacff02

Please sign in to comment.