hotfix: logger tailing errors

This commit is contained in:
Ben Allfree 2023-10-19 15:47:44 -07:00
parent 08ae50e93f
commit d1c25e0d47

View File

@ -1,5 +1,5 @@
import { mkInstanceDataPath, PUBLIC_DEBUG } from '$constants' import { mkInstanceDataPath, PUBLIC_DEBUG } from '$constants'
import { LoggerService } from '@pockethost/common' import { createCleanupManager, LoggerService } from '@pockethost/common'
import * as fs from 'fs' import * as fs from 'fs'
import { Tail } from 'tail' import { Tail } from 'tail'
import * as winston from 'winston' import * as winston from 'winston'
@ -51,6 +51,10 @@ function createOrGetLogger(instanceId: string, target: string): winston.Logger {
export function InstanceLogger(instanceId: string, target: string) { export function InstanceLogger(instanceId: string, target: string) {
const logger = createOrGetLogger(instanceId, target) const logger = createOrGetLogger(instanceId, target)
const { error, warn } = LoggerService()
.create('InstanceLogger')
.breadcrumb(instanceId)
.breadcrumb(target)
const api = { const api = {
info: (msg: string) => { info: (msg: string) => {
@ -65,16 +69,36 @@ export function InstanceLogger(instanceId: string, target: string) {
): UnsubFunc => { ): UnsubFunc => {
const logFile = mkInstanceDataPath(instanceId, `logs`, `${target}.log`) const logFile = mkInstanceDataPath(instanceId, `logs`, `${target}.log`)
const cm = createCleanupManager()
let tid: any
cm.add(() => clearTimeout(tid))
const check = () => {
try {
const tail = new Tail(logFile, { nLines: linesBack }) const tail = new Tail(logFile, { nLines: linesBack })
tail.on('line', (line) => { tail.on('line', (line) => {
try {
const entry = JSON.parse(line) const entry = JSON.parse(line)
data(entry) data(entry)
} catch (e) {
data({
level: 'info',
message: line,
time: new Date().toISOString(),
})
}
}) })
// Return an unsubscribe function to remove the listener when done cm.add(() => tail.unwatch())
} catch (e) {
warn(e)
tid = setTimeout(check, 1000)
}
}
check()
return () => { return () => {
tail.unwatch() cm.shutdown()
} }
}, },
} }