Skip to content

Commit

Permalink
Merge pull request #78 from Clever/INFRANG-6518-2
Browse files Browse the repository at this point in the history
add asyncstore support
  • Loading branch information
ChrisScotMartin authored Oct 24, 2024
2 parents de3bcb9 + 0131904 commit 6317a34
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 88 deletions.
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
12
14
18 changes: 17 additions & 1 deletion lib/logger/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class Logger {
globals = null;
logWriter = null;
logRouter = null;
asyncLocalStorage = null;

constructor(
source,
Expand All @@ -52,6 +53,7 @@ class Logger {
this.globals = {};
this.globals.source = source;
this.logWriter = output;
this.asyncLocalStorage = null;

if (process.env._TEAM_OWNER) {
this.globals.team = process.env._TEAM_OWNER;
Expand All @@ -76,6 +78,10 @@ class Logger {
}
}

setAsyncLocalStorage(asyncLocalStorage) {
this.asyncLocalStorage = asyncLocalStorage;
}

setRouter(r) {
this.logRouter = r;
}
Expand Down Expand Up @@ -238,7 +244,17 @@ class Logger {
if (LOG_LEVEL_ENUM[logLvl] < LOG_LEVEL_ENUM[this.logLvl]) {
return;
}
const data = assign({ level: logLvl }, this.globals, metadata, userdata);
// I'm not clever enough to want to do these in one line without extra vars.
// We're on a REALLY old version of TS compiling to ES5. So I don't get a lot of the fancy tools
// like ?. and ??.
const store = this.asyncLocalStorage && this.asyncLocalStorage.getStore();
const storeData = store || { get: () => ({}) };
const contextData = storeData.get("context") ? storeData.get("context") : {};
const plainContextData =
contextData instanceof Map ? Object.fromEntries(contextData) : contextData;

var data = assign({ level: logLvl }, this.globals, metadata, plainContextData, userdata);

if (this.logRouter) {
data._kvmeta = this.logRouter.route(data);
} else if (globalRouter) {
Expand Down
Loading

0 comments on commit 6317a34

Please sign in to comment.