-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
171 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,6 @@ | |
"filtered": "LEVEL=info ZEED=demo2 node index.js" | ||
}, | ||
"dependencies": { | ||
"zeed": "file:../.." | ||
"zeed": "../.." | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* eslint-disable no-console */ | ||
import { logCaptureConsole } from '../../../src/common' | ||
import { Logger } from '../../../src/index.browser' | ||
|
||
export function logTest() { | ||
logCaptureConsole(Logger('console')) | ||
|
||
{ | ||
const log = Logger('demo') | ||
log('Hello World') | ||
log.info('Info') | ||
log.warn('Warning') | ||
log.error('Error') | ||
} | ||
|
||
{ | ||
const log = Logger('demo2') | ||
log('Hello World') | ||
log.info('Info') | ||
log.warn('Warning') | ||
log.error('Error') | ||
|
||
log('Some binary data', new Uint8Array([1, 2, 3, 99, 100, 101])) | ||
} | ||
|
||
console.log('Hello World') | ||
console.info('Info') | ||
console.warn('Warning') | ||
console.error('Error') | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import type { LoggerInterface } from './log-base' | ||
import { LogLevelDebug, LogLevelError, LogLevelInfo, LogLevelWarn } from './log-base' | ||
|
||
let onlyOnce: boolean | undefined | ||
|
||
/** | ||
* Overrides the global console methods to capture log messages and forward them to the provided logger. | ||
* Also captures window errors and unhandled rejections and logs them using the provided logger. | ||
* | ||
* @param log - The logger to which the captured log messages will be forwarded. | ||
*/ | ||
export function logCaptureConsole(log: LoggerInterface) { | ||
if (onlyOnce) { | ||
log.error('use logCaptureConsole only once!') | ||
return | ||
} | ||
|
||
onlyOnce = true | ||
|
||
globalThis.console.log = (...args: any[]) => log.generic(LogLevelDebug, ...args) | ||
globalThis.console.debug = (...args: any[]) => log.generic(LogLevelDebug, ...args) | ||
globalThis.console.warn = (...args: any[]) => log.generic(LogLevelWarn, ...args) | ||
globalThis.console.error = (...args: any[]) => log.generic(LogLevelError, ...args) | ||
globalThis.console.info = (...args: any[]) => log.generic(LogLevelInfo, ...args) | ||
|
||
globalThis.addEventListener?.('unhandledrejection', (event: any) => { | ||
log.generic(LogLevelError, 'onUnhandledrejection', event) | ||
}) | ||
|
||
globalThis.addEventListener?.('error', (event: any) => { | ||
log.generic(LogLevelError, 'onError', event) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { getGlobalContext } from '../global' | ||
|
||
// Global logger to guarantee all submodules use the same logger instance | ||
|
||
export type OriginalConsole = Pick<Console, 'log' | 'info' | 'warn' | 'error' | 'debug' > & { console: Console } | ||
|
||
declare global { | ||
interface ZeedGlobalContext { | ||
originalConsole?: OriginalConsole | ||
} | ||
} | ||
|
||
/** | ||
* Retrieves the global console object, ensuring that it is stored in the global context. | ||
* 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 { | ||
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), | ||
} | ||
} | ||
return gcontext.originalConsole | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters