From a117b7774dd097699f184e10f031711c4ea0a4c0 Mon Sep 17 00:00:00 2001 From: Pavlo Naumenko Date: Tue, 30 Jan 2024 19:35:15 +0200 Subject: [PATCH 1/2] fix: JOIN-39427 fix long message cropping --- packages/gcloud-logger/__tests__/Logger.ts | 21 ++++++++++++++++++++- packages/gcloud-logger/src/Logger.ts | 4 ++-- packages/gcloud-logger/src/index.ts | 4 +++- 3 files changed, 25 insertions(+), 4 deletions(-) diff --git a/packages/gcloud-logger/__tests__/Logger.ts b/packages/gcloud-logger/__tests__/Logger.ts index 893d587..1335873 100644 --- a/packages/gcloud-logger/__tests__/Logger.ts +++ b/packages/gcloud-logger/__tests__/Logger.ts @@ -1,5 +1,7 @@ import { Logger } from '../src' describe('logger', () => { + afterEach(() => jest.resetAllMocks()) + it('truncates field over maxFieldLength', () => { const stdoutSpy = jest.spyOn(process.stdout, 'write'); @@ -17,4 +19,21 @@ describe('logger', () => { const log = '{"response":{"some":"[FILTERED]"},"message":"msg","severity":"DEBUG","level":100}\n' expect(stdoutSpy).toHaveBeenCalledWith(log); }) -}) + + it('ignores provided max field length if warning', () => { + const stdoutSpy = jest.spyOn(process.stdout, 'write') + + const logger = new Logger(true, 'DEBUG', [], 5) + logger.warn('msg', { response: { some: 'longWord' } }) + const log = '{"response":{"some":"longWord"},"message":"msg","severity":"WARNING","level":400}\n' + expect(stdoutSpy).toHaveBeenCalledWith(log) + }) + + it('truncates field over maxFieldLengthForError if warning', () => { + const stdoutSpy = jest.spyOn(process.stdout, 'write') + + const logger = new Logger(true, 'DEBUG', [], 5) + logger.warn('msg', { response: { some: 'x'.repeat(8000) } }) + expect(stdoutSpy.mock.calls[0]?.[0].length).toBeLessThan(4100) + }) + }) diff --git a/packages/gcloud-logger/src/Logger.ts b/packages/gcloud-logger/src/Logger.ts index 6743d37..fd05ccd 100644 --- a/packages/gcloud-logger/src/Logger.ts +++ b/packages/gcloud-logger/src/Logger.ts @@ -38,7 +38,7 @@ enum Colors { } const errorOutputStartsFrom = LevelNumber.ERROR -const maxFieldLengthForError = 4000 +const maxFieldLengthForError = 8000 const logLevel = (level: string | undefined) => { switch (level) { @@ -168,7 +168,7 @@ export class Logger { const replaceCircular = (obj: any, alreadySeen = new WeakSet()): any => { if (typeof obj === 'string') { if (obj.length > maxLength) { - return obj.substring(0, this.maxFieldLength) + '...TRUNCATED' + return obj.substring(0, maxLength) + '...TRUNCATED' } } if (typeof obj !== 'object') { diff --git a/packages/gcloud-logger/src/index.ts b/packages/gcloud-logger/src/index.ts index fc22b0d..e0881a5 100644 --- a/packages/gcloud-logger/src/index.ts +++ b/packages/gcloud-logger/src/index.ts @@ -2,11 +2,13 @@ import { Logger } from './Logger' export { Logger, Level } from './Logger' +const DEFAULT_MAX_LENGTH = '1000' + export const logger = new Logger( process.env['NODE_ENV'] === 'production', process.env['LOG_LEVEL'], undefined, - parseInt(process.env['LOG_MAX_FIELD_LENGTH'] ?? '', 10), + parseInt(process.env['LOG_MAX_FIELD_LENGTH'] ?? DEFAULT_MAX_LENGTH, 10), ) export const reportError = (e: unknown) => { From a41003c1d14a2bd49873b5a11641bc73a4bd059a Mon Sep 17 00:00:00 2001 From: Pavlo Naumenko Date: Tue, 30 Jan 2024 19:35:59 +0200 Subject: [PATCH 2/2] change const --- packages/gcloud-logger/src/Logger.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/gcloud-logger/src/Logger.ts b/packages/gcloud-logger/src/Logger.ts index fd05ccd..10c8a0a 100644 --- a/packages/gcloud-logger/src/Logger.ts +++ b/packages/gcloud-logger/src/Logger.ts @@ -38,7 +38,7 @@ enum Colors { } const errorOutputStartsFrom = LevelNumber.ERROR -const maxFieldLengthForError = 8000 +const maxFieldLengthForError = 4000 const logLevel = (level: string | undefined) => { switch (level) {