-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathlogger.ts
More file actions
331 lines (288 loc) · 11.2 KB
/
Copy pathlogger.ts
File metadata and controls
331 lines (288 loc) · 11.2 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
import type { LoggerConfig, LogLevel } from '@objectstack/spec/system';
import type { Logger } from '@objectstack/spec/contracts';
/**
* Universal Logger Implementation
*
* A configurable logger that works in both browser and Node.js environments.
* - Node.js: Uses Pino for high-performance structured logging
* - Browser: Simple console-based implementation
*
* Features:
* - Structured logging with multiple formats (json, text, pretty)
* - Log level filtering
* - Sensitive data redaction
* - File logging with rotation (Node.js only via Pino)
* - Browser console integration
* - Distributed tracing support (traceId, spanId)
*/
export class ObjectLogger implements Logger {
private config: Required<Omit<LoggerConfig, 'file' | 'rotation' | 'name'>> & { file?: string; rotation?: { maxSize: string; maxFiles: number }; name?: string };
private isNode: boolean;
private pinoLogger?: any; // Pino logger instance for Node.js
private pinoInstance?: any; // Base Pino instance for creating child loggers
private require?: any; // CommonJS require function for Node.js
constructor(config: Partial<LoggerConfig> = {}) {
// Detect runtime environment
this.isNode = typeof process !== 'undefined' && process.versions?.node !== undefined;
// Set defaults
this.config = {
name: config.name,
level: config.level ?? 'info',
format: config.format ?? (this.isNode ? 'json' : 'pretty'),
redact: config.redact ?? ['password', 'token', 'secret', 'key'],
sourceLocation: config.sourceLocation ?? false,
file: config.file,
rotation: config.rotation ?? {
maxSize: '10m',
maxFiles: 5
}
};
// Initialize Pino logger for Node.js
if (this.isNode) {
this.initPinoLogger();
}
}
/**
* Initialize Pino logger for Node.js
*/
private initPinoLogger() {
if (!this.isNode) return;
try {
// Create require function dynamically for Node.js (avoids bundling issues in browser)
// @ts-ignore - dynamic import of Node.js module
const { createRequire } = eval('require("module")');
this.require = createRequire(import.meta.url);
// Synchronous import for Pino using createRequire (works in ESM)
const pino = this.require('pino');
// Build Pino options
const pinoOptions: any = {
level: this.config.level,
redact: {
paths: this.config.redact,
censor: '***REDACTED***'
}
};
// Add name if provided
if (this.config.name) {
pinoOptions.name = this.config.name;
}
// Transport configuration for pretty printing or file output
const targets: any[] = [];
// Console transport
if (this.config.format === 'pretty') {
// Check if pino-pretty is available
let hasPretty = false;
try {
this.require.resolve('pino-pretty');
hasPretty = true;
} catch (e) {
// ignore
}
if (hasPretty) {
targets.push({
target: 'pino-pretty',
options: {
colorize: true,
translateTime: 'SYS:standard',
ignore: 'pid,hostname'
},
level: this.config.level
});
} else {
console.warn('[Logger] pino-pretty not found. Install it for pretty logging: pnpm add -D pino-pretty');
// Fallback to text/simple
targets.push({
target: 'pino/file',
options: { destination: 1 },
level: this.config.level
});
}
} else if (this.config.format === 'json') {
// JSON to stdout
targets.push({
target: 'pino/file',
options: { destination: 1 }, // stdout
level: this.config.level
});
} else {
// text format (simple)
targets.push({
target: 'pino/file',
options: { destination: 1 },
level: this.config.level
});
}
// File transport (if configured)
if (this.config.file) {
targets.push({
target: 'pino/file',
options: {
destination: this.config.file,
mkdir: true
},
level: this.config.level
});
}
// Create transport
if (targets.length > 0) {
pinoOptions.transport = targets.length === 1 ? targets[0] : { targets };
}
// Create Pino logger
this.pinoInstance = pino(pinoOptions);
this.pinoLogger = this.pinoInstance;
} catch (error) {
// Fallback to console if Pino is not available
console.warn('[Logger] Pino not available, falling back to console:', error);
this.pinoLogger = null;
}
}
/**
* Redact sensitive keys from context object (for browser)
*/
private redactSensitive(obj: any): any {
if (!obj || typeof obj !== 'object') return obj;
const redacted = Array.isArray(obj) ? [...obj] : { ...obj };
for (const key in redacted) {
const lowerKey = key.toLowerCase();
const shouldRedact = this.config.redact.some((pattern: string) =>
lowerKey.includes(pattern.toLowerCase())
);
if (shouldRedact) {
redacted[key] = '***REDACTED***';
} else if (typeof redacted[key] === 'object' && redacted[key] !== null) {
redacted[key] = this.redactSensitive(redacted[key]);
}
}
return redacted;
}
/**
* Format log entry for browser
*/
private formatBrowserLog(level: LogLevel, message: string, context?: Record<string, any>): string {
if (this.config.format === 'json') {
return JSON.stringify({
timestamp: new Date().toISOString(),
level,
message,
...context
});
}
if (this.config.format === 'text') {
const parts = [new Date().toISOString(), level.toUpperCase(), message];
if (context && Object.keys(context).length > 0) {
parts.push(JSON.stringify(context));
}
return parts.join(' | ');
}
// Pretty format
const levelColors: Record<LogLevel, string> = {
debug: '\x1b[36m', // Cyan
info: '\x1b[32m', // Green
warn: '\x1b[33m', // Yellow
error: '\x1b[31m', // Red
fatal: '\x1b[35m' // Magenta
};
const reset = '\x1b[0m';
const color = levelColors[level] || '';
let output = `${color}[${level.toUpperCase()}]${reset} ${message}`;
if (context && Object.keys(context).length > 0) {
output += ` ${JSON.stringify(context, null, 2)}`;
}
return output;
}
/**
* Log using browser console
*/
private logBrowser(level: LogLevel, message: string, context?: Record<string, any>, error?: Error) {
const redactedContext = context ? this.redactSensitive(context) : undefined;
const mergedContext = error ? { ...redactedContext, error: { message: error.message, stack: error.stack } } : redactedContext;
const formatted = this.formatBrowserLog(level, message, mergedContext);
const consoleMethod = level === 'debug' ? 'debug' :
level === 'info' ? 'log' :
level === 'warn' ? 'warn' :
level === 'error' || level === 'fatal' ? 'error' :
'log';
console[consoleMethod](formatted);
}
/**
* Public logging methods
*/
debug(message: string, meta?: Record<string, any>): void {
if (this.isNode && this.pinoLogger) {
this.pinoLogger.debug(meta || {}, message);
} else {
this.logBrowser('debug', message, meta);
}
}
info(message: string, meta?: Record<string, any>): void {
if (this.isNode && this.pinoLogger) {
this.pinoLogger.info(meta || {}, message);
} else {
this.logBrowser('info', message, meta);
}
}
warn(message: string, meta?: Record<string, any>): void {
if (this.isNode && this.pinoLogger) {
this.pinoLogger.warn(meta || {}, message);
} else {
this.logBrowser('warn', message, meta);
}
}
error(message: string, error?: Error, meta?: Record<string, any>): void {
if (this.isNode && this.pinoLogger) {
const errorContext = error ? { err: error, ...meta } : meta || {};
this.pinoLogger.error(errorContext, message);
} else {
this.logBrowser('error', message, meta, error);
}
}
fatal(message: string, error?: Error, meta?: Record<string, any>): void {
if (this.isNode && this.pinoLogger) {
const errorContext = error ? { err: error, ...meta } : meta || {};
this.pinoLogger.fatal(errorContext, message);
} else {
this.logBrowser('fatal', message, meta, error);
}
}
/**
* Create a child logger with additional context
* Note: Child loggers share the parent's Pino instance
*/
child(context: Record<string, any>): ObjectLogger {
const childLogger = new ObjectLogger(this.config);
// For Node.js with Pino, create a Pino child logger
if (this.isNode && this.pinoInstance) {
childLogger.pinoLogger = this.pinoInstance.child(context);
childLogger.pinoInstance = this.pinoInstance;
}
return childLogger;
}
/**
* Set trace context for distributed tracing
*/
withTrace(traceId: string, spanId?: string): ObjectLogger {
return this.child({ traceId, spanId });
}
/**
* Cleanup resources
*/
async destroy(): Promise<void> {
if (this.pinoLogger && this.pinoLogger.flush) {
await new Promise<void>((resolve) => {
this.pinoLogger.flush(() => resolve());
});
}
}
/**
* Compatibility method for console.log usage
*/
log(message: string, ...args: any[]): void {
this.info(message, args.length > 0 ? { args } : undefined);
}
}
/**
* Create a logger instance
*/
export function createLogger(config?: Partial<LoggerConfig>): ObjectLogger {
return new ObjectLogger(config);
}