| 9 | }; |
| 10 | |
| 11 | export class Logger implements T { |
| 12 | private logger: LogHelper; |
| 13 | private prefix: string; |
| 14 | private _timeStamps: Record<string, Date>; |
| 15 | |
| 16 | constructor(logger: LogHelper, prefix?: string) { |
| 17 | this.logger = logger; |
| 18 | this.prefix = prefix ? `[${prefix}] ` : ''; |
| 19 | this._timeStamps = {}; |
| 20 | } |
| 21 | info(json: object): void { |
| 22 | this.logger.info(this.prefix + util.format('%j', json)); |
| 23 | } |
| 24 | error(json: object): void { |
| 25 | this.logger.error(this.prefix + util.format('%j', json)); |
| 26 | } |
| 27 | log(json: object): void { |
| 28 | this.info(json); |
| 29 | } |
| 30 | warn(json: object): void { |
| 31 | this.logger.warn(this.prefix + util.format('%j', json)); |
| 32 | } |
| 33 | debug(json: object): void { |
| 34 | this.logger.debug(this.prefix + util.format('%j', json)); |
| 35 | } |
| 36 | setPrefix(prefix: string) { |
| 37 | this.prefix = `[${prefix}] `; |
| 38 | } |
| 39 | cleanPrefix() { |
| 40 | this.prefix = ``; |
| 41 | } |
| 42 | time(name: string) { |
| 43 | this._timeStamps[name] = new Date(); |
| 44 | this.logger.info(name + ': ' + this._timeStamps[name]); |
| 45 | } |
| 46 | timeLog(name: string) { |
| 47 | if (this._timeStamps[name]) { |
| 48 | const timeStamp = +new Date() - +this._timeStamps[name]; |
| 49 | const seconds = Math.floor(timeStamp / 1000); |
| 50 | const minutes = Math.floor(seconds / 60); |
| 51 | const hours = Math.floor(minutes / 60); |
| 52 | let timeStr = name + ':'; |
| 53 | if (hours >= 1) { |
| 54 | timeStr += ' ' + hours + 'h'; |
| 55 | } |
| 56 | if (minutes >= 1) { |
| 57 | timeStr += ' ' + minutes + 'm'; |
| 58 | } |
| 59 | if (seconds >= 1) { |
| 60 | timeStr += ' ' + seconds + 's'; |
| 61 | } |
| 62 | if (timeStamp >= 1) { |
| 63 | timeStr += ' ' + (timeStamp % 1000) + 'ms'; |
| 64 | } |
| 65 | this.logger.info(timeStr); |
| 66 | return; |
| 67 | } |
| 68 | this.logger.warn(name + ' is not found'); |
nothing calls this directly
no outgoing calls
no test coverage detected