|
| 1 | +// tslint:disable:no-bitwise |
| 2 | + |
| 3 | +import chalk from 'chalk'; |
| 4 | +import { inject, injectable } from 'inversify'; |
| 5 | +import { EOL } from 'os'; |
| 6 | +import { LogLevel } from './LogLevel'; |
| 7 | + |
| 8 | +/** |
| 9 | + * A basic logger |
| 10 | + * |
| 11 | + * @export |
| 12 | + * @class Logger |
| 13 | + * @since 0.0.1 |
| 14 | + * @version 0.0.1 |
| 15 | + * @author Yannick Fricke <yannickfricke@googlemail.com> |
| 16 | + * @license MIT |
| 17 | + * @copyright MedjaiBot https://github.com/MedjaiBot/server |
| 18 | + */ |
| 19 | +@injectable() |
| 20 | +export class Logger { |
| 21 | + |
| 22 | + public logLevel: number; |
| 23 | + /** |
| 24 | + * The stream where to write normal messages |
| 25 | + * |
| 26 | + * @private |
| 27 | + * @type {NodeJS.WriteStream} |
| 28 | + * @memberof Logger |
| 29 | + */ |
| 30 | + private outputStream: NodeJS.WriteStream; |
| 31 | + |
| 32 | + /** |
| 33 | + * The stream where to write error messages |
| 34 | + * |
| 35 | + * @private |
| 36 | + * @type {NodeJS.WriteStream} |
| 37 | + * @memberof Logger |
| 38 | + */ |
| 39 | + private errorStream: NodeJS.WriteStream; |
| 40 | + |
| 41 | + /** |
| 42 | + * Constructs a new Logger |
| 43 | + * @param outputStream The stream where to log normal messages to |
| 44 | + * @param errorStream The stream where to log error messages to |
| 45 | + * @param logLevel The log level of this logger. Default is info. |
| 46 | + */ |
| 47 | + constructor( |
| 48 | + @inject(Symbol.for('Logger_output_stream')) |
| 49 | + outputStream = process.stdout, |
| 50 | + @inject(Symbol.for('Logger_error_stream')) |
| 51 | + errorStream = process.stderr, |
| 52 | + @inject(Symbol.for('logger.loglevel')) |
| 53 | + logLevel = LogLevel.INFO, |
| 54 | + ) { |
| 55 | + this.outputStream = outputStream; |
| 56 | + this.errorStream = errorStream; |
| 57 | + this.logLevel = logLevel; |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Logs the given message with the INFO level |
| 62 | + * |
| 63 | + * @param message The message to log |
| 64 | + */ |
| 65 | + public info(message: string) { |
| 66 | + this.log( |
| 67 | + this.outputStream, |
| 68 | + LogLevel.INFO, |
| 69 | + message, |
| 70 | + ); |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Logs the given message with the DEBUG level |
| 75 | + * @param message The message to log |
| 76 | + */ |
| 77 | + public debug(message: string) { |
| 78 | + this.log( |
| 79 | + this.outputStream, |
| 80 | + LogLevel.DEBUG, |
| 81 | + message, |
| 82 | + ); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Logs the given message with the WARN level |
| 87 | + * |
| 88 | + * @param {string} message The message to log |
| 89 | + * @param {string} error The error to log |
| 90 | + * @memberof Logger |
| 91 | + */ |
| 92 | + public warn(message: string) { |
| 93 | + this.log( |
| 94 | + this.outputStream, |
| 95 | + LogLevel.WARN, |
| 96 | + message, |
| 97 | + ); |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Logs the given message with the ERROR level |
| 102 | + * |
| 103 | + * @param {string} message The message to log |
| 104 | + * @param {string} error The error to log |
| 105 | + * @memberof Logger |
| 106 | + */ |
| 107 | + public error(message: string, error?: string) { |
| 108 | + this.log( |
| 109 | + this.errorStream, |
| 110 | + LogLevel.ERROR, |
| 111 | + error === undefined ? message : `${message} ${error}`, |
| 112 | + ); |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * Dumps an object to the stream with the DUMP level |
| 117 | + * @param description The description for the object |
| 118 | + * @param obj The object to log |
| 119 | + */ |
| 120 | + public dumpObject(description: string, obj: object) { |
| 121 | + this.log( |
| 122 | + this.outputStream, |
| 123 | + LogLevel.DUMP, |
| 124 | + `${description}: ${JSON.stringify(obj, undefined, 4)}`, |
| 125 | + ); |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * Prefixes a number with a zero when the value is less than ten |
| 130 | + * |
| 131 | + * @public |
| 132 | + * @param {number} number The number to format |
| 133 | + * @returns The formatted number |
| 134 | + * @memberof Logger |
| 135 | + */ |
| 136 | + public formatNumber(number: number, prefix: string = '0', length: number = 2) { |
| 137 | + const stringLength = number.toString().length; |
| 138 | + |
| 139 | + if (stringLength === length) { |
| 140 | + return number.toString(); |
| 141 | + } |
| 142 | + |
| 143 | + if (stringLength > length) { |
| 144 | + return number.toString().substr(0, length); |
| 145 | + } |
| 146 | + |
| 147 | + return number.toString().padStart(length, prefix); |
| 148 | + } |
| 149 | + |
| 150 | + /** |
| 151 | + * Logs a message to the given stream with the given level |
| 152 | + * |
| 153 | + * @private |
| 154 | + * @param {NodeJS.WriteStream} stream The stream to write to |
| 155 | + * @param {string} level The log level of the message |
| 156 | + * @param {string} message The message to log |
| 157 | + * @memberof Logger |
| 158 | + */ |
| 159 | + private log(stream: NodeJS.WriteStream, level: number, message: string) { |
| 160 | + // The log level as a (colored) string |
| 161 | + let logLevel; |
| 162 | + |
| 163 | + if ((this.logLevel & LogLevel.INFO) === level) { |
| 164 | + logLevel = chalk.supportsColor ? chalk.blue('INFO') : 'INFO'; |
| 165 | + } |
| 166 | + if ((this.logLevel & LogLevel.DEBUG) === level) { |
| 167 | + logLevel = chalk.supportsColor ? chalk.cyanBright('DEBUG') : 'DEBUG'; |
| 168 | + } |
| 169 | + if ((this.logLevel & LogLevel.WARN) === level) { |
| 170 | + logLevel = chalk.supportsColor ? chalk.yellow('WARN') : 'WARN'; |
| 171 | + } |
| 172 | + if ((this.logLevel & LogLevel.ERROR) === level) { |
| 173 | + logLevel = chalk.supportsColor ? chalk.redBright('ERROR') : 'ERROR'; |
| 174 | + } |
| 175 | + if ((this.logLevel & LogLevel.DUMP) === level) { |
| 176 | + logLevel = chalk.supportsColor ? chalk.cyanBright('DUMP') : 'DUMP'; |
| 177 | + } |
| 178 | + |
| 179 | + // Sets the timestamp variable to the current time and date |
| 180 | + const timestamp = new Date(); |
| 181 | + |
| 182 | + // The formatted date joined by a point |
| 183 | + const formattedDate = [ |
| 184 | + this.formatNumber(timestamp.getDate()), |
| 185 | + this.formatNumber(timestamp.getMonth() + 1), |
| 186 | + timestamp.getFullYear(), |
| 187 | + ].join('.'); |
| 188 | + |
| 189 | + // The formatted time joined by a semicolon |
| 190 | + const formattedTime = [ |
| 191 | + this.formatNumber(timestamp.getHours()), |
| 192 | + this.formatNumber(timestamp.getMinutes()), |
| 193 | + this.formatNumber(timestamp.getSeconds()), |
| 194 | + this.formatNumber(timestamp.getMilliseconds(), '0', 3), |
| 195 | + ].join(':'); |
| 196 | + |
| 197 | + // Writes the log messages to the given stream |
| 198 | + stream.write(`[${formattedDate} ${formattedTime}] [${logLevel}] ${message}${EOL}`); |
| 199 | + } |
| 200 | +} |
0 commit comments