Skip to content

Commit aacff02

Browse files
committed
feat: introduce logger class
1 parent ea1178d commit aacff02

File tree

2 files changed

+29
-7
lines changed

2 files changed

+29
-7
lines changed

packages/api/src/events/auto-trace.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,13 @@ import {
88
GetApisCommand,
99
} from "@aws-sdk/client-apigatewayv2";
1010
import { acquireLock, releaseLock } from "../lib/locks";
11+
import Logger from "../lib/logger";
1112

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

16+
const logger = new Logger("auto-trace");
17+
1518
const getAccountLambdas = async () => {
1619
const lambdaClient = new LambdaClient();
1720

@@ -21,7 +24,7 @@ const getAccountLambdas = async () => {
2124
do {
2225
const listFunctionsCommand = new ListFunctionsCommand({
2326
Marker: nextToken,
24-
MaxItems: 50
27+
MaxItems: 50,
2528
});
2629
const { Functions, NextMarker } =
2730
await lambdaClient.send(listFunctionsCommand);
@@ -62,7 +65,7 @@ export const autoTrace = async () => {
6265
// Make sure we lock so that only one process is updating lambdas
6366
const lockAcquired = await acquireLock("auto-trace");
6467
if (!lockAcquired) {
65-
console.log("Lock not acquired, skipping");
68+
logger.info("Lock not acquired, skipping");
6669
return;
6770
}
6871

@@ -96,7 +99,7 @@ export const autoTrace = async () => {
9699
);
97100
});
98101

99-
console.log(`Found ${lambdasWithoutLayer.length} lambdas to update`);
102+
logger.info(`Found ${lambdasWithoutLayer.length} lambdas to update`);
100103

101104
for (const lambda of lambdasWithoutLayer) {
102105
try {
@@ -122,13 +125,13 @@ export const autoTrace = async () => {
122125
const res = await new LambdaClient().send(
123126
updateFunctionConfigurationCommand,
124127
);
125-
console.log(res);
128+
logger.info(res);
126129

127-
console.log(`✓ Updated ${lambda.FunctionName}`);
130+
logger.info(`✓ Updated ${lambda.FunctionName}`);
128131
// TODO: save function info in DynamoDB
129132
} catch (e) {
130-
console.log(`✗ Failed to update ${lambda.FunctionName}`);
131-
console.error(e);
133+
logger.warn(`✗ Failed to update ${lambda.FunctionName}`);
134+
logger.warn(e);
132135
}
133136
}
134137

packages/api/src/lib/logger.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Logger {
2+
constructor(prefix) {
3+
this.prefix = `[${prefix}]`;
4+
}
5+
6+
info(...message) {
7+
console.log(this.prefix, ...message);
8+
}
9+
10+
error(...message) {
11+
console.error(this.prefix, ...message);
12+
}
13+
14+
warn(...message) {
15+
console.warn(this.prefix, ...message);
16+
}
17+
}
18+
19+
export default Logger;

0 commit comments

Comments
 (0)