From a395898d424bad0856e104478545b15dc1923f21 Mon Sep 17 00:00:00 2001 From: Dirk Holtwick Date: Thu, 7 Nov 2024 16:30:36 +0100 Subject: [PATCH 1/2] fix: console check --- src/browser/log/log-browser-factory.ts | 12 +++--------- src/browser/log/log-browser.ts | 3 +++ src/common/log/log-console-capture.ts | 6 +++++- src/common/log/log-console-original.ts | 22 ++++++++++++---------- src/common/log/log-console.ts | 3 +++ src/common/log/log-noop.ts | 13 +++++++++++++ src/node/files.spec.ts | 1 + 7 files changed, 40 insertions(+), 20 deletions(-) create mode 100644 src/common/log/log-noop.ts diff --git a/src/browser/log/log-browser-factory.ts b/src/browser/log/log-browser-factory.ts index 73b2b85..f6601ae 100644 --- a/src/browser/log/log-browser-factory.ts +++ b/src/browser/log/log-browser-factory.ts @@ -3,6 +3,7 @@ import { LogLevelAll, LogLevelDebug, LogLevelError, LogLevelFatal, LogLevelInfo, import { browserSelectColorByName } from '../../common/log/log-colors' import { getGlobalConsole } from '../../common/log/log-console-original' import { parseLogLevel, useNamespaceFilter } from '../../common/log/log-filter' +import { LoggerHandlerNoop } from '../../common/log/log-noop' import { browserSupportsColors } from './log-colors' /** @@ -20,7 +21,6 @@ export function LoggerBrowserSetupDebugFactory(opt: LogHandlerOptions = {}) { const styleDefault = `${styleFont}` const styleBold = `font-weight: 600; ${styleFont}` const useColors = browserSupportsColors() - const noop: any = () => {} // logCaptureConsole will override the console methods, so we need to get the original ones const originalConsole = getGlobalConsole() @@ -39,7 +39,7 @@ export function LoggerBrowserSetupDebugFactory(opt: LogHandlerOptions = {}) { const matches = useNamespaceFilter(filter) const level = parseLogLevel(logLevel ?? LogLevelAll) - if (matches(name) && level !== LogLevelOff) { + if (matches(name) && level !== LogLevelOff && originalConsole) { const fixedArgs: string[] = [] if (useColors) { const color = browserSelectColorByName(name) @@ -91,13 +91,7 @@ export function LoggerBrowserSetupDebugFactory(opt: LogHandlerOptions = {}) { }) } else { - log = (() => {}) as LoggerInterface - log.debug = noop - log.info = noop - log.warn = noop - log.error = noop - log.assert = noop - log.fatal = noop + log = LoggerHandlerNoop() } log.extend = (subName: string) => { diff --git a/src/browser/log/log-browser.ts b/src/browser/log/log-browser.ts index 3c9e8ca..df18bea 100644 --- a/src/browser/log/log-browser.ts +++ b/src/browser/log/log-browser.ts @@ -29,6 +29,9 @@ export function LoggerBrowserHandler(opt: LogHandlerOptions = {}): LogHandler { // logCaptureConsole will override the console methods, so we need to get the original ones const originalConsole = getGlobalConsole() + if (!originalConsole) + return () => {} + return (msg: LogMessage) => { if (!matchesLevel(msg.level)) return diff --git a/src/common/log/log-console-capture.ts b/src/common/log/log-console-capture.ts index 79fa3fc..c597a02 100644 --- a/src/common/log/log-console-capture.ts +++ b/src/common/log/log-console-capture.ts @@ -18,7 +18,11 @@ export function logCaptureConsole(log: LoggerInterface) { onlyOnce = true // Save the original console methods - getGlobalConsole() + const originalConsole = getGlobalConsole() + + if (!originalConsole) { + globalThis.console = {} as any + } globalThis.console.log = (...args: any[]) => log.debug(...args) globalThis.console.debug = (...args: any[]) => log.debug(...args) diff --git a/src/common/log/log-console-original.ts b/src/common/log/log-console-original.ts index 6f7c3e2..4a8b1a9 100644 --- a/src/common/log/log-console-original.ts +++ b/src/common/log/log-console-original.ts @@ -15,18 +15,20 @@ declare global { * If the global context does not have a reference to the original console object, it creates one. * The original console object is then bound to the global context, allowing access to its methods. */ -export function getGlobalConsole(): OriginalConsole { +export function getGlobalConsole(): OriginalConsole | undefined { const gcontext = getGlobalContext() if (gcontext.originalConsole == null) { - const originalConsole = console - gcontext.originalConsole = { - console: originalConsole, - log: originalConsole.log.bind(originalConsole), - info: originalConsole.info.bind(originalConsole), - warn: originalConsole.warn.bind(originalConsole), - error: originalConsole.error.bind(originalConsole), - debug: originalConsole.debug.bind(originalConsole), - } + const originalConsole = typeof console !== 'undefined' ? console : undefined + gcontext.originalConsole = originalConsole + ? { + console: originalConsole, + log: originalConsole.log.bind(originalConsole), + info: originalConsole.info.bind(originalConsole), + warn: originalConsole.warn.bind(originalConsole), + error: originalConsole.error.bind(originalConsole), + debug: originalConsole.debug.bind(originalConsole), + } + : undefined } return gcontext.originalConsole } diff --git a/src/common/log/log-console.ts b/src/common/log/log-console.ts index 42cab8e..18eea0e 100644 --- a/src/common/log/log-console.ts +++ b/src/common/log/log-console.ts @@ -22,6 +22,9 @@ export function LoggerConsoleHandler(opt: LogHandlerOptions = {}): LogHandler { // logCaptureConsole will override the console methods, so we need to get the original ones const originalConsole = getGlobalConsole() + if (!originalConsole) + return () => {} + return (msg: LogMessage) => { if (!matchesLevel(msg.level)) return diff --git a/src/common/log/log-noop.ts b/src/common/log/log-noop.ts new file mode 100644 index 0000000..dada4ab --- /dev/null +++ b/src/common/log/log-noop.ts @@ -0,0 +1,13 @@ +import type { LoggerInterface } from './log-base' + +export function LoggerHandlerNoop(): LoggerInterface { + const noop: any = () => {} + const log = (() => {}) as LoggerInterface + log.debug = noop + log.info = noop + log.warn = noop + log.error = noop + log.assert = noop + log.fatal = noop + return log +} diff --git a/src/node/files.spec.ts b/src/node/files.spec.ts index cc8d6bf..ee369bc 100644 --- a/src/node/files.spec.ts +++ b/src/node/files.spec.ts @@ -79,6 +79,7 @@ describe('files.spec', () => { "common/log/log-filter.ts", "common/log/log-memory.spec.ts", "common/log/log-memory.ts", + "common/log/log-noop.ts", "node/log/log-context-node.ts", "node/log/log-file.spec.ts", "node/log/log-file.ts", From ae94dcc590a55fca45cc712cc1ef17fbc504c3b1 Mon Sep 17 00:00:00 2001 From: Dirk Holtwick Date: Thu, 7 Nov 2024 16:30:39 +0100 Subject: [PATCH 2/2] 0.25.10 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 0a516ae..dc27c58 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "zeed", "type": "module", - "version": "0.25.9", + "version": "0.25.10", "description": "🌱 Simple foundation library", "author": { "name": "Dirk Holtwick",