Mona commited on
Commit ·
513a1ec
1
Parent(s): 43f9824
feat: add structured event-driven logger
Browse filesLevel-based logging (debug, info, warn, error) with:
- Colorized console output for headless mode
- EventEmitter for TUI consumption
- Quiet mode for terminal dashboard
- ISO timestamps and JSON-compatible metadata
Zero dependencies — pure Node.js EventEmitter.
- src/log.js +51 -0
src/log.js
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Structured logger with event emission for TUI consumption.
|
| 2 |
+
// Levels: debug < info < warn < error
|
| 3 |
+
// In headless mode, writes to stderr (keeps stdout clean for piping).
|
| 4 |
+
// In TUI mode, emits events consumed by the dashboard.
|
| 5 |
+
|
| 6 |
+
import { EventEmitter } from 'node:events';
|
| 7 |
+
|
| 8 |
+
const LEVELS = { debug: 0, info: 1, warn: 2, error: 3 };
|
| 9 |
+
const COLORS = { debug: '\x1b[90m', info: '\x1b[36m', warn: '\x1b[33m', error: '\x1b[31m' };
|
| 10 |
+
const RESET = '\x1b[0m';
|
| 11 |
+
const ICONS = { debug: '·', info: '●', warn: '⚠', error: '✗' };
|
| 12 |
+
|
| 13 |
+
class Logger extends EventEmitter {
|
| 14 |
+
#level = LEVELS.info;
|
| 15 |
+
#quiet = false;
|
| 16 |
+
|
| 17 |
+
setLevel(level) {
|
| 18 |
+
this.#level = LEVELS[level] ?? LEVELS.info;
|
| 19 |
+
}
|
| 20 |
+
|
| 21 |
+
/** Suppress console output (TUI mode — events only). */
|
| 22 |
+
quiet(on = true) {
|
| 23 |
+
this.#quiet = on;
|
| 24 |
+
}
|
| 25 |
+
|
| 26 |
+
#log(level, msg, meta) {
|
| 27 |
+
if (LEVELS[level] < this.#level) return;
|
| 28 |
+
const ts = new Date();
|
| 29 |
+
const entry = {
|
| 30 |
+
ts,
|
| 31 |
+
time: ts.toLocaleTimeString('en-GB', { hour12: false }),
|
| 32 |
+
level,
|
| 33 |
+
icon: ICONS[level],
|
| 34 |
+
msg,
|
| 35 |
+
meta,
|
| 36 |
+
};
|
| 37 |
+
this.emit('entry', entry);
|
| 38 |
+
if (!this.#quiet) {
|
| 39 |
+
const prefix = `${COLORS[level]}${ICONS[level]}${RESET}`;
|
| 40 |
+
const metaStr = meta ? ` ${COLORS.debug}${JSON.stringify(meta)}${RESET}` : '';
|
| 41 |
+
process.stderr.write(` ${entry.time} ${prefix} ${msg}${metaStr}\n`);
|
| 42 |
+
}
|
| 43 |
+
}
|
| 44 |
+
|
| 45 |
+
debug(msg, meta) { this.#log('debug', msg, meta); }
|
| 46 |
+
info(msg, meta) { this.#log('info', msg, meta); }
|
| 47 |
+
warn(msg, meta) { this.#log('warn', msg, meta); }
|
| 48 |
+
error(msg, meta) { this.#log('error', msg, meta); }
|
| 49 |
+
}
|
| 50 |
+
|
| 51 |
+
export const log = new Logger();
|