|
| 1 | +import { Logger } from 'tslog'; |
| 2 | +import { AsyncLocalStorage } from 'async_hooks'; |
| 3 | +export { Logger as ILogger }; |
| 4 | +// The category according to package name |
| 5 | +export enum LoggingScope { |
| 6 | + CORE = 'CORE', |
| 7 | + BUILD = 'BUILD', |
| 8 | + SERVE = 'SERVE', |
| 9 | + AUDIT = 'AUDIT', |
| 10 | +} |
| 11 | + |
| 12 | +type LoggingScopeTypes = keyof typeof LoggingScope; |
| 13 | + |
| 14 | +export enum LoggingLevel { |
| 15 | + SILLY = 'silly', |
| 16 | + TRACE = 'trace', |
| 17 | + DEBUG = 'debug', |
| 18 | + INFO = 'info', |
| 19 | + WARN = 'warn', |
| 20 | + ERROR = 'error', |
| 21 | + FATAL = 'fatal', |
| 22 | +} |
| 23 | + |
| 24 | +export interface LoggerOptions { |
| 25 | + level?: LoggingLevel; |
| 26 | + displayRequestId?: boolean; |
| 27 | +} |
| 28 | + |
| 29 | +type LoggerMapConfig = { |
| 30 | + [scope in LoggingScope]: LoggerOptions; |
| 31 | +}; |
| 32 | + |
| 33 | +const defaultMapConfig: LoggerMapConfig = { |
| 34 | + [LoggingScope.CORE]: { |
| 35 | + level: LoggingLevel.DEBUG, |
| 36 | + displayRequestId: false, |
| 37 | + }, |
| 38 | + [LoggingScope.BUILD]: { |
| 39 | + level: LoggingLevel.DEBUG, |
| 40 | + displayRequestId: false, |
| 41 | + }, |
| 42 | + [LoggingScope.SERVE]: { |
| 43 | + level: LoggingLevel.DEBUG, |
| 44 | + displayRequestId: false, |
| 45 | + }, |
| 46 | + [LoggingScope.AUDIT]: { |
| 47 | + level: LoggingLevel.DEBUG, |
| 48 | + displayRequestId: false, |
| 49 | + }, |
| 50 | +}; |
| 51 | + |
| 52 | +export type AsyncRequestIdStorage = AsyncLocalStorage<{ requestId: string }>; |
| 53 | +class LoggerFactory { |
| 54 | + private loggerMap: { [scope: string]: Logger }; |
| 55 | + public readonly asyncReqIdStorage: AsyncRequestIdStorage; |
| 56 | + |
| 57 | + constructor() { |
| 58 | + this.asyncReqIdStorage = new AsyncLocalStorage(); |
| 59 | + |
| 60 | + this.loggerMap = { |
| 61 | + [LoggingScope.CORE]: this.createLogger(LoggingScope.CORE), |
| 62 | + [LoggingScope.BUILD]: this.createLogger(LoggingScope.BUILD), |
| 63 | + [LoggingScope.SERVE]: this.createLogger(LoggingScope.SERVE), |
| 64 | + }; |
| 65 | + } |
| 66 | + |
| 67 | + public getLogger({ |
| 68 | + scopeName, |
| 69 | + options, |
| 70 | + }: { |
| 71 | + scopeName: LoggingScopeTypes; |
| 72 | + options?: LoggerOptions; |
| 73 | + }) { |
| 74 | + if (!(scopeName in LoggingScope)) |
| 75 | + throw new Error( |
| 76 | + `The ${scopeName} does not belong to ${Object.keys(LoggingScope)}` |
| 77 | + ); |
| 78 | + // if scope name exist in mapper and not update config |
| 79 | + if (scopeName in this.loggerMap) { |
| 80 | + if (!options) return this.loggerMap[scopeName]; |
| 81 | + // if options existed, update settings. |
| 82 | + const logger = this.loggerMap[scopeName]; |
| 83 | + this.updateSettings(logger, options); |
| 84 | + return logger; |
| 85 | + } |
| 86 | + // if scope name does not exist in map or exist but would like to update config |
| 87 | + const newLogger = this.createLogger(scopeName as LoggingScope, options); |
| 88 | + this.loggerMap[scopeName] = newLogger; |
| 89 | + return newLogger; |
| 90 | + } |
| 91 | + |
| 92 | + private updateSettings(logger: Logger, options: LoggerOptions) { |
| 93 | + const prevSettings = logger.settings; |
| 94 | + logger.setSettings({ |
| 95 | + minLevel: options.level || prevSettings.minLevel, |
| 96 | + displayRequestId: |
| 97 | + options.displayRequestId || prevSettings.displayRequestId, |
| 98 | + }); |
| 99 | + } |
| 100 | + |
| 101 | + private createLogger(name: LoggingScope, options?: LoggerOptions) { |
| 102 | + return new Logger({ |
| 103 | + name, |
| 104 | + minLevel: options?.level || defaultMapConfig[name].level, |
| 105 | + // use function call for requestId, then when logger get requestId, it will get newest store again |
| 106 | + requestId: () => this.asyncReqIdStorage.getStore()?.requestId as string, |
| 107 | + displayRequestId: |
| 108 | + options?.displayRequestId || defaultMapConfig[name].displayRequestId, |
| 109 | + }); |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +const factory = new LoggerFactory(); |
| 114 | +export const getLogger = factory.getLogger.bind(factory); |
| 115 | +export const asyncReqIdStorage = factory.asyncReqIdStorage; |
0 commit comments