|
| 1 | +/** |
| 2 | + * A simple logging wrapper. |
| 3 | + */ |
| 4 | + |
| 5 | +/** |
| 6 | + * Format placeholder for the log message. |
| 7 | + */ |
| 8 | +export const MSG_PLACEHOLDER = '{msg}'; |
| 9 | + |
| 10 | +/** |
| 11 | + * Format placeholder for the log type. |
| 12 | + */ |
| 13 | +export const TYPE_PLACEHOLDER = '{type}'; |
| 14 | + |
| 15 | +/** |
| 16 | + * @class |
| 17 | + * Logging wrapper class. |
| 18 | + */ |
| 19 | +export class Logger |
| 20 | +{ |
| 21 | + /** |
| 22 | + * @constructor |
| 23 | + * Makes a new logger instance. |
| 24 | + * @param fmt The logger format. |
| 25 | + */ |
| 26 | + constructor(fmt?: string) |
| 27 | + { |
| 28 | + this._format = fmt ?? MSG_PLACEHOLDER; |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * @private |
| 33 | + */ |
| 34 | + private _format: string; |
| 35 | + |
| 36 | + /** |
| 37 | + * Whether to print verbose logs. |
| 38 | + */ |
| 39 | + public verboseMode: boolean = false; |
| 40 | + |
| 41 | + /** |
| 42 | + * Whether to print debugging logs. |
| 43 | + */ |
| 44 | + public debugMode: boolean = false; |
| 45 | + |
| 46 | + /** |
| 47 | + * Format a log string. |
| 48 | + * @param fmt The format. |
| 49 | + * @param msg The message. |
| 50 | + * @param type The log type. |
| 51 | + * @returns Formatted string. |
| 52 | + */ |
| 53 | + public static formatLog(fmt: string, msg: string, type: string): string |
| 54 | + { |
| 55 | + return fmt |
| 56 | + .replace(MSG_PLACEHOLDER, msg) |
| 57 | + .replace(TYPE_PLACEHOLDER, type); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Make a log text, printed onto console. |
| 62 | + * @param type The log type. |
| 63 | + * @param msg The message strings. |
| 64 | + * @returns A formatted string. |
| 65 | + */ |
| 66 | + public makeLog(type: string, ...msg: string[]): string |
| 67 | + { |
| 68 | + return Logger.formatLog(this._format, msg.join(' '), type); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Print debug logs (if debugMode is set). |
| 73 | + * @param msg |
| 74 | + * @returns Itself. |
| 75 | + */ |
| 76 | + public debug(...msg: string[]): this |
| 77 | + { |
| 78 | + if (this.debugMode) |
| 79 | + console.debug(this.makeLog('debug', ...msg)); |
| 80 | + return this; |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Print verbose logs (if verboseMode is set). |
| 85 | + * @param msg |
| 86 | + * @returns Itself. |
| 87 | + */ |
| 88 | + public verbose(...msg: string[]): this |
| 89 | + { |
| 90 | + if (this.verboseMode) |
| 91 | + console.log(this.makeLog('verbose', ...msg)); |
| 92 | + return this; |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Print logs. |
| 97 | + * @param msg |
| 98 | + * @returns Itself. |
| 99 | + */ |
| 100 | + public log(...msg: string[]): this |
| 101 | + { |
| 102 | + console.log(this.makeLog('log', ...msg)); |
| 103 | + return this; |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Print info logs. |
| 108 | + * @param msg |
| 109 | + * @returns Itself. |
| 110 | + */ |
| 111 | + public info(...msg: string[]): this |
| 112 | + { |
| 113 | + console.info(this.makeLog('info', ...msg)); |
| 114 | + return this; |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * Print warnings. |
| 119 | + * @param msg |
| 120 | + * @returns Itself. |
| 121 | + */ |
| 122 | + public warn(...msg: string[]): this |
| 123 | + { |
| 124 | + console.warn(this.makeLog('warn', ...msg)); |
| 125 | + return this; |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * Print errors. |
| 130 | + * @param msg |
| 131 | + * @returns Itself. |
| 132 | + */ |
| 133 | + public error(...msg: string[]): this |
| 134 | + { |
| 135 | + console.error(this.makeLog('error', ...msg)); |
| 136 | + return this; |
| 137 | + } |
| 138 | +} |
0 commit comments