Skip to content

Commit

Permalink
feat: Clear global variable appropriately
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielMuller committed Dec 4, 2024
1 parent 9aa5d82 commit 54231c1
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@serverless-guru/logger",
"version": "1.0.5",
"version": "1.0.6",
"description": "Common logger utility",
"main": "./lib/cjs/index.js",
"types": "./lib/cjs/index.d.ts",
Expand Down
13 changes: 7 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,15 @@ class Logger {

private serviceName: string;
private correlationId: string;
private resetCorrelationId: boolean;
private applicationName: string;
private persistentContext: JSONObject;
private console: Console;

constructor(serviceName: string, applicationName: string, correlationId: string | null = null) {
this.serviceName = serviceName;
this.correlationId = correlationId ? correlationId : randomUUID();
this.resetCorrelationId = correlationId ? false : true;
this.applicationName = applicationName;
this.persistentContext = {};
this.console =
Expand Down Expand Up @@ -63,9 +65,7 @@ class Logger {

const arrayToLowerCase = (array: StringArray): StringArray => {
if (Array.isArray(array)) {
return array
.filter((el) => typeof el === "string")
.map((el) => el.toLowerCase());
return array.filter((el) => typeof el === "string").map((el) => el.toLowerCase());
}
return [];
};
Expand Down Expand Up @@ -261,6 +261,7 @@ class Logger {
setCorrelationId(correlationId: string): void {
if (correlationId) {
this.correlationId = correlationId;
this.resetCorrelationId = true;
}
}

Expand All @@ -275,9 +276,9 @@ class Logger {

clearLogContext(): void {
this.persistentContext = {};
this.correlationId = "";
this.applicationName = "";
this.serviceName = "";
if (this.resetCorrelationId) {
this.correlationId = randomUUID();
}
}

metric(activity: string, meta: MetricMeta): void {
Expand Down
13 changes: 12 additions & 1 deletion test/logger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,18 @@ describe("Log Outputs", () => {
logger.clearLogContext();
logger.info("Simple");
expect(console.info).toHaveBeenCalledWith(
'{"level":"INFO","service":"","correlationId":"","message":"Simple"}'
'{"level":"INFO","service":"testService","correlationId":"testId","message":"Simple"}'
);
});

test("Global Context Reset", () => {
const logger = new Logger("testService", "testApp");
logger.clearLogContext();
logger.info("Simple");
expect(console.info).toHaveBeenCalledWith(
expect.stringMatching(
/{"level":"INFO","service":"testService","correlationId":"[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}","message":"Simple"}/
)
);
});

Expand Down

0 comments on commit 54231c1

Please sign in to comment.