(filePath: string)
| 40 | // we don't pay an open/close per line. Throws if the path's directory |
| 41 | // can't be created. |
| 42 | export function createFileLogger(filePath: string): FileLogger { |
| 43 | mkdirSync(dirname(filePath), { recursive: true }); |
| 44 | // O_APPEND | O_CREAT | O_WRONLY = 'a' in node's flag shorthand. |
| 45 | const fd = openSync(filePath, "a"); |
| 46 | let closed = false; |
| 47 | return { |
| 48 | write(level, args) { |
| 49 | if (closed) return; |
| 50 | const line = `${formatLogEntry(level, args)}\n`; |
| 51 | writeSync(fd, line); |
| 52 | }, |
| 53 | close() { |
| 54 | if (closed) return; |
| 55 | closed = true; |
| 56 | try { |
| 57 | closeSync(fd); |
| 58 | } catch { |
| 59 | // Already closed or filesystem went away. Either way, |
| 60 | // nothing useful to do; the process is likely exiting. |
| 61 | } |
| 62 | }, |
| 63 | }; |
| 64 | } |
| 65 | |
| 66 | // Patch console.{log, error} so each call also lands in LOG_FILE, |
| 67 | // then install handlers for uncaughtException and unhandledRejection |
no test coverage detected