From 686d8dc0b0b425216ee9c21ea3db1643b4bbffae Mon Sep 17 00:00:00 2001 From: Andrew Henry Date: Wed, 12 Jun 2024 13:32:55 -0400 Subject: [PATCH] fix: stringify json logging to be single line --- package-lock.json | 1 + package.json | 1 + src/utils/logger.ts | 12 +++++++++++- 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/package-lock.json b/package-lock.json index d8d1a7b..ce96221 100644 --- a/package-lock.json +++ b/package-lock.json @@ -18,6 +18,7 @@ "@trpc/client": "10.45.2", "@trpc/react-query": "10.45.2", "@trpc/server": "10.45.2", + "fast-safe-stringify": "2.1.1", "fuse.js": "7.0.0", "next": "14.2.3", "next-auth": "4.24.7", diff --git a/package.json b/package.json index 3f901e9..06ff3f2 100644 --- a/package.json +++ b/package.json @@ -28,6 +28,7 @@ "@trpc/client": "10.45.2", "@trpc/react-query": "10.45.2", "@trpc/server": "10.45.2", + "fast-safe-stringify": "2.1.1", "fuse.js": "7.0.0", "next": "14.2.3", "next-auth": "4.24.7", diff --git a/src/utils/logger.ts b/src/utils/logger.ts index 3de07cb..3e46595 100644 --- a/src/utils/logger.ts +++ b/src/utils/logger.ts @@ -1,7 +1,17 @@ /* eslint-disable @typescript-eslint/no-explicit-any */ /* eslint-disable @typescript-eslint/no-unsafe-assignment */ +import safeStringify from 'fast-safe-stringify' import { Logger } from 'tslog' +// This is a workaround for the issue with JSON.stringify and circular references +const stringify = (obj: any) => { + try { + return JSON.stringify(obj) + } catch (e) { + return safeStringify(obj) + } +} + // If you need logs during tests you can set the env var TEST_LOGGING=true const getLoggerType = () => { if (process.env.NODE_ENV === 'development') { @@ -60,7 +70,7 @@ export const logger = new Logger({ output.data = logObjWithMeta['1'] } - console.log(output) + console.log(stringify(output)) }, }, })