I'm having trouble accessing the fileHandler
object from my logger in order to flush the buffer to the file.
This is the program I am working with:
import * as log from "https://deno.land/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5e2d2a3a1e6e70696b706e">[email protected]</a>/log/mod.ts"
import { Application } from "https://deno.land/x/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a2cdc3c9e2d4948c918c93">[email protected]</a>/mod.ts";
const app = new Application()
const port = 7001
await log.setup({
handlers:{
file: new log.handlers.FileHandler("DEBUG",{
filename: "logger.log",
formatter: lr => {
return `${lr.datetime.toISOString()} [${lr.levelName}] ${lr.msg}`
}
})
},
loggers: {
default: {
level: "DEBUG",
handlers: ["file"]
}
}
})
const logger = log.getLogger()
logger.debug("hi there")
app.use((ctx) => {
ctx.response.body = 'Hi there'
})
console.log(`listening on port ${port}`)
app.listen({ port })
The issue I'm facing is that the log message isn't being written to the file. If I remove the last line ( app.listen() ), it does write to the file because the process ends. However, if I leave it listening, the process never ends and the log buffer is never flushed.
Even when I interrupt the process with Ctrl-C, it still doesn't write to the file.
According to the documentation (https://deno.land/[email protected]/log/README.md), I can force a log flush using the flush method from FileHandler. But I'm uncertain about how to access the fileHandler object.
So, I have attempted the following:
const logger = log.getLogger()
logger.debug("hi there")
logger.handlers[0].flush()
Surprisingly, this works in JavaScript, but not in TypeScript.
In TypeScript, I get the following error:
error: TS2339 [ERROR]: Property 'flush' does not exist on type 'BaseHandler'.
logger.handlers[0].flush()