From 284178350490ecdb5d0360c5c0f9be7d0378928d Mon Sep 17 00:00:00 2001 From: Hongbo Wu Date: Tue, 25 Jul 2023 23:39:03 +0800 Subject: [PATCH] limit max log entry size to 256kb --- packages/api/src/apollo.ts | 8 ++++---- packages/api/src/utils/logger.ts | 14 ++++++++++++-- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/packages/api/src/apollo.ts b/packages/api/src/apollo.ts index e286b09a9..1527fa9ce 100644 --- a/packages/api/src/apollo.ts +++ b/packages/api/src/apollo.ts @@ -39,10 +39,10 @@ const contextFunc: ContextFunction = async ({ req, res, }) => { - // logger.info(`handling gql request`, { - // query: req.body.query, - // variables: req.body.variables, - // }) + logger.info(`handling gql request`, { + query: req.body.query, + variables: req.body.variables, + }) const token = req?.cookies?.auth || req?.headers?.authorization const claims = await getClaimsByToken(token) diff --git a/packages/api/src/utils/logger.ts b/packages/api/src/utils/logger.ts index 0e89cb97f..0feb934c4 100644 --- a/packages/api/src/utils/logger.ts +++ b/packages/api/src/utils/logger.ts @@ -29,7 +29,6 @@ const googleConfigs = { level: 'info', logName: 'logger', levels: config.syslog.levels, - maxEntrySize: 256000, // 256KB } function localConfig(id: string): ConsoleTransportOptions { @@ -56,6 +55,17 @@ function localConfig(id: string): ConsoleTransportOptions { } } +class GcpLoggingTransport extends LoggingWinston { + log(info: any, callback: (err: Error | null, apiResponse?: any) => void) { + const infoString = JSON.stringify(info) + if (infoString.length > 250000) { + // max size for a log entry is 256KB + info = infoString.substring(0, 256000) + } + super.log(info, callback) + } +} + /** * Builds a logger with common options, including a transport for GCP when running in the cloud. * @param id Name of the log stream. @@ -73,7 +83,7 @@ export function buildLogger(id: string, options?: LoggerOptions): Logger { export function buildLoggerTransport(id: string): TransportStream { return env.dev.isLocal ? new transports.Console(localConfig(id)) - : new LoggingWinston({ ...googleConfigs, ...{ logName: id } }) + : new GcpLoggingTransport({ ...googleConfigs, ...{ logName: id } }) } /**