Skip to content

Commit f637c13

Browse files
committed
add a file logger util
1 parent 0e2f52e commit f637c13

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

packages/common/src/logger.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,40 @@ export const consoleLogger: SkLogger = {
3434
warn: (message: string, ...args: unknown[]) => console.warn(message, ...args), // eslint-disable-line no-console
3535
error: (message: string, ...args: unknown[]) => console.error(message, ...args), // eslint-disable-line no-console
3636
};
37+
38+
/**
39+
* File-based logger for Node.js debugging contexts
40+
* Appends logs to a file with timestamps
41+
*/
42+
export function createFileLogger(filePath: string): SkLogger {
43+
// Lazy import fs to avoid bundling issues
44+
let fs: any = null;
45+
46+
const writeLog = (level: string, message: string, ...args: unknown[]) => {
47+
if (!fs) {
48+
try {
49+
fs = require('fs');
50+
} catch (e) {
51+
// File logging not available in this context
52+
return;
53+
}
54+
}
55+
56+
const timestamp = new Date().toISOString();
57+
const argsStr = args.length > 0 ? ' ' + JSON.stringify(args) : '';
58+
const logLine = `[${timestamp}] [${level}] ${message}${argsStr}\n`;
59+
60+
try {
61+
fs.appendFileSync(filePath, logLine, 'utf8');
62+
} catch (e) {
63+
// Silently fail if we can't write to the file
64+
}
65+
};
66+
67+
return {
68+
debug: (message: string, ...args: unknown[]) => writeLog('DEBUG', message, ...args),
69+
info: (message: string, ...args: unknown[]) => writeLog('INFO', message, ...args),
70+
warn: (message: string, ...args: unknown[]) => writeLog('WARN', message, ...args),
71+
error: (message: string, ...args: unknown[]) => writeLog('ERROR', message, ...args),
72+
};
73+
}

0 commit comments

Comments
 (0)