From a50834127a4a065fa96f07c517dc08829bbc8c6b Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Wed, 5 Jul 2023 10:18:58 +0200 Subject: [PATCH] Use destinations instead of transports --- .../backend-core/src/logging/pino/logger.ts | 54 +++++++++++-------- 1 file changed, 31 insertions(+), 23 deletions(-) diff --git a/packages/backend-core/src/logging/pino/logger.ts b/packages/backend-core/src/logging/pino/logger.ts index 8d3329ce8a..7667bf83a9 100644 --- a/packages/backend-core/src/logging/pino/logger.ts +++ b/packages/backend-core/src/logging/pino/logger.ts @@ -12,6 +12,7 @@ import { LOG_CONTEXT } from "../index" import { budibaseTempDir } from "../../objectStore" import environment from "../../environment" +import pinoPretty from "pino-pretty" // LOGGER @@ -30,32 +31,20 @@ if (!env.DISABLE_PINO_LOGGER) { timestamp: () => `,"timestamp":"${new Date(Date.now()).toISOString()}"`, } - let outFile: rfs.RotatingFileStream | undefined - if (!env.isDev()) { - pinoOptions.transport = { - target: "pino-pretty", - options: { - singleLine: true, - }, - } - } else { - const fileName = path.join( - budibaseTempDir(), - "logs", - `${environment.SERVICE_NAME}.logs` - ) - outFile = rfs.createStream(fileName, { - size: "10M", + const destinations: pino.DestinationStream[] = [] - teeToStdout: true, - }) - - outFile.on("rotation", () => { - fs.copyFileSync(fileName, `${fileName}.bak`) - }) + if (env.isDev()) { + destinations.push(pinoPretty({ singleLine: true })) } - pinoInstance = outFile ? pino(pinoOptions, outFile) : pino(pinoOptions) + // TODO + if (true) { + destinations.push(localFileDestination()) + } + + pinoInstance = destinations.length + ? pino(pinoOptions, pino.multistream(destinations)) + : pino(pinoOptions) // CONSOLE OVERRIDES @@ -82,6 +71,25 @@ if (!env.DISABLE_PINO_LOGGER) { return typeof obj === "string" } + function localFileDestination() { + const fileName = path.join( + budibaseTempDir(), + "logs", + `${environment.SERVICE_NAME}.logs` + ) + const outFile = rfs.createStream(fileName, { + size: "10M", + + teeToStdout: true, + }) + + outFile.on("rotation", () => { + fs.copyFileSync(fileName, `${fileName}.bak`) + }) + + return outFile + } + /** * Backwards compatibility between console logging statements * and pino logging requirements.