({
level,
name,
message,
task,
}: {
level: LoggerEvent;
name: string;
message: string;
task?: Function;
})
| 38 | }; |
| 39 | |
| 40 | private log({ |
| 41 | level, |
| 42 | name, |
| 43 | message, |
| 44 | task, |
| 45 | }: { |
| 46 | level: LoggerEvent; |
| 47 | name: string; |
| 48 | message: string; |
| 49 | task?: Function; |
| 50 | }) { |
| 51 | // test if this level is enabled or not |
| 52 | if (levels[this.level] > levels[level]) { |
| 53 | return; // do nothing |
| 54 | } |
| 55 | |
| 56 | // format |
| 57 | let text = message; |
| 58 | if (level === 'warn') text = colors.yellow(text); |
| 59 | if (level === 'error') text = colors.red(text); |
| 60 | const time = new Date(); |
| 61 | const log = `${colors.dim( |
| 62 | `[${String(time.getHours() + 1).padStart(2, '0')}:${String(time.getMinutes() + 1).padStart( |
| 63 | 2, |
| 64 | '0', |
| 65 | )}:${String(time.getSeconds()).padStart(2, '0')}]`, |
| 66 | )} ${colors.dim(`[${name}]`)} ${text}`; |
| 67 | |
| 68 | // add to log history and remove old logs to keep memory low |
| 69 | const lastHistoryItem = this.history[this.history.length - 1]; |
| 70 | if (lastHistoryItem && lastHistoryItem.val === log) { |
| 71 | lastHistoryItem.count++; |
| 72 | } else { |
| 73 | this.history.push({val: log, count: 1}); |
| 74 | } |
| 75 | while (this.history.length > this.logCount) { |
| 76 | this.history.shift(); |
| 77 | } |
| 78 | |
| 79 | // log |
| 80 | if (typeof this.callbacks[level] === 'function') { |
| 81 | this.callbacks[level](log); |
| 82 | } else { |
| 83 | throw new Error(`No logging method defined for ${level}`); |
| 84 | } |
| 85 | |
| 86 | // logger takes a possibly processor-intensive task, and only |
| 87 | // processes it when this log level is enabled |
| 88 | task && task(this); |
| 89 | } |
| 90 | |
| 91 | /** emit messages only visible when --debug is passed */ |
| 92 | public debug(message: string, options?: LoggerOptions): void { |
no outgoing calls
no test coverage detected