Skip to content

Commit

Permalink
Merge pull request #101 from join-com/JOIN-39427
Browse files Browse the repository at this point in the history
fix: JOIN-39427 fix long message cropping
  • Loading branch information
pavnaumenko authored Jan 30, 2024
2 parents efdcc74 + a41003c commit 72d1027
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
21 changes: 20 additions & 1 deletion packages/gcloud-logger/__tests__/Logger.ts
Original file line number Diff line number Diff line change
@@ -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');

Expand All @@ -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)
})
})
2 changes: 1 addition & 1 deletion packages/gcloud-logger/src/Logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand Down
4 changes: 3 additions & 1 deletion packages/gcloud-logger/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down

0 comments on commit 72d1027

Please sign in to comment.