| 32 | } |
| 33 | |
| 34 | class RootLoggerImpl implements RootLogger { |
| 35 | private config: LoggingConfig | undefined; |
| 36 | private globalSink: RotatingFileSink | undefined; |
| 37 | private readonly sessions = new Map<string, SessionEntry>(); |
| 38 | private readonly sessionsById = new Map<string, Set<string>>(); |
| 39 | |
| 40 | isConfigured(): boolean { |
| 41 | return this.config !== undefined; |
| 42 | } |
| 43 | |
| 44 | getConfig(): LoggingConfig | undefined { |
| 45 | return this.config; |
| 46 | } |
| 47 | |
| 48 | configure(config: LoggingConfig): Promise<void> { |
| 49 | if (this.config !== undefined && sameLoggingConfig(this.config, config)) { |
| 50 | return Promise.resolve(); |
| 51 | } |
| 52 | const oldGlobalSink = this.globalSink; |
| 53 | this.config = config; |
| 54 | this.globalSink = makeGlobalSink(config); |
| 55 | return oldGlobalSink?.close() ?? Promise.resolve(); |
| 56 | } |
| 57 | |
| 58 | attachSession(input: SessionAttachInput): SessionLogHandle { |
| 59 | const existing = this.findOpenSession(input.sessionId, input.sessionDir); |
| 60 | if (existing !== undefined) { |
| 61 | existing.refCount += 1; |
| 62 | return makeHandle(existing); |
| 63 | } |
| 64 | const config = this.config; |
| 65 | if (config === undefined || config.level === 'off') { |
| 66 | return makeNoopHandle(input.sessionId); |
| 67 | } |
| 68 | const sink = new RotatingFileSink({ |
| 69 | path: join(input.sessionDir, 'logs', 'kimi-code.log'), |
| 70 | maxBytes: config.sessionMaxBytes, |
| 71 | files: config.sessionFiles, |
| 72 | }); |
| 73 | const entry: SessionEntry = { |
| 74 | logId: `session-log-${String(++nextSessionLogId)}`, |
| 75 | sessionId: input.sessionId, |
| 76 | sessionDir: input.sessionDir, |
| 77 | sink, |
| 78 | state: 'open', |
| 79 | closePromise: undefined, |
| 80 | refCount: 1, |
| 81 | }; |
| 82 | this.sessions.set(entry.logId, entry); |
| 83 | this.trackSessionId(entry); |
| 84 | return makeHandle(entry); |
| 85 | } |
| 86 | |
| 87 | async flush(): Promise<boolean> { |
| 88 | const tasks: Promise<boolean>[] = []; |
| 89 | if (this.globalSink !== undefined) tasks.push(this.globalSink.flush()); |
| 90 | for (const entry of this.sessions.values()) { |
| 91 | tasks.push(this.flushEntry(entry)); |
nothing calls this directly
no outgoing calls
no test coverage detected