-
Notifications
You must be signed in to change notification settings - Fork 998
Expand file tree
/
Copy pathlogger.ts
More file actions
150 lines (130 loc) · 3.93 KB
/
logger.ts
File metadata and controls
150 lines (130 loc) · 3.93 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
import { Request, createLogger } from '@browserless.io/browserless';
export interface SessionContext {
trackingId?: string;
sessionId?: string;
path?: string;
method?: string;
[key: string]: string | undefined;
}
export class Logger {
protected _trace: (...args: unknown[]) => void;
protected _debug: (...args: unknown[]) => void;
protected _info: (...args: unknown[]) => void;
protected _warn: (...args: unknown[]) => void;
protected _error: (...args: unknown[]) => void;
protected _fatal: (...args: unknown[]) => void;
protected sessionContext: SessionContext = {};
constructor(
protected prefix: string,
protected request?: Request,
) {
const logger = createLogger(prefix);
this._trace = logger.extend('trace');
this._debug = logger.extend('debug');
this._info = logger.extend('info');
this._warn = logger.extend('warn');
this._error = logger.extend('error');
this._fatal = logger.extend('fatal');
// Initialize session context from request if available
if (request) {
this.sessionContext.path = request.parsed?.pathname;
this.sessionContext.method = request.method;
}
}
/**
* Sets session-specific context that will be included in all log messages.
* This allows downstream components to add context like trackingId, sessionId, etc.
*
* @param context - Session context to merge with existing context
*/
public setSessionContext(context: SessionContext): void {
this.sessionContext = { ...this.sessionContext, ...context };
}
/**
* Updates a specific session context value
*
* @param key - Context key to update
* @param value - Context value
*/
public setSessionValue(key: string, value: string): void {
this.sessionContext[key] = value;
}
/**
* Returns the current session context
*/
public getSessionContext(): SessionContext {
return { ...this.sessionContext };
}
/**
* Builds the context prefix that will be included in every log message.
* Format: [IP] [trackingId=xxx] [sessionId=xxx] [path] [method]
*/
protected get contextPrefix(): string {
const parts: string[] = [];
// Add IP address
if (this.request?.socket.remoteAddress) {
parts.push(`[${this.request.socket.remoteAddress}]`);
}
// Add tracking ID if available
if (this.sessionContext.trackingId) {
parts.push(`[trackingId=${this.sessionContext.trackingId}]`);
}
// Add session ID if available
if (this.sessionContext.sessionId) {
parts.push(`[sessionId=${this.sessionContext.sessionId}]`);
}
// Add method and path for request context
if (this.sessionContext.method && this.sessionContext.path) {
parts.push(`[${this.sessionContext.method} ${this.sessionContext.path}]`);
}
return parts.length > 0 ? parts.join(' ') : '';
}
public trace(...messages: unknown[]) {
const prefix = this.contextPrefix;
if (prefix) {
this._trace(prefix, ...messages);
} else {
this._trace(...messages);
}
}
public debug(...messages: unknown[]) {
const prefix = this.contextPrefix;
if (prefix) {
this._debug(prefix, ...messages);
} else {
this._debug(...messages);
}
}
public info(...messages: unknown[]) {
const prefix = this.contextPrefix;
if (prefix) {
this._info(prefix, ...messages);
} else {
this._info(...messages);
}
}
public warn(...messages: unknown[]) {
const prefix = this.contextPrefix;
if (prefix) {
this._warn(prefix, ...messages);
} else {
this._warn(...messages);
}
}
public error(...messages: unknown[]) {
const prefix = this.contextPrefix;
if (prefix) {
this._error(prefix, ...messages);
} else {
this._error(...messages);
}
}
public fatal(...messages: unknown[]) {
const prefix = this.contextPrefix;
if (prefix) {
this._fatal(prefix, ...messages);
} else {
this._fatal(...messages);
}
}
}