limit max log entry size to 256kb

This commit is contained in:
Hongbo Wu 2023-07-25 23:39:03 +08:00
parent f5ae16a904
commit 2841783504
2 changed files with 16 additions and 6 deletions

View file

@ -39,10 +39,10 @@ const contextFunc: ContextFunction<ExpressContext, ResolverContext> = 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)

View file

@ -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 } })
}
/**