fix: double logging in debug mode

This commit is contained in:
Ben Allfree 2023-10-20 03:50:35 -07:00
parent 4085a73c25
commit 23bf51ebe5

View File

@ -6,9 +6,18 @@ import * as winston from 'winston'
type UnsubFunc = () => void type UnsubFunc = () => void
const loggers: { [key: string]: winston.Logger } = {} const loggers: {
[key: string]: {
info: (msg: string) => void
error: (msg: string) => void
tail: (
linesBack: number,
data: (line: winston.LogEntry) => void,
) => UnsubFunc
}
} = {}
function createOrGetLogger(instanceId: string, target: string): winston.Logger { export function InstanceLogger(instanceId: string, target: string) {
const loggerKey = `${instanceId}_${target}` const loggerKey = `${instanceId}_${target}`
if (loggers[loggerKey]) { if (loggers[loggerKey]) {
return loggers[loggerKey]! return loggers[loggerKey]!
@ -45,12 +54,6 @@ function createOrGetLogger(instanceId: string, target: string): winston.Logger {
], ],
}) })
loggers[loggerKey] = logger
return logger
}
export function InstanceLogger(instanceId: string, target: string) {
const logger = createOrGetLogger(instanceId, target)
const { error, warn } = LoggerService() const { error, warn } = LoggerService()
.create('InstanceLogger') .create('InstanceLogger')
.breadcrumb(instanceId) .breadcrumb(instanceId)
@ -103,10 +106,13 @@ export function InstanceLogger(instanceId: string, target: string) {
}, },
} }
if (PUBLIC_DEBUG) { if (PUBLIC_DEBUG) {
const { dbg } = LoggerService().create(`Logger:${instanceId}`) const { dbg } = LoggerService().create(`Logger`).breadcrumb(instanceId)
api.tail(0, dbg) api.tail(0, (entry) => {
dbg(entry.message)
})
} }
loggers[loggerKey] = api
return api return api
} }