-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver-logger.js
More file actions
290 lines (249 loc) · 7.25 KB
/
Copy pathserver-logger.js
File metadata and controls
290 lines (249 loc) · 7.25 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
import fs from 'node:fs/promises';
import path from 'node:path';
/**
* Logging Framework for ConflixIQ Studio
* Provides structured logging with multiple levels: DEBUG, INFO, WARN, ERROR
* Supports both file and console output with configurable log levels
*/
// Log Levels enum
const LogLevels = {
DEBUG: 0,
INFO: 1,
WARN: 2,
ERROR: 3,
};
// ANSI color codes for console output
const Colors = {
DEBUG: '\x1b[36m', // Cyan
INFO: '\x1b[32m', // Green
WARN: '\x1b[33m', // Yellow
ERROR: '\x1b[31m', // Red
RESET: '\x1b[0m',
TIMESTAMP: '\x1b[90m', // Gray
BOLD: '\x1b[1m',
};
// Configuration
const LOG_FOLDER =
process.env.LOGS_PATH || process.env.VITE_LOG_FOLDER || path.join(process.cwd(), 'logs');
const LOG_LEVEL = process.env.LOG_LEVEL || 'INFO';
const CONSOLE_OUTPUT = process.env.LOG_CONSOLE !== 'false'; // Default: true
const FILE_OUTPUT = process.env.LOG_FILE !== 'false'; // Default: true
const MAX_LOG_SIZE = Number.parseInt(process.env.LOG_MAX_SIZE || '10485760', 10); // 10MB default
const LOG_RETENTION_DAYS = Number.parseInt(process.env.LOG_RETENTION || '7', 10);
// Parse log level from environment
let currentLogLevel = LogLevels[LOG_LEVEL.toUpperCase()] ?? LogLevels.INFO;
// Track if folder creation was attempted
let folderEnsured = false;
let folderCreationError = null;
/**
* Ensure log folder exists
*/
async function ensureLogFolder() {
if (folderEnsured) return;
try {
await fs.mkdir(LOG_FOLDER, { recursive: true });
folderEnsured = true;
} catch (err) {
folderCreationError = err;
console.error('[Logger] Failed to create log folder:', err.message);
}
}
/**
* Get current log file path (daily rotation)
*/
function getLogFilePath() {
const date = new Date();
const dateStr = date.toISOString().slice(0, 10); // YYYY-MM-DD
return path.join(LOG_FOLDER, `conflixiq-studio-${dateStr}.log`);
}
/**
* Format message parts for logging
*/
function formatMessage(parts) {
return parts
.map((p) => {
if (typeof p === 'string') return p;
if (p instanceof Error) return `${p.name}: ${p.message}`;
try {
return JSON.stringify(p);
} catch {
return String(p);
}
})
.join(' ');
}
/**
* Format log entry with timestamp and level
*/
function formatLogEntry(level, message) {
const ts = new Date().toISOString();
return `${ts} [${level.toUpperCase().padEnd(5)}] ${message}`;
}
/**
* Format log entry for console with colors
*/
function formatConsoleEntry(level, message) {
const ts = new Date().toISOString();
const color = Colors[level.toUpperCase()] || Colors.RESET;
return `${Colors.TIMESTAMP}${ts}${Colors.RESET} ${color}${Colors.BOLD}[${level.toUpperCase().padEnd(5)}]${Colors.RESET} ${message}`;
}
/**
* Check if log file needs rotation (size-based)
*/
async function checkLogRotation(filePath) {
try {
const stats = await fs.stat(filePath);
if (stats.size > MAX_LOG_SIZE) {
const timestamp = new Date().toISOString().replaceAll(/[:.]/g, '-');
const rotatedPath = `${filePath}.${timestamp}`;
await fs.rename(filePath, rotatedPath);
return true;
}
return false;
} catch (err) {
// File doesn't exist yet, no rotation needed
console.error('[Logger] Error checking log rotation:', err.message);
return false;
}
}
/**
* Clean up old log files based on retention policy
*/
async function cleanupOldLogs() {
if (!folderEnsured) return;
try {
const files = await fs.readdir(LOG_FOLDER);
const now = Date.now();
const retentionMs = LOG_RETENTION_DAYS * 24 * 60 * 60 * 1000;
for (const file of files) {
if (!file.startsWith('conflixiq-studio-')) continue;
const filePath = path.join(LOG_FOLDER, file);
try {
const stats = await fs.stat(filePath);
if (now - stats.mtimeMs > retentionMs) {
await fs.unlink(filePath);
}
} catch (err) {
// Log cleanup error but continue with other files
console.error('[Logger] Error deleting old log file:', err.message);
}
}
} catch (err) {
// Log cleanup failure but continue operation
console.error('[Logger] Error cleaning up old logs:', err.message);
}
}
/**
* Write log to file
*/
async function writeToFile(level, message) {
if (!FILE_OUTPUT) return;
try {
await ensureLogFolder();
const filePath = getLogFilePath();
// Check for rotation
await checkLogRotation(filePath);
// Write log entry
const line = `${formatLogEntry(level, message)}\n`;
await fs.appendFile(filePath, line, { encoding: 'utf8' });
// Cleanup old logs periodically (every 100 logs)
if (Math.random() < 0.01) {
await cleanupOldLogs();
}
} catch (err) {
console.error('[Logger] Failed to write log file:', err.message);
}
}
/**
* Write log to console
*/
function writeToConsole(level, message) {
if (!CONSOLE_OUTPUT) return;
const consoleMessage = formatConsoleEntry(level, message);
const method = level.toLowerCase();
// Use appropriate console method
if (console[method]) {
console[method](consoleMessage);
} else {
console.log(consoleMessage);
}
}
/**
* Main logging function
*/
async function log(level, ...parts) {
const levelNum = LogLevels[level.toUpperCase()];
// Check if log level should be output
if (levelNum < currentLogLevel) {
return;
}
const message = formatMessage(parts);
// Output to console
writeToConsole(level, message);
// Output to file
await writeToFile(level, message);
}
/**
* Synchronous version for error handling
*/
function logSync(level, ...parts) {
const levelNum = LogLevels[level.toUpperCase()];
// Check if log level should be output
if (levelNum < currentLogLevel) {
return;
}
const message = formatMessage(parts);
// Output to console only (sync)
writeToConsole(level, message);
// Note: Async file write will happen, but we don't wait for it
writeToFile(level, message).catch(console.error);
}
/**
* Set log level dynamically
*/
export function setLogLevel(level) {
const levelNum = LogLevels[level.toUpperCase()];
if (levelNum !== undefined) {
currentLogLevel = levelNum;
logSync('info', `Log level set to ${level}`);
}
}
/**
* Get current log level
*/
export function getLogLevel() {
return Object.keys(LogLevels).find((key) => LogLevels[key] === currentLogLevel);
}
/**
* Get logger statistics
*/
export function getLoggerStats() {
return {
logLevel: getLogLevel(),
logFolder: LOG_FOLDER,
folderCreated: folderEnsured,
folderError: folderCreationError?.message || null,
consoleOutput: CONSOLE_OUTPUT,
fileOutput: FILE_OUTPUT,
maxLogSize: MAX_LOG_SIZE,
retentionDays: LOG_RETENTION_DAYS,
};
}
/**
* Server Logger - Main export
*/
export const serverLogger = {
debug: (...parts) => log('DEBUG', ...parts),
info: (...parts) => log('INFO', ...parts),
warn: (...parts) => log('WARN', ...parts),
error: (...parts) => log('ERROR', ...parts),
// Synchronous versions (for critical errors during shutdown)
debugSync: (...parts) => logSync('DEBUG', ...parts),
infoSync: (...parts) => logSync('INFO', ...parts),
warnSync: (...parts) => logSync('WARN', ...parts),
errorSync: (...parts) => logSync('ERROR', ...parts),
// Utility methods
setLevel: setLogLevel,
getLevel: getLogLevel,
getStats: getLoggerStats,
};