-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogger.js
More file actions
140 lines (118 loc) · 3.44 KB
/
logger.js
File metadata and controls
140 lines (118 loc) · 3.44 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
// ==========================================
// Logger Utility - Structured Logging
// ==========================================
// This logger provides:
// - Consistent log levels (info, warn, error)
// - Request-level requestId tracking
// - Response time measurement
// - Machine-readable JSON format for production systems
// - Human-readable console output for development
class Logger {
constructor() {
this.LogLevel = {
INFO: "INFO",
WARN: "WARN",
ERROR: "ERROR",
};
}
// Generate timestamp in ISO format
getTimestamp() {
return new Date().toISOString();
}
// Format log entry with all necessary metadata
formatLog(level, message, metadata = {}) {
return {
timestamp: this.getTimestamp(),
level,
message,
...metadata,
};
}
// Print to console with color coding based on level
print(level, message, metadata = {}) {
const log = this.formatLog(level, message, metadata);
const colorCodes = {
INFO: "\x1b[36m", // Cyan
WARN: "\x1b[33m", // Yellow
ERROR: "\x1b[31m", // Red
};
const resetCode = "\x1b[0m";
const color = colorCodes[level] || "";
console.log(`${color}[${log.timestamp}] [${level}]${resetCode}`, message);
// Print metadata if it exists
if (Object.keys(metadata).length > 0) {
console.log(" Metadata:", JSON.stringify(metadata, null, 2));
}
}
// INFO level - normal operations, non-breaking events
info(message, metadata = {}) {
this.print(this.LogLevel.INFO, message, metadata);
}
// WARN level - potentially problematic situations, recoverable issues
warn(message, metadata = {}) {
this.print(this.LogLevel.WARN, message, metadata);
}
// ERROR level - failures, exceptions, system errors
error(message, metadata = {}) {
this.print(this.LogLevel.ERROR, message, metadata);
}
// Log incoming HTTP request
logRequest(req, requestId) {
const metadata = {
requestId,
method: req.method,
url: req.url,
userAgent: req.headers["user-agent"] || "unknown",
};
this.info(`Incoming Request: ${req.method} ${req.url}`, metadata);
}
// Log outgoing HTTP response with timing
logResponse(req, statusCode, responseTime, requestId) {
const metadata = {
requestId,
method: req.method,
url: req.url,
statusCode,
responseTimeMs: responseTime,
};
const level = statusCode >= 400 ? this.LogLevel.WARN : this.LogLevel.INFO;
this.print(
level,
`Response Sent: ${req.method} ${req.url} -> ${statusCode}`,
metadata,
);
}
// Log middleware progression
logMiddleware(name, status, requestId, details = {}) {
const metadata = {
requestId,
middleware: name,
status,
...details,
};
this.info(`Middleware: ${name} (${status})`, metadata);
}
// Log error with full context
logError(error, requestId, context = {}) {
const metadata = {
requestId,
errorMessage: error.message,
errorStack: error.stack,
...context,
};
this.error(`Exception Caught: ${error.message}`, metadata);
}
// Structured log for task operations
logTaskOperation(operation, taskId, status, requestId, details = {}) {
const metadata = {
requestId,
operation,
taskId,
status,
...details,
};
this.info(`Task Operation: ${operation} on ${taskId}`, metadata);
}
}
// Export singleton instance
module.exports = new Logger();