-
Notifications
You must be signed in to change notification settings - Fork 371
feat(clerk-js): Introduce debugLogger #6452
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jacekradko
wants to merge
39
commits into
main
Choose a base branch
from
feat/debug-module
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,548
−29
Open
Changes from 3 commits
Commits
Show all changes
39 commits
Select commit
Hold shift + click to select a range
ab3cde4
feat(clerk-js): Introduce debugLogger
jacekradko 4931533
small fixes
jacekradko f9a6a97
Merge branch 'main' into feat/debug-module
jacekradko d24c0e8
changeset
jacekradko 0f1803d
coderabbit
jacekradko 3ba5c80
Merge branch 'main' into feat/debug-module
jacekradko 775fccb
fix build
jacekradko b6f886a
wip
jacekradko eaeacb8
safe initializations
jacekradko c3e6e52
wip
jacekradko bd86e19
pr feedback
jacekradko a7cfc1f
pr feedback
jacekradko 806688a
Merge branch 'main' into feat/debug-module
jacekradko 9c234a4
Merge branch 'main' into feat/debug-module
jacekradko 496cd4c
Integrate telemetry collector
jacekradko aedc788
correct telemetry endpoint
jacekradko 9828a80
tests
jacekradko 32ce445
wip
jacekradko 053efaf
Merge branch 'main' into feat/debug-module
jacekradko 59b998e
fix lint and test
jacekradko 7c311cc
wip
jacekradko 4bcf294
Merge branch 'main' into feat/debug-module
jacekradko 441061e
wip
jacekradko d79b5db
Merge branch 'main' into feat/debug-module
jacekradko 2170de0
refactor flush methods to avoid stacking requests
jacekradko 7604e58
remove redundant try/catch
jacekradko cfc5a05
consistent flush logic
jacekradko 2f4b36b
Resolve merge conflicts: bundlewatch thresholds, TelemetryCollector l…
jacekradko 5ff7b86
wip
jacekradko 1562ebf
Merge branch 'main' into feat/debug-module
jacekradko f532cf0
wip
jacekradko 55bda4e
Merge branch 'main' into feat/debug-module
jacekradko 243f598
pr feedback
jacekradko b25d4d4
pr feedback
jacekradko a1994c4
wip
jacekradko fa0cffd
Merge branch 'main' into feat/debug-module
jacekradko e544597
fix lint
jacekradko c7798ff
Merge branch 'main' into feat/debug-module
jacekradko 275ba94
wip
jacekradko File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,170 @@ | ||
import { DebugLogger } from './logger'; | ||
import { type CompositeLoggerOptions, CompositeTransport } from './transports/composite'; | ||
import { ConsoleTransport } from './transports/console'; | ||
import { type TelemetryLoggerOptions, TelemetryTransport } from './transports/telemetry'; | ||
import type { DebugLogFilter, DebugLogLevel } from './types'; | ||
|
||
export type * from './types'; | ||
|
||
export { ConsoleTransport, TelemetryTransport, CompositeTransport }; | ||
export type { TelemetryLoggerOptions, CompositeLoggerOptions }; | ||
|
||
/** | ||
* Options for configuring a logger with endpoint, filters, and log level | ||
*/ | ||
export interface LoggerOptions { | ||
endpoint?: string; | ||
filters?: DebugLogFilter[]; | ||
logLevel?: DebugLogLevel; | ||
} | ||
|
||
/** | ||
* Options for configuring a console logger with filters and log level | ||
*/ | ||
export interface ConsoleLoggerOptions { | ||
filters?: DebugLogFilter[]; | ||
logLevel?: DebugLogLevel; | ||
} | ||
|
||
/** | ||
* Singleton instance for managing debug logger initialization | ||
*/ | ||
class DebugLoggerManager { | ||
private static instance: DebugLoggerManager; | ||
private initialized = false; | ||
private logger: any = null; | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
private constructor() {} | ||
|
||
/** | ||
* Gets the singleton instance | ||
*/ | ||
static getInstance(): DebugLoggerManager { | ||
if (!DebugLoggerManager.instance) { | ||
DebugLoggerManager.instance = new DebugLoggerManager(); | ||
} | ||
return DebugLoggerManager.instance; | ||
} | ||
|
||
/** | ||
* Initializes the debug logger if not already initialized | ||
* @param options - Configuration options for the logger | ||
* @returns The debug logger instance | ||
*/ | ||
async initialize(options: LoggerOptions = {}): Promise<any> { | ||
if (this.initialized && this.logger) { | ||
return this.logger; | ||
} | ||
|
||
try { | ||
const { endpoint, logLevel = 'info', filters } = options; | ||
|
||
const transports = [{ transport: new ConsoleTransport() }, { transport: new TelemetryTransport(endpoint) }]; | ||
jacekradko marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
const transportInstances = transports.map(t => t.transport); | ||
const compositeTransport = new CompositeTransport(transportInstances); | ||
const logger = new DebugLogger(compositeTransport, logLevel, filters); | ||
|
||
this.logger = logger; | ||
this.initialized = true; | ||
return this.logger; | ||
} catch (error) { | ||
console.error('Failed to initialize debug module:', error); | ||
return null; | ||
} | ||
} | ||
|
||
/** | ||
* Gets the current logger instance | ||
*/ | ||
getLogger(): any { | ||
return this.logger; | ||
} | ||
|
||
/** | ||
* Checks if the debug logger is initialized | ||
*/ | ||
isInitialized(): boolean { | ||
return this.initialized; | ||
} | ||
|
||
/** | ||
* Resets the initialization state (for testing purposes) | ||
*/ | ||
reset(): void { | ||
this.initialized = false; | ||
this.logger = null; | ||
} | ||
} | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
/** | ||
* Gets or initializes the debug logger | ||
* @param options - Configuration options for the logger | ||
* @returns Promise resolving to the debug logger instance | ||
*/ | ||
export async function getDebugLogger(options: LoggerOptions = {}): Promise<any> { | ||
const manager = DebugLoggerManager.getInstance(); | ||
return manager.initialize(options); | ||
} | ||
|
||
/** | ||
* Creates a composite logger with both console and telemetry transports | ||
* @param options - Configuration options for the logger | ||
* @returns Object containing the logger and composite transport | ||
*/ | ||
export function createLogger(options: { endpoint?: string; logLevel?: DebugLogLevel; filters?: DebugLogFilter[] }) { | ||
const { endpoint, logLevel = 'info', filters } = options; | ||
|
||
return createCompositeLogger({ | ||
transports: [{ transport: new ConsoleTransport() }, { transport: new TelemetryTransport(endpoint) }], | ||
logLevel, | ||
filters, | ||
}); | ||
} | ||
|
||
/** | ||
* Creates a console-only logger | ||
* @param options - Configuration options for the console logger | ||
* @returns Object containing the logger and console transport | ||
*/ | ||
export function createConsoleLogger(options: ConsoleLoggerOptions) { | ||
const { logLevel = 'info', filters } = options; | ||
const transport = new ConsoleTransport(); | ||
const logger = new DebugLogger(transport, logLevel, filters); | ||
return { logger, transport }; | ||
} | ||
|
||
/** | ||
* Creates a telemetry-only logger | ||
* @param options - Configuration options for the telemetry logger | ||
* @returns Object containing the logger and telemetry transport | ||
*/ | ||
export function createTelemetryLogger(options: TelemetryLoggerOptions) { | ||
const { endpoint, logLevel = 'info', filters } = options; | ||
const transport = new TelemetryTransport(endpoint); | ||
const logger = new DebugLogger(transport, logLevel, filters); | ||
return { logger, transport }; | ||
} | ||
|
||
/** | ||
* Creates a composite logger with multiple transports | ||
* @param options - Configuration options for the composite logger | ||
* @returns Object containing the logger and composite transport | ||
*/ | ||
export function createCompositeLogger(options: CompositeLoggerOptions) { | ||
const { transports, logLevel = 'info', filters } = options; | ||
|
||
const transportInstances = transports.map(t => t.transport); | ||
const compositeTransport = new CompositeTransport(transportInstances); | ||
|
||
const logger = new DebugLogger(compositeTransport, logLevel, filters); | ||
return { logger, transport: compositeTransport }; | ||
} | ||
|
||
/** | ||
* @internal | ||
* Resets the debug logger manager (for testing purposes) | ||
*/ | ||
export function __internal_resetDebugLogger(): void { | ||
DebugLoggerManager.getInstance().reset(); | ||
} | ||
jacekradko marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or 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,116 @@ | ||
import type { DebugLogEntry, DebugLogFilter, DebugLogLevel, DebugTransport } from './types'; | ||
|
||
/** | ||
* Minimal debug logger interface for engineers | ||
*/ | ||
export class DebugLogger { | ||
private readonly transport: DebugTransport; | ||
private readonly logLevel: DebugLogLevel; | ||
private readonly filters?: DebugLogFilter[]; | ||
|
||
constructor(transport: DebugTransport, logLevel: DebugLogLevel = 'info', filters?: DebugLogFilter[]) { | ||
this.transport = transport; | ||
this.logLevel = logLevel; | ||
this.filters = filters; | ||
} | ||
|
||
error(message: string, context?: Record<string, unknown>, source?: string): void { | ||
this.log('error', message, context, source); | ||
} | ||
|
||
warn(message: string, context?: Record<string, unknown>, source?: string): void { | ||
this.log('warn', message, context, source); | ||
} | ||
|
||
info(message: string, context?: Record<string, unknown>, source?: string): void { | ||
this.log('info', message, context, source); | ||
} | ||
|
||
debug(message: string, context?: Record<string, unknown>, source?: string): void { | ||
this.log('debug', message, context, source); | ||
} | ||
|
||
trace(message: string, context?: Record<string, unknown>, source?: string): void { | ||
this.log('trace', message, context, source); | ||
} | ||
|
||
private log(level: DebugLogLevel, message: string, context?: Record<string, unknown>, source?: string): void { | ||
if (!this.shouldLogLevel(level)) { | ||
return; | ||
} | ||
|
||
if (!this.shouldLogFilters(level, message, source)) { | ||
return; | ||
} | ||
|
||
const entry: DebugLogEntry = { | ||
id: crypto.randomUUID(), | ||
timestamp: Date.now(), | ||
level, | ||
message, | ||
context, | ||
source, | ||
}; | ||
|
||
this.transport.send(entry).catch(err => { | ||
console.error('Failed to send log entry:', err); | ||
}); | ||
} | ||
|
||
private shouldLogLevel(level: DebugLogLevel): boolean { | ||
const levels: DebugLogLevel[] = ['error', 'warn', 'info', 'debug', 'trace']; | ||
const currentLevelIndex = levels.indexOf(this.logLevel); | ||
const messageLevelIndex = levels.indexOf(level); | ||
return messageLevelIndex <= currentLevelIndex; | ||
} | ||
|
||
private shouldLogFilters(level: DebugLogLevel, message: string, source?: string): boolean { | ||
if (!this.filters || this.filters.length === 0) { | ||
return true; | ||
} | ||
|
||
return this.filters.every(filter => { | ||
if (filter.level && filter.level !== level) { | ||
return false; | ||
} | ||
|
||
if (filter.source) { | ||
if (typeof filter.source === 'string') { | ||
if (source !== filter.source) { | ||
return false; | ||
} | ||
} else if (filter.source instanceof RegExp) { | ||
if (!source || !filter.source.test(source)) { | ||
return false; | ||
} | ||
} | ||
} | ||
|
||
if (filter.includePatterns && filter.includePatterns.length > 0) { | ||
const matchesInclude = filter.includePatterns.some(pattern => { | ||
if (typeof pattern === 'string') { | ||
return message.includes(pattern); | ||
} | ||
return pattern.test(message); | ||
}); | ||
if (!matchesInclude) { | ||
return false; | ||
} | ||
} | ||
|
||
if (filter.excludePatterns && filter.excludePatterns.length > 0) { | ||
const matchesExclude = filter.excludePatterns.some(pattern => { | ||
if (typeof pattern === 'string') { | ||
return message.includes(pattern); | ||
} | ||
return pattern.test(message); | ||
}); | ||
if (matchesExclude) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
}); | ||
} | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} |
27 changes: 27 additions & 0 deletions
27
packages/clerk-js/src/core/modules/debug/transports/composite.ts
This file contains hidden or 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,27 @@ | ||
import type { DebugLogEntry, DebugLogFilter, DebugTransport } from '../types'; | ||
|
||
export interface CompositeLoggerOptions { | ||
transports: Array<{ | ||
transport: DebugTransport; | ||
options?: Record<string, unknown>; | ||
}>; | ||
logLevel?: 'error' | 'warn' | 'info' | 'debug' | 'trace'; | ||
filters?: DebugLogFilter[]; | ||
} | ||
|
||
export class CompositeTransport implements DebugTransport { | ||
private readonly transports: DebugTransport[]; | ||
|
||
constructor(transports: DebugTransport[]) { | ||
this.transports = transports; | ||
} | ||
|
||
async send(entry: DebugLogEntry): Promise<void> { | ||
const promises = this.transports.map(transport => | ||
transport.send(entry).catch(err => { | ||
console.error('Failed to send to transport:', err); | ||
}), | ||
); | ||
await Promise.allSettled(promises); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.