Skip to content

Commit

Permalink
Merge branch 'release/v0.25.10'
Browse files Browse the repository at this point in the history
  • Loading branch information
holtwick committed Nov 7, 2024
2 parents be05149 + ae94dcc commit 7aff870
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 21 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "zeed",
"type": "module",
"version": "0.25.9",
"version": "0.25.10",
"description": "🌱 Simple foundation library",
"author": {
"name": "Dirk Holtwick",
Expand Down
12 changes: 3 additions & 9 deletions src/browser/log/log-browser-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

/**
Expand All @@ -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()
Expand All @@ -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)
Expand Down Expand Up @@ -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) => {
Expand Down
3 changes: 3 additions & 0 deletions src/browser/log/log-browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion src/common/log/log-console-capture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
22 changes: 12 additions & 10 deletions src/common/log/log-console-original.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
3 changes: 3 additions & 0 deletions src/common/log/log-console.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions src/common/log/log-noop.ts
Original file line number Diff line number Diff line change
@@ -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
}
1 change: 1 addition & 0 deletions src/node/files.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 7aff870

Please sign in to comment.