Skip to content

Commit

Permalink
testing aysnc store
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisScotMartin committed Oct 8, 2024
1 parent 7949d1a commit 3e08662
Show file tree
Hide file tree
Showing 2 changed files with 1,346 additions and 728 deletions.
10 changes: 8 additions & 2 deletions lib/logger/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,25 +33,28 @@ function getGlobalRouter() {
return globalRouter;
}

// This is a port from kayvee-go/logger/logger.go
// This is a modified from kayvee-go/logger/logger.go
class Logger {
formatter = null;
logLvl = null;
globals = null;
logWriter = null;
logRouter = null;
asyncLocalStorage = null;

constructor(
source,
logLvl = process.env.KAYVEE_LOG_LEVEL,
formatter = kv.format,
output = console.error,
asyncLocalStorage = null,
) {
this.formatter = formatter;
this.logLvl = this._validateLogLvl(logLvl);
this.globals = {};
this.globals.source = source;
this.logWriter = output;
this.asyncLocalStorage = asyncLocalStorage;

if (process.env._TEAM_OWNER) {
this.globals.team = process.env._TEAM_OWNER;
Expand Down Expand Up @@ -238,7 +241,10 @@ class Logger {
if (LOG_LEVEL_ENUM[logLvl] < LOG_LEVEL_ENUM[this.logLvl]) {
return;
}
const data = assign({ level: logLvl }, this.globals, metadata, userdata);
const storeData = this.asyncLocalStorage ? this.asyncLocalStorage.getStore() || {} : {};
const contextData = storeData.context ? { context: storeData.context } : {};

const data = assign({ level: logLvl }, this.globals, metadata, contextData, userdata);
if (this.logRouter) {
data._kvmeta = this.logRouter.route(data);
} else if (globalRouter) {
Expand Down
Loading

0 comments on commit 3e08662

Please sign in to comment.