From 44e97f1ec76a37f1128f8d05c1b41a199ef3560a Mon Sep 17 00:00:00 2001 From: Chaitanya Potti Date: Thu, 4 Apr 2024 18:49:10 +0800 Subject: [PATCH] fix behavior in browser to original --- src/loglevel-sentry.ts | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/loglevel-sentry.ts b/src/loglevel-sentry.ts index 3ffa494..e7122b3 100644 --- a/src/loglevel-sentry.ts +++ b/src/loglevel-sentry.ts @@ -95,6 +95,7 @@ export default class LoglevelSentry { logger.methodFactory = (method, level, name) => { if (name) this.category = name.toString(); + const defaultMethod = defaultMethodFactory(method, level, name); const overrideDefaultMethod = (...args: unknown[]) => { let logData: Record = { timestamp: new Date(), level: method.toUpperCase(), logger: name }; @@ -103,7 +104,7 @@ export default class LoglevelSentry { } else { logData = { ...logData, message: args[0] ?? "", extra: args.length > 1 ? args.slice(1) : undefined }; } - defaultMethodFactory(method, level, name)(JSON.stringify(normalize(logData))); + if (defaultMethod) defaultMethod(JSON.stringify(normalize(logData))); }; switch (method) { @@ -112,13 +113,19 @@ export default class LoglevelSentry { const [err, otherArgs] = await LoglevelSentry.translateError(args); this.error(err, ...otherArgs); - overrideDefaultMethod(err, ...otherArgs); + // keep behavior consistent to console in browser + if (typeof window !== "undefined") { + if (defaultMethod) defaultMethod(err, ...otherArgs); + } else overrideDefaultMethod(err, ...otherArgs); }; default: return (...args: unknown[]) => { this.log(LoglevelSentry.translateLevel(method), ...args); - overrideDefaultMethod(...args); + // keep behavior consistent to console in browser + if (typeof window !== "undefined") { + if (defaultMethod) defaultMethod(...args); + } else overrideDefaultMethod(...args); }; } };