mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-06 12:54:20 +01:00
feat(settings): add manager logs viewer (#344)
* feat(settings): add manager logs viewer with backend log capture * style(settings): polish logs viewer layout for mobile and full-height output
This commit is contained in:
1 parent
4862d9c5cb
commit
94e3e1917d
28 files changed
+2123
-39
No files matched your search
@@ -0,0 +1,252 @@
|
||||
import { describe, it, expect, beforeEach } from 'vitest'
|
||||
import { DEFAULTS } from '@opencode-manager/shared/config'
|
||||
import {
|
||||
appendManagerLogEntry,
|
||||
createProcessLogForwarder,
|
||||
readManagerLogEntries,
|
||||
resetManagerLogBuffer,
|
||||
} from './log-buffer'
|
||||
|
||||
describe('log buffer', () => {
|
||||
beforeEach(() => {
|
||||
resetManagerLogBuffer()
|
||||
})
|
||||
|
||||
it('returns an appended entry with seq 1, ISO timestamp and latestSeq 1', () => {
|
||||
appendManagerLogEntry({ level: 'info', source: 'manager', message: 'hello' })
|
||||
|
||||
const result = readManagerLogEntries({})
|
||||
expect(result.entries).toHaveLength(1)
|
||||
const entry = result.entries.at(0)
|
||||
expect(entry?.seq).toBe(1)
|
||||
expect(entry?.level).toBe('info')
|
||||
expect(entry?.source).toBe('manager')
|
||||
expect(entry?.message).toBe('hello')
|
||||
expect(() => new Date(entry?.timestamp ?? '').toISOString()).not.toThrow()
|
||||
expect(result.latestSeq).toBe(1)
|
||||
})
|
||||
|
||||
it('afterSeq returns only newer entries', () => {
|
||||
appendManagerLogEntry({ level: 'info', source: 'manager', message: 'one' })
|
||||
appendManagerLogEntry({ level: 'info', source: 'manager', message: 'two' })
|
||||
appendManagerLogEntry({ level: 'info', source: 'manager', message: 'three' })
|
||||
|
||||
const result = readManagerLogEntries({ afterSeq: 2 })
|
||||
expect(result.entries.map((entry) => entry.message)).toEqual(['three'])
|
||||
})
|
||||
|
||||
it('an afterSeq query with more than DEFAULT_PAGE_SIZE matches drains pages oldest-first without gaps', () => {
|
||||
const total = DEFAULTS.LOGS.DEFAULT_PAGE_SIZE + 2
|
||||
for (let i = 0; i < total; i++) {
|
||||
appendManagerLogEntry({ level: 'info', source: 'manager', message: `entry-${i}` })
|
||||
}
|
||||
|
||||
const firstPage = readManagerLogEntries({ afterSeq: 0 })
|
||||
expect(firstPage.entries).toHaveLength(DEFAULTS.LOGS.DEFAULT_PAGE_SIZE)
|
||||
expect(firstPage.entries[0]?.seq).toBe(1)
|
||||
expect(firstPage.entries.at(-1)?.seq).toBe(DEFAULTS.LOGS.DEFAULT_PAGE_SIZE)
|
||||
|
||||
const secondPage = readManagerLogEntries({
|
||||
afterSeq: firstPage.entries.at(-1)?.seq,
|
||||
})
|
||||
expect(secondPage.entries.map((entry) => entry.seq)).toEqual([
|
||||
DEFAULTS.LOGS.DEFAULT_PAGE_SIZE + 1,
|
||||
DEFAULTS.LOGS.DEFAULT_PAGE_SIZE + 2,
|
||||
])
|
||||
|
||||
const drainedSeqs = [...firstPage.entries, ...secondPage.entries].map((entry) => entry.seq)
|
||||
expect(drainedSeqs).toHaveLength(total)
|
||||
drainedSeqs.forEach((seq, index) => {
|
||||
expect(seq).toBe(index + 1)
|
||||
})
|
||||
})
|
||||
|
||||
it('keeps only the newest BUFFER_CAPACITY entries and reports evictions', () => {
|
||||
const total = DEFAULTS.LOGS.BUFFER_CAPACITY + 5
|
||||
for (let i = 0; i < total; i++) {
|
||||
appendManagerLogEntry({ level: 'info', source: 'manager', message: `entry-${i}` })
|
||||
}
|
||||
|
||||
const newestPage = readManagerLogEntries({ limit: DEFAULTS.LOGS.MAX_PAGE_SIZE })
|
||||
expect(newestPage.dropped).toBe(5)
|
||||
expect(newestPage.oldestSeq).toBe(6)
|
||||
expect(newestPage.latestSeq).toBe(total)
|
||||
expect(newestPage.entries.at(0)?.message).toBe(`entry-${total - DEFAULTS.LOGS.MAX_PAGE_SIZE}`)
|
||||
expect(newestPage.entries.at(-1)?.message).toBe(`entry-${total - 1}`)
|
||||
|
||||
const retainedCount = newestPage.latestSeq - newestPage.oldestSeq + 1
|
||||
expect(retainedCount).toBe(DEFAULTS.LOGS.BUFFER_CAPACITY)
|
||||
})
|
||||
|
||||
it('level filter keeps minimum severity: warn returns warn and error only', () => {
|
||||
appendManagerLogEntry({ level: 'debug', source: 'manager', message: 'debug' })
|
||||
appendManagerLogEntry({ level: 'info', source: 'manager', message: 'info' })
|
||||
appendManagerLogEntry({ level: 'warn', source: 'manager', message: 'warn' })
|
||||
appendManagerLogEntry({ level: 'error', source: 'manager', message: 'error' })
|
||||
|
||||
const result = readManagerLogEntries({ level: 'warn' })
|
||||
expect(result.entries.map((entry) => entry.level)).toEqual(['warn', 'error'])
|
||||
})
|
||||
|
||||
it('source filter excludes other sources', () => {
|
||||
appendManagerLogEntry({ level: 'info', source: 'manager', message: 'from manager' })
|
||||
appendManagerLogEntry({ level: 'info', source: 'opencode', message: 'from opencode' })
|
||||
|
||||
const result = readManagerLogEntries({ source: 'opencode' })
|
||||
expect(result.entries.map((entry) => entry.message)).toEqual(['from opencode'])
|
||||
})
|
||||
|
||||
it('limit above MAX_PAGE_SIZE is capped to MAX_PAGE_SIZE newest matches', () => {
|
||||
const total = DEFAULTS.LOGS.MAX_PAGE_SIZE + 10
|
||||
for (let i = 0; i < total; i++) {
|
||||
appendManagerLogEntry({ level: 'info', source: 'manager', message: `entry-${i}` })
|
||||
}
|
||||
|
||||
const result = readManagerLogEntries({ limit: DEFAULTS.LOGS.MAX_PAGE_SIZE * 10 })
|
||||
expect(result.entries).toHaveLength(DEFAULTS.LOGS.MAX_PAGE_SIZE)
|
||||
expect(result.entries.at(0)?.message).toBe(`entry-${total - DEFAULTS.LOGS.MAX_PAGE_SIZE}`)
|
||||
expect(result.entries.at(-1)?.message).toBe(`entry-${total - 1}`)
|
||||
})
|
||||
|
||||
it('truncates messages longer than MAX_ENTRY_LENGTH with the truncated marker', () => {
|
||||
const longMessage = 'x'.repeat(DEFAULTS.LOGS.MAX_ENTRY_LENGTH + 100)
|
||||
appendManagerLogEntry({ level: 'info', source: 'manager', message: longMessage })
|
||||
|
||||
const result = readManagerLogEntries({})
|
||||
expect(result.entries).toHaveLength(1)
|
||||
const storedMessage = result.entries.at(0)?.message
|
||||
expect(storedMessage?.endsWith(' …[truncated]')).toBe(true)
|
||||
expect(storedMessage?.length).toBe(DEFAULTS.LOGS.MAX_ENTRY_LENGTH + ' …[truncated]'.length)
|
||||
})
|
||||
})
|
||||
|
||||
describe('process log forwarder', () => {
|
||||
beforeEach(() => {
|
||||
resetManagerLogBuffer()
|
||||
})
|
||||
|
||||
it('emits one entry per complete line and nothing for a trailing partial line', () => {
|
||||
const forwarder = createProcessLogForwarder({ source: 'opencode', defaultLevel: 'info' })
|
||||
forwarder.write('first\nsecond\npartial')
|
||||
|
||||
expect(readManagerLogEntries({ source: 'opencode' }).entries.map((entry) => entry.message)).toEqual([
|
||||
'first',
|
||||
'second',
|
||||
])
|
||||
|
||||
forwarder.write('\n')
|
||||
expect(readManagerLogEntries({ source: 'opencode' }).entries.map((entry) => entry.message)).toEqual([
|
||||
'first',
|
||||
'second',
|
||||
'partial',
|
||||
])
|
||||
})
|
||||
|
||||
it('joins a line split across two writes and emits it once', () => {
|
||||
const forwarder = createProcessLogForwarder({ source: 'opencode', defaultLevel: 'info' })
|
||||
forwarder.write('hel')
|
||||
forwarder.write('lo\n')
|
||||
|
||||
expect(readManagerLogEntries({ source: 'opencode' }).entries.map((entry) => entry.message)).toEqual(['hello'])
|
||||
})
|
||||
|
||||
it('flush emits a trailing partial line', () => {
|
||||
const forwarder = createProcessLogForwarder({ source: 'opencode', defaultLevel: 'info' })
|
||||
forwarder.write('tail without newline')
|
||||
forwarder.flush()
|
||||
|
||||
expect(readManagerLogEntries({ source: 'opencode' }).entries.map((entry) => entry.message)).toEqual([
|
||||
'tail without newline',
|
||||
])
|
||||
})
|
||||
|
||||
it('skips blank and whitespace-only lines', () => {
|
||||
const forwarder = createProcessLogForwarder({ source: 'opencode', defaultLevel: 'info' })
|
||||
forwarder.write('\n \n\t\nreal line\n')
|
||||
|
||||
expect(readManagerLogEntries({ source: 'opencode' }).entries.map((entry) => entry.message)).toEqual(['real line'])
|
||||
})
|
||||
|
||||
it('preserves trailing spaces and tabs on nonblank lines', () => {
|
||||
const forwarder = createProcessLogForwarder({ source: 'opencode', defaultLevel: 'info' })
|
||||
forwarder.write('formatted value: \n\taligned diagnostic\t\n')
|
||||
|
||||
expect(readManagerLogEntries({ source: 'opencode' }).entries.map((entry) => entry.message)).toEqual([
|
||||
'formatted value: ',
|
||||
'\taligned diagnostic\t',
|
||||
])
|
||||
})
|
||||
|
||||
it('parses embedded levels and falls back to the forwarder default', () => {
|
||||
const stdoutForwarder = createProcessLogForwarder({ source: 'opencode', defaultLevel: 'info' })
|
||||
const stderrForwarder = createProcessLogForwarder({ source: 'opencode', defaultLevel: 'error' })
|
||||
|
||||
stdoutForwarder.write('ERROR boom\n')
|
||||
stderrForwarder.write('INFO fine\n')
|
||||
stderrForwarder.write('no level token here\n')
|
||||
|
||||
expect(readManagerLogEntries({ source: 'opencode' }).entries).toEqual(
|
||||
expect.arrayContaining([
|
||||
expect.objectContaining({ message: 'ERROR boom', level: 'error', source: 'opencode' }),
|
||||
expect.objectContaining({ message: 'INFO fine', level: 'info', source: 'opencode' }),
|
||||
expect.objectContaining({ message: 'no level token here', level: 'error', source: 'opencode' }),
|
||||
]),
|
||||
)
|
||||
})
|
||||
|
||||
it('carries the configured source on every entry', () => {
|
||||
const forwarder = createProcessLogForwarder({ source: 'opencode', defaultLevel: 'info' })
|
||||
forwarder.write('one\ntwo\n')
|
||||
|
||||
const result = readManagerLogEntries({ source: 'opencode' })
|
||||
expect(result.entries).toHaveLength(2)
|
||||
expect(result.entries.every((entry) => entry.source === 'opencode')).toBe(true)
|
||||
})
|
||||
|
||||
it('appends an endless unterminated line once it exceeds MAX_ENTRY_LENGTH instead of growing unbounded', () => {
|
||||
const forwarder = createProcessLogForwarder({ source: 'opencode', defaultLevel: 'info' })
|
||||
forwarder.write('a'.repeat(DEFAULTS.LOGS.MAX_ENTRY_LENGTH + 1))
|
||||
expect(readManagerLogEntries({ source: 'opencode' }).entries).toHaveLength(1)
|
||||
|
||||
forwarder.write('b'.repeat(10))
|
||||
forwarder.flush()
|
||||
|
||||
const entries = readManagerLogEntries({ source: 'opencode' }).entries
|
||||
expect(entries).toHaveLength(2)
|
||||
expect(entries[0]?.message.startsWith('a')).toBe(true)
|
||||
expect(entries[1]?.message.startsWith('b')).toBe(true)
|
||||
})
|
||||
|
||||
it('rejoins a multibyte UTF-8 character whose bytes are split across two writes', () => {
|
||||
const forwarder = createProcessLogForwarder({ source: 'opencode', defaultLevel: 'info' })
|
||||
const bytes = new TextEncoder().encode('opencode log: 😀 done\n')
|
||||
forwarder.write(bytes.slice(0, 16))
|
||||
forwarder.write(bytes.slice(16))
|
||||
|
||||
expect(readManagerLogEntries({ source: 'opencode' }).entries.map((entry) => entry.message)).toEqual([
|
||||
'opencode log: 😀 done',
|
||||
])
|
||||
})
|
||||
|
||||
it('flush finalizes a trailing incomplete UTF-8 sequence instead of losing it', () => {
|
||||
const forwarder = createProcessLogForwarder({ source: 'opencode', defaultLevel: 'info' })
|
||||
const bytes = new TextEncoder().encode('warn: café')
|
||||
forwarder.write(bytes.slice(0, bytes.length - 1))
|
||||
forwarder.flush()
|
||||
|
||||
expect(readManagerLogEntries({ source: 'opencode' }).entries.map((entry) => entry.message)).toEqual([
|
||||
'warn: caf\uFFFD',
|
||||
])
|
||||
})
|
||||
|
||||
it('returns the same instanceId on every read and ignores buffer resets', () => {
|
||||
const first = readManagerLogEntries({})
|
||||
appendManagerLogEntry({ level: 'info', source: 'manager', message: 'after reset' })
|
||||
resetManagerLogBuffer()
|
||||
appendManagerLogEntry({ level: 'info', source: 'manager', message: 'after reset' })
|
||||
const second = readManagerLogEntries({})
|
||||
|
||||
expect(first.instanceId).toMatch(/^[0-9a-f-]{36}$/i)
|
||||
expect(second.instanceId).toBe(first.instanceId)
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,176 @@
|
||||
import { randomUUID } from 'node:crypto'
|
||||
import { StringDecoder } from 'node:string_decoder'
|
||||
import { DEFAULTS } from '@opencode-manager/shared/config'
|
||||
import type {
|
||||
ManagerLogEntry,
|
||||
ManagerLogLevel,
|
||||
ManagerLogQuery,
|
||||
ManagerLogSource,
|
||||
ManagerLogsResponse,
|
||||
} from '@opencode-manager/shared/schemas'
|
||||
|
||||
const instanceId = randomUUID()
|
||||
const entries: ManagerLogEntry[] = []
|
||||
let nextSeq = 1
|
||||
let dropped = 0
|
||||
|
||||
const LEVEL_SEVERITY: Record<ManagerLogLevel, number> = {
|
||||
debug: 0,
|
||||
info: 1,
|
||||
warn: 2,
|
||||
error: 3,
|
||||
}
|
||||
|
||||
const TRUNCATION_SUFFIX = ' …[truncated]'
|
||||
|
||||
export function stringifyLogArg(value: unknown): string {
|
||||
if (value instanceof Error) {
|
||||
return `${value.name}: ${value.message}\n${value.stack ?? ''}`
|
||||
}
|
||||
if (typeof value === 'string') {
|
||||
return value
|
||||
}
|
||||
try {
|
||||
return JSON.stringify(value) ?? String(value)
|
||||
} catch {
|
||||
return String(value)
|
||||
}
|
||||
}
|
||||
|
||||
export function composeLogMessage(prefix: string, message: string, args: unknown[]): string {
|
||||
const prefixed = prefix ? `[${prefix}] ${message}` : message
|
||||
if (args.length === 0) {
|
||||
return prefixed
|
||||
}
|
||||
return [prefixed, ...args.map(stringifyLogArg)].join(' ')
|
||||
}
|
||||
|
||||
export function appendManagerLogEntry(input: {
|
||||
level: ManagerLogLevel
|
||||
source: ManagerLogSource
|
||||
message: string
|
||||
}): void {
|
||||
const message =
|
||||
input.message.length > DEFAULTS.LOGS.MAX_ENTRY_LENGTH
|
||||
? input.message.slice(0, DEFAULTS.LOGS.MAX_ENTRY_LENGTH) + TRUNCATION_SUFFIX
|
||||
: input.message
|
||||
|
||||
entries.push({
|
||||
seq: nextSeq++,
|
||||
timestamp: new Date().toISOString(),
|
||||
level: input.level,
|
||||
source: input.source,
|
||||
message,
|
||||
})
|
||||
|
||||
while (entries.length > DEFAULTS.LOGS.BUFFER_CAPACITY) {
|
||||
entries.shift()
|
||||
dropped++
|
||||
}
|
||||
}
|
||||
|
||||
const PROCESS_LOG_LEVEL_PATTERN = /^\s*\[?(DEBUG|INFO|WARN|WARNING|ERROR|FATAL)\b/i
|
||||
|
||||
export function parseProcessLogLevel(line: string, fallback: ManagerLogLevel): ManagerLogLevel {
|
||||
const match = line.match(PROCESS_LOG_LEVEL_PATTERN)
|
||||
if (!match) {
|
||||
return fallback
|
||||
}
|
||||
const token = match[1]?.toLowerCase() ?? ''
|
||||
if (token === 'warning') {
|
||||
return 'warn'
|
||||
}
|
||||
if (token === 'fatal') {
|
||||
return 'error'
|
||||
}
|
||||
if (token === 'debug' || token === 'info' || token === 'warn' || token === 'error') {
|
||||
return token
|
||||
}
|
||||
return fallback
|
||||
}
|
||||
|
||||
export function createProcessLogForwarder(options: {
|
||||
source: ManagerLogSource
|
||||
defaultLevel: ManagerLogLevel
|
||||
}): { write: (chunk: Uint8Array | string) => void; flush: () => void } {
|
||||
const decoder = new StringDecoder('utf8')
|
||||
let remainder = ''
|
||||
|
||||
const appendLine = (line: string) => {
|
||||
if (line.trimEnd().length === 0) {
|
||||
return
|
||||
}
|
||||
appendManagerLogEntry({
|
||||
source: options.source,
|
||||
level: parseProcessLogLevel(line, options.defaultLevel),
|
||||
message: line,
|
||||
})
|
||||
}
|
||||
|
||||
const appendDecodedText = (text: string) => {
|
||||
const parts = (remainder + text).split(/\r?\n/)
|
||||
remainder = parts.pop() ?? ''
|
||||
for (const part of parts) {
|
||||
appendLine(part)
|
||||
}
|
||||
if (remainder.length > DEFAULTS.LOGS.MAX_ENTRY_LENGTH) {
|
||||
appendLine(remainder)
|
||||
remainder = ''
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
write(chunk: Uint8Array | string): void {
|
||||
appendDecodedText(decoder.write(chunk))
|
||||
},
|
||||
flush(): void {
|
||||
remainder += decoder.end()
|
||||
if (remainder.length === 0) {
|
||||
return
|
||||
}
|
||||
appendLine(remainder)
|
||||
remainder = ''
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
export function readManagerLogEntries(query: ManagerLogQuery): ManagerLogsResponse {
|
||||
const afterSeq = query.afterSeq ?? 0
|
||||
const hasCursor = query.afterSeq !== undefined
|
||||
const minSeverity = query.level ? LEVEL_SEVERITY[query.level] : undefined
|
||||
|
||||
const matched = entries.filter((entry) => {
|
||||
if (entry.seq <= afterSeq) {
|
||||
return false
|
||||
}
|
||||
if (minSeverity !== undefined && LEVEL_SEVERITY[entry.level] < minSeverity) {
|
||||
return false
|
||||
}
|
||||
if (query.source && entry.source !== query.source) {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
})
|
||||
|
||||
const limit = Math.min(query.limit ?? DEFAULTS.LOGS.DEFAULT_PAGE_SIZE, DEFAULTS.LOGS.MAX_PAGE_SIZE)
|
||||
const page = hasCursor
|
||||
? matched.slice(0, limit)
|
||||
: limit < matched.length
|
||||
? matched.slice(matched.length - limit)
|
||||
: matched
|
||||
|
||||
return {
|
||||
entries: page,
|
||||
instanceId,
|
||||
latestSeq: nextSeq - 1,
|
||||
oldestSeq: entries[0]?.seq ?? 0,
|
||||
dropped,
|
||||
capacity: DEFAULTS.LOGS.BUFFER_CAPACITY,
|
||||
}
|
||||
}
|
||||
|
||||
export function resetManagerLogBuffer(): void {
|
||||
entries.length = 0
|
||||
nextSeq = 1
|
||||
dropped = 0
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
import { describe, it, expect, beforeEach, vi } from 'vitest'
|
||||
import { logger } from './logger'
|
||||
import { readManagerLogEntries, resetManagerLogBuffer } from './log-buffer'
|
||||
|
||||
type ConsoleSpy = ReturnType<typeof vi.spyOn>
|
||||
|
||||
describe('logger', () => {
|
||||
let warnSpy: ConsoleSpy
|
||||
let debugSpy: ConsoleSpy
|
||||
|
||||
beforeEach(() => {
|
||||
resetManagerLogBuffer()
|
||||
vi.spyOn(console, 'log').mockImplementation(() => {})
|
||||
warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {})
|
||||
vi.spyOn(console, 'error').mockImplementation(() => {})
|
||||
debugSpy = vi.spyOn(console, 'debug').mockImplementation(() => {})
|
||||
})
|
||||
|
||||
it('info routes into the buffer with level info and source manager', () => {
|
||||
logger.info('hello')
|
||||
|
||||
const { entries } = readManagerLogEntries({})
|
||||
expect(entries).toHaveLength(1)
|
||||
expect(entries[0]?.level).toBe('info')
|
||||
expect(entries[0]?.source).toBe('manager')
|
||||
expect(entries[0]?.message).toBe('hello')
|
||||
})
|
||||
|
||||
it('error folds an Error arg into one composed buffered entry', () => {
|
||||
logger.error('failed', new Error('boom'))
|
||||
|
||||
const { entries } = readManagerLogEntries({})
|
||||
expect(entries).toHaveLength(1)
|
||||
const message = entries[0]?.message
|
||||
expect(message).toContain('failed')
|
||||
expect(message).toContain('Error')
|
||||
expect(message).toContain('boom')
|
||||
expect(message).toContain('failed Error: boom')
|
||||
})
|
||||
|
||||
it('debug records nothing and writes nothing when ENV.LOGGING.DEBUG is false', () => {
|
||||
logger.debug('quiet')
|
||||
|
||||
expect(debugSpy).not.toHaveBeenCalled()
|
||||
const { entries, latestSeq } = readManagerLogEntries({})
|
||||
expect(entries).toHaveLength(0)
|
||||
expect(latestSeq).toBe(0)
|
||||
})
|
||||
|
||||
it('warn calls console.warn exactly once with prefixed line containing the message', () => {
|
||||
logger.warn('careful')
|
||||
|
||||
expect(warnSpy).toHaveBeenCalledTimes(1)
|
||||
const line = warnSpy.mock.calls[0]?.[0] as string
|
||||
expect(line).toContain('[WARN]')
|
||||
expect(line).toContain('careful')
|
||||
expect(line).toMatch(/^\[\d{4}-\d{2}-\d{2}T.*\] \[WARN\] careful$/)
|
||||
})
|
||||
})
|
||||
@@ -1,4 +1,5 @@
|
||||
import { ENV } from '@opencode-manager/shared/config/env'
|
||||
import { appendManagerLogEntry, composeLogMessage } from './log-buffer'
|
||||
|
||||
type LogLevel = 'info' | 'warn' | 'error' | 'debug'
|
||||
|
||||
@@ -9,27 +10,36 @@ class Logger {
|
||||
this.prefix = prefix
|
||||
}
|
||||
|
||||
private format(level: LogLevel, message: string): string {
|
||||
const timestamp = new Date().toISOString()
|
||||
const prefixStr = this.prefix ? `[${this.prefix}] ` : ''
|
||||
return `[${timestamp}] [${level.toUpperCase()}] ${prefixStr}${message}`
|
||||
private format(level: LogLevel, composed: string): string {
|
||||
return `[${new Date().toISOString()}] [${level.toUpperCase()}] ${composed}`
|
||||
}
|
||||
|
||||
private emit(
|
||||
level: LogLevel,
|
||||
write: (line: string) => void,
|
||||
message: string,
|
||||
args: unknown[],
|
||||
): void {
|
||||
const composed = composeLogMessage(this.prefix, message, args)
|
||||
write(this.format(level, composed))
|
||||
appendManagerLogEntry({ level, source: 'manager', message: composed })
|
||||
}
|
||||
|
||||
info(message: string, ...args: unknown[]): void {
|
||||
console.log(this.format('info', message), ...args)
|
||||
this.emit('info', (line) => console.log(line), message, args)
|
||||
}
|
||||
|
||||
warn(message: string, ...args: unknown[]): void {
|
||||
console.warn(this.format('warn', message), ...args)
|
||||
this.emit('warn', (line) => console.warn(line), message, args)
|
||||
}
|
||||
|
||||
error(message: string, ...args: unknown[]): void {
|
||||
console.error(this.format('error', message), ...args)
|
||||
this.emit('error', (line) => console.error(line), message, args)
|
||||
}
|
||||
|
||||
debug(message: string, ...args: unknown[]): void {
|
||||
if (ENV.LOGGING.DEBUG) {
|
||||
console.debug(this.format('debug', message), ...args)
|
||||
this.emit('debug', (line) => console.debug(line), message, args)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user