From ff67df8e795d37b849f0c5df0f65959515c51764 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Tue, 4 Jul 2023 17:07:58 +0100 Subject: [PATCH] Rotate logs --- packages/backend-core/package.json | 1 + .../backend-core/src/logging/pino/logger.ts | 36 +++++++++++++------ yarn.lock | 5 +++ 3 files changed, 31 insertions(+), 11 deletions(-) diff --git a/packages/backend-core/package.json b/packages/backend-core/package.json index 4a1ed5c373..7f3c064c92 100644 --- a/packages/backend-core/package.json +++ b/packages/backend-core/package.json @@ -51,6 +51,7 @@ "pouchdb": "7.3.0", "pouchdb-find": "7.2.2", "redlock": "4.2.0", + "rotating-file-stream": "3.1.0", "sanitize-s3-objectkey": "0.0.1", "semver": "7.3.7", "tar-fs": "2.1.1", diff --git a/packages/backend-core/src/logging/pino/logger.ts b/packages/backend-core/src/logging/pino/logger.ts index 6bcbf481c5..8d3329ce8a 100644 --- a/packages/backend-core/src/logging/pino/logger.ts +++ b/packages/backend-core/src/logging/pino/logger.ts @@ -1,12 +1,17 @@ -import env from "../../environment" import pino, { LoggerOptions } from "pino" +import path from "path" +import fs from "fs" +import * as rfs from "rotating-file-stream" + +import { IdentityType } from "@budibase/types" + +import env from "../../environment" import * as context from "../../context" import * as correlation from "../correlation" -import { IdentityType } from "@budibase/types" import { LOG_CONTEXT } from "../index" -import path from "path" import { budibaseTempDir } from "../../objectStore" +import environment from "../../environment" // LOGGER @@ -25,7 +30,8 @@ if (!env.DISABLE_PINO_LOGGER) { timestamp: () => `,"timestamp":"${new Date(Date.now()).toISOString()}"`, } - if (env.isDev()) { + let outFile: rfs.RotatingFileStream | undefined + if (!env.isDev()) { pinoOptions.transport = { target: "pino-pretty", options: { @@ -33,15 +39,23 @@ if (!env.DISABLE_PINO_LOGGER) { }, } } else { - pinoOptions.transport = { - target: "pino/file", - options: { - destination: path.join(budibaseTempDir(), "pino.logs"), - }, - } + const fileName = path.join( + budibaseTempDir(), + "logs", + `${environment.SERVICE_NAME}.logs` + ) + outFile = rfs.createStream(fileName, { + size: "10M", + + teeToStdout: true, + }) + + outFile.on("rotation", () => { + fs.copyFileSync(fileName, `${fileName}.bak`) + }) } - pinoInstance = pino(pinoOptions) + pinoInstance = outFile ? pino(pinoOptions, outFile) : pino(pinoOptions) // CONSOLE OVERRIDES diff --git a/yarn.lock b/yarn.lock index 9537bb81fa..c6c6235d02 100644 --- a/yarn.lock +++ b/yarn.lock @@ -23197,6 +23197,11 @@ rollup@^3.18.0: optionalDependencies: fsevents "~2.3.2" +rotating-file-stream@3.1.0: + version "3.1.0" + resolved "https://registry.yarnpkg.com/rotating-file-stream/-/rotating-file-stream-3.1.0.tgz#6cf50e1671de82a396de6d31d39a6f2445f45fba" + integrity sha512-TkMF6cP1/QDcon9D71mjxHoflNuznNOrY5JJQfuxkKklZRmoow/lWBLNxXVjb6KcjAU8BDCV145buLgOx9Px1Q== + rrweb-cssom@^0.6.0: version "0.6.0" resolved "https://registry.yarnpkg.com/rrweb-cssom/-/rrweb-cssom-0.6.0.tgz#ed298055b97cbddcdeb278f904857629dec5e0e1"