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 6527620
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 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
14 changes: 8 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,16 @@ 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();
console.log(correlationId, this.correlationId);
this.resetCorrelationId = correlationId ? false : true;
this.applicationName = applicationName;
this.persistentContext = {};
this.console =
Expand Down Expand Up @@ -63,9 +66,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 +262,7 @@ class Logger {
setCorrelationId(correlationId: string): void {
if (correlationId) {
this.correlationId = correlationId;
this.resetCorrelationId = true;
}
}

Expand All @@ -275,9 +277,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

0 comments on commit 6527620

Please sign in to comment.