| 17 | const logLevels: Array<LogLevel> = ["log", "error", "warn", "info", "debug"]; |
| 18 | |
| 19 | export class Logger { |
| 20 | #name: string; |
| 21 | readonly #level: number; |
| 22 | #filteredKeys: string[] = []; |
| 23 | #jsonReplacer?: (key: string, value: unknown) => unknown; |
| 24 | #additionalFields: () => Record<string, unknown>; |
| 25 | |
| 26 | constructor( |
| 27 | name: string, |
| 28 | level: LogLevel = "info", |
| 29 | filteredKeys: string[] = [], |
| 30 | jsonReplacer?: (key: string, value: unknown) => unknown, |
| 31 | additionalFields?: () => Record<string, unknown> |
| 32 | ) { |
| 33 | this.#name = name; |
| 34 | this.#level = logLevels.indexOf((env.TRIGGER_LOG_LEVEL ?? level) as LogLevel); |
| 35 | this.#filteredKeys = filteredKeys; |
| 36 | this.#jsonReplacer = createReplacer(jsonReplacer); |
| 37 | this.#additionalFields = additionalFields ?? (() => ({})); |
| 38 | } |
| 39 | |
| 40 | child(fields: Record<string, unknown>) { |
| 41 | return new Logger( |
| 42 | this.#name, |
| 43 | logLevels[this.#level], |
| 44 | this.#filteredKeys, |
| 45 | this.#jsonReplacer, |
| 46 | () => ({ ...this.#additionalFields(), ...fields }) |
| 47 | ); |
| 48 | } |
| 49 | |
| 50 | // Return a new Logger instance with the same name and a new log level |
| 51 | // but filter out the keys from the log messages (at any level) |
| 52 | filter(...keys: string[]) { |
| 53 | return new Logger(this.#name, logLevels[this.#level], keys, this.#jsonReplacer); |
| 54 | } |
| 55 | |
| 56 | static satisfiesLogLevel(logLevel: LogLevel, setLevel: LogLevel) { |
| 57 | return logLevels.indexOf(logLevel) <= logLevels.indexOf(setLevel); |
| 58 | } |
| 59 | |
| 60 | log(message: string, ...args: Array<Record<string, unknown> | undefined>) { |
| 61 | if (this.#level < 0) return; |
| 62 | |
| 63 | this.#structuredLog(console.log, message, "log", ...args); |
| 64 | } |
| 65 | |
| 66 | error(message: string, ...args: Array<Record<string, unknown> | undefined>) { |
| 67 | if (this.#level < 1) return; |
| 68 | |
| 69 | this.#structuredLog(console.error, message, "error", ...args); |
| 70 | } |
| 71 | |
| 72 | warn(message: string, ...args: Array<Record<string, unknown> | undefined>) { |
| 73 | if (this.#level < 2) return; |
| 74 | |
| 75 | this.#structuredLog(console.warn, message, "warn", ...args); |
| 76 | } |
nothing calls this directly
no outgoing calls
no test coverage detected
searching dependent graphs…