Repository navigation
Expand file tree
/
Copy pathlogger.ts
More file actions
119 lines (106 loc) · 2.99 KB
/
Copy pathlogger.ts
File metadata and controls
119 lines (106 loc) · 2.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/**
* Cross-Platform Logger Utility
*
* Provides consistent logging across Node.js environments
* with fallback for environments without winston
*/
export enum LogLevel {
ERROR = 0,
WARN = 1,
INFO = 2,
DEBUG = 3
}
export class Logger {
private name: string;
private level: LogLevel;
private winston: any;
constructor(name: string, level: LogLevel = LogLevel.INFO) {
this.name = name;
this.level = level;
this.initializeWinston();
}
private async initializeWinston() {
try {
const winston = await import('winston');
this.winston = winston.default.createLogger({
level: this.levelToString(this.level),
format: winston.default.format.combine(
winston.default.format.timestamp(),
winston.default.format.printf(({ timestamp, level, message }) => {
return `${timestamp} [${this.name}] ${level.toUpperCase()}: ${message}`;
})
),
transports: [
new winston.default.transports.Console(),
],
});
} catch (error) {
// Fallback to console logging if winston is not available
this.winston = null;
}
}
error(message: string, ...args: any[]): void {
if (this.level >= LogLevel.ERROR) {
this.log('error', message, ...args);
}
}
warn(message: string, ...args: any[]): void {
if (this.level >= LogLevel.WARN) {
this.log('warn', message, ...args);
}
}
info(message: string, ...args: any[]): void {
if (this.level >= LogLevel.INFO) {
this.log('info', message, ...args);
}
}
debug(message: string, ...args: any[]): void {
if (this.level >= LogLevel.DEBUG) {
this.log('debug', message, ...args);
}
}
private log(level: string, message: string, ...args: any[]): void {
// Skip logging in test environment to avoid console noise
if (process.env.NODE_ENV === 'test') {
return;
}
if (this.winston) {
this.winston[level](message, ...args);
} else {
// Fallback to console
const timestamp = new Date().toISOString();
const logMessage = `${timestamp} [${this.name}] ${level.toUpperCase()}: ${message}`;
switch (level) {
4EDA
case 'error':
console.error(logMessage, ...args);
break;
case 'warn':
console.warn(logMessage, ...args);
break;
case 'info':
console.info(logMessage, ...args);
break;
case 'debug':
console.debug(logMessage, ...args);
break;
default:
console.log(logMessage, ...args);
}
}
}
private levelToString(level: LogLevel): string {
switch (level) {
case LogLevel.ERROR: return 'error';
case LogLevel.WARN: return 'warn';
case LogLevel.INFO: return 'info';
case LogLevel.DEBUG: return 'debug';
default: return 'info';
}
}
setLevel(level: LogLevel): void {
this.level = level;
if (this.winston) {
this.winston.level = this.levelToString(level);
}
}
}