|
1 | | -import pino from 'pino'; |
2 | | - |
3 | | -// Logger Info |
4 | | -const transport = pino.transport({ |
5 | | - targets: [ |
6 | | - { |
7 | | - level: 'trace', |
8 | | - target: 'pino-pretty', |
9 | | - options: { colorize: true }, |
10 | | - }, |
11 | | - ], |
12 | | -}); |
13 | | - |
14 | | -export const poolLogger = pino(transport); |
| 1 | +interface Colors { |
| 2 | + reset: string; |
| 3 | + red: string; |
| 4 | + yellow: string; |
| 5 | + blue: string; |
| 6 | + green: string; |
| 7 | +} |
| 8 | + |
| 9 | +interface LogMetadata { |
| 10 | + timestamp: string; |
| 11 | + pid: number; |
| 12 | + level: string; |
| 13 | + message: string; |
| 14 | + [key: string]: any; |
| 15 | +} |
| 16 | + |
| 17 | +export enum LogLevel { |
| 18 | + ERROR = 0, |
| 19 | + WARN = 1, |
| 20 | + INFO = 2, |
| 21 | + DEBUG = 3, |
| 22 | +} |
| 23 | + |
| 24 | +export class Logger { |
| 25 | + constructor( |
| 26 | + private enabled: boolean = true, |
| 27 | + private logLevel: LogLevel = LogLevel.DEBUG, |
| 28 | + ) {} |
| 29 | + |
| 30 | + private readonly colors: Colors = { |
| 31 | + // Should reset ANSI color after using color: https://gist.github.com/pinksynth/209937bd424edb2bd21f7c8bf756befd |
| 32 | + reset: '\x1b[0m', |
| 33 | + red: '\x1b[31m', |
| 34 | + yellow: '\x1b[33m', |
| 35 | + blue: '\x1b[34m', |
| 36 | + green: '\x1b[32m', |
| 37 | + }; |
| 38 | + |
| 39 | + private getTimestamp(): string { |
| 40 | + const date = new Date(); |
| 41 | + const year = date.getFullYear(); |
| 42 | + const month = String(date.getMonth() + 1).padStart(2, '0'); |
| 43 | + const day = String(date.getDate()).padStart(2, '0'); |
| 44 | + const hours = String(date.getHours()).padStart(2, '0'); |
| 45 | + const minutes = String(date.getMinutes()).padStart(2, '0'); |
| 46 | + const seconds = String(date.getSeconds()).padStart(2, '0'); |
| 47 | + return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`; |
| 48 | + } |
| 49 | + private formatLog( |
| 50 | + level: string, |
| 51 | + color: keyof Colors, |
| 52 | + message: any[], |
| 53 | + extraMetadata: object = {}, |
| 54 | + ): string { |
| 55 | + const metadata: LogMetadata = { |
| 56 | + timestamp: this.getTimestamp(), |
| 57 | + pid: process.pid, |
| 58 | + level, |
| 59 | + message: message.join(' '), |
| 60 | + ...extraMetadata, |
| 61 | + }; |
| 62 | + return `[${metadata.timestamp}] ${this.colors[color]}[${level}]${this.colors.reset} (${metadata.pid}) -- ${metadata.message}`; |
| 63 | + } |
| 64 | + |
| 65 | + private log( |
| 66 | + level: LogLevel, |
| 67 | + color: keyof Colors, |
| 68 | + label: string, |
| 69 | + ...args: any[] |
| 70 | + ): void { |
| 71 | + if (this.enabled && this.logLevel >= level) { |
| 72 | + console.log(this.formatLog(label, color, args)); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + public error(...args: any[]): void { |
| 77 | + this.log(LogLevel.ERROR, 'red', 'ERROR', ...args); |
| 78 | + } |
| 79 | + |
| 80 | + public warn(...args: any[]): void { |
| 81 | + this.log(LogLevel.WARN, 'yellow', 'WARN', ...args); |
| 82 | + } |
| 83 | + |
| 84 | + public info(...args: any[]): void { |
| 85 | + this.log(LogLevel.INFO, 'green', 'INFO', ...args); |
| 86 | + } |
| 87 | + |
| 88 | + public debug(...args: any[]): void { |
| 89 | + this.log(LogLevel.DEBUG, 'blue', 'DEBUG', ...args); |
| 90 | + } |
| 91 | + |
| 92 | + public setLogLevel(level: LogLevel): void { |
| 93 | + this.logLevel = level; |
| 94 | + } |
| 95 | + |
| 96 | + public setEnabled(enabled: boolean): void { |
| 97 | + this.enabled = enabled; |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +export const logger = new Logger(true, LogLevel.DEBUG); |
0 commit comments