Skip to content

Commit

Permalink
chore(core): refactor logger to make all defined and non-defined logg…
Browse files Browse the repository at this point in the history
…ing scope both hide the function name and file path
  • Loading branch information
kokokuo committed Sep 15, 2022
1 parent 18a90d7 commit 9efb93e
Showing 1 changed file with 14 additions and 35 deletions.
49 changes: 14 additions & 35 deletions packages/core/src/lib/utils/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,35 +30,12 @@ export interface LoggerOptions {
displayFilePath?: DisplayFilePathTypes;
}

type LoggerMapConfig = {
[scope in LoggingScope]: LoggerOptions;
};

const defaultMapConfig: LoggerMapConfig = {
[LoggingScope.CORE]: {
level: LoggingLevel.DEBUG,
displayRequestId: false,
displayFilePath: 'hidden',
displayFunctionName: false,
},
[LoggingScope.BUILD]: {
level: LoggingLevel.DEBUG,
displayRequestId: false,
displayFilePath: 'hidden',
displayFunctionName: false,
},
[LoggingScope.SERVE]: {
level: LoggingLevel.DEBUG,
displayRequestId: false,
displayFilePath: 'hidden',
displayFunctionName: false,
},
[LoggingScope.ACCESS_LOG]: {
level: LoggingLevel.DEBUG,
displayRequestId: false,
displayFilePath: 'hidden',
displayFunctionName: false,
},
// The default logger options
const defaultLoggerOptions: LoggerOptions = {
level: LoggingLevel.DEBUG,
displayRequestId: false,
displayFilePath: 'hidden',
displayFunctionName: false,
};

export type AsyncRequestIdStorage = AsyncLocalStorage<{ requestId: string }>;
Expand All @@ -70,9 +47,11 @@ class LoggerFactory {
this.asyncReqIdStorage = new AsyncLocalStorage();

this.loggerMap = {
// Here, create default scope logger, we could add other package or extension logger name in here
[LoggingScope.CORE]: this.createLogger(LoggingScope.CORE),
[LoggingScope.BUILD]: this.createLogger(LoggingScope.BUILD),
[LoggingScope.SERVE]: this.createLogger(LoggingScope.SERVE),
[LoggingScope.ACCESS_LOG]: this.createLogger(LoggingScope.ACCESS_LOG),
};
}

Expand All @@ -96,7 +75,7 @@ class LoggerFactory {
return logger;
}
// if scope name does not exist in map or exist but would like to update config
const newLogger = this.createLogger(scopeName as LoggingScope, options);
const newLogger = this.createLogger(scopeName, options);
this.loggerMap[scopeName] = newLogger;
return newLogger;
}
Expand All @@ -113,19 +92,19 @@ class LoggerFactory {
});
}

private createLogger(name: LoggingScope, options?: LoggerOptions) {
private createLogger(name: string, options?: LoggerOptions) {
return new Logger({
name,
minLevel: options?.level || defaultMapConfig[name].level,
minLevel: options?.level || defaultLoggerOptions.level,
// use function call for requestId, then when logger get requestId, it will get newest store again
requestId: () => this.asyncReqIdStorage.getStore()?.requestId as string,
displayRequestId:
options?.displayRequestId || defaultMapConfig[name].displayRequestId,
options?.displayRequestId || defaultLoggerOptions.displayRequestId,
displayFunctionName:
options?.displayFunctionName ||
defaultMapConfig[name].displayFunctionName,
defaultLoggerOptions.displayFunctionName,
displayFilePath:
options?.displayFilePath || defaultMapConfig[name].displayFilePath,
options?.displayFilePath || defaultLoggerOptions.displayFilePath,
});
}
}
Expand Down

0 comments on commit 9efb93e

Please sign in to comment.