|
| 1 | +import debug, { Debugger } from 'debug'; |
| 2 | + |
| 3 | +enum LogLevel { |
| 4 | + debug = 1, |
| 5 | + info = 2, |
| 6 | + warn = 3, |
| 7 | + error = 4, |
| 8 | + none = 5, |
| 9 | +} |
| 10 | + |
| 11 | +/** |
| 12 | + * A logger class with support for multiple namespaces and log levels. |
| 13 | + */ |
| 14 | +class Logger { |
| 15 | + private _levelToOutput: LogLevel; |
| 16 | + private _logger: Debugger; |
| 17 | + |
| 18 | + constructor(levelToOutput: LogLevel, namespace: string) { |
| 19 | + this._levelToOutput = levelToOutput; |
| 20 | + this._logger = debug(namespace); |
| 21 | + } |
| 22 | + |
| 23 | + /** |
| 24 | + * creates a new Logger instance by inspecting the executing environment |
| 25 | + */ |
| 26 | + static fromEnv(namespace: string): Logger { |
| 27 | + // by default, log everything in development and nothing in production |
| 28 | + let level = process.env.NODE_ENV !== 'production' ? LogLevel.debug : LogLevel.none; |
| 29 | + |
| 30 | + if (localStorage.getItem('debug')) { |
| 31 | + // if a 'debug' key is found in localStorage, use the level in storage or 'debug' by default |
| 32 | + const storageLevel = localStorage.getItem('debug-level') || 'debug'; |
| 33 | + level = LogLevel[storageLevel as keyof typeof LogLevel]; |
| 34 | + } else if (process.env.NODE_ENV !== 'production') { |
| 35 | + // if running in development with no localStorage key, use debug |
| 36 | + level = LogLevel.debug; |
| 37 | + // set the keys so they can be easily changed in the browser DevTools |
| 38 | + localStorage.setItem('debug', '*'); |
| 39 | + localStorage.setItem('debug-level', 'debug'); |
| 40 | + } |
| 41 | + |
| 42 | + return new Logger(level, namespace); |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * log a debug message |
| 47 | + */ |
| 48 | + debug = (message: string, ...args: any[]) => this._log(LogLevel.debug, message, args); |
| 49 | + |
| 50 | + /** |
| 51 | + * log an info message |
| 52 | + */ |
| 53 | + info = (message: string, ...args: any[]) => this._log(LogLevel.info, message, args); |
| 54 | + |
| 55 | + /** |
| 56 | + * log a warn message |
| 57 | + */ |
| 58 | + warn = (message: string, ...args: any[]) => this._log(LogLevel.warn, message, args); |
| 59 | + |
| 60 | + /** |
| 61 | + * log an error message |
| 62 | + */ |
| 63 | + error = (message: string, ...args: any[]) => this._log(LogLevel.error, message, args); |
| 64 | + |
| 65 | + /** |
| 66 | + * A shared logging function which will only output logs based on the level of this Logger instance |
| 67 | + * @param level the level of the message being logged |
| 68 | + * @param message the message to log |
| 69 | + * @param args optional additional arguments to log |
| 70 | + */ |
| 71 | + private _log(level: LogLevel, message: string, args: any[]) { |
| 72 | + // don't log if the level to output is greater than the level of this message |
| 73 | + if (this._levelToOutput > level) return; |
| 74 | + |
| 75 | + // convert the provided log level number to the string name |
| 76 | + const prefix = Object.keys(LogLevel).reduce( |
| 77 | + (prev, curr) => (level === LogLevel[curr as keyof typeof LogLevel] ? curr : prev), |
| 78 | + '??', |
| 79 | + ); |
| 80 | + |
| 81 | + this._logger(`[${prefix}] ${message}`, ...args); |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +/** |
| 86 | + * the main logger for the app |
| 87 | + */ |
| 88 | +export const log = Logger.fromEnv('main'); |
| 89 | + |
| 90 | +/** |
| 91 | + * the logger for GRPC requests and responses |
| 92 | + */ |
| 93 | +export const grpcLog = Logger.fromEnv('grpc'); |
| 94 | + |
| 95 | +/** |
| 96 | + * the logger for state updates via mobx actions |
| 97 | + */ |
| 98 | +export const actionLog = Logger.fromEnv('action'); |
0 commit comments