All files / packages/debug/src index.ts

100% Statements 13/13
100% Branches 2/2
100% Functions 5/5
100% Lines 13/13

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69                                            1211x     1211x       2738867x 2738867x 2738867x 364x 364x     2738867x       472264x       2266603x       488x 488x               80x                      
import type { Debugger } from 'debug'
import debug from 'debug'
 
/**
 * Debug categories used across YDB SDK
 */
export type YDBLogCategory =
	| 'auth'
	| 'coordination'
	| 'driver'
	| 'error'
	| 'grpc'
	| 'query'
	| 'retry'
	| 'topic'
	| 'tx'
 
/**
 * Centralized debug logger for YDB SDK (inspired by Playwright's DebugLogger)
 */
export class YDBDebugLogger {
	#namespace: string
	#debuggers = new Map<string, Debugger>()
 
	constructor(namespace: string) {
		this.#namespace = namespace
	}
 
	get #debug(): Debugger {
		let ns = this.#namespace
		let cachedDebugger = this.#debuggers.get(ns)
		if (!cachedDebugger) {
			cachedDebugger = debug(ns)
			this.#debuggers.set(ns, cachedDebugger)
		}
 
		return cachedDebugger
	}
 
	log(message: any, ...args: any[]): void {
		this.#debug(message, ...args)
	}
 
	get enabled() {
		return !!this.#debug.enabled
	}
 
	extend(subname: string): YDBDebugLogger {
		let newNamespace = `${this.#namespace}:${subname}`
		return new YDBDebugLogger(newNamespace)
	}
}
 
/**
 * Convenience functions for common logging patterns
 */
// Sorted for consistency and maintainability
export let loggers = {
	auth: new YDBDebugLogger('ydbjs:auth'),
	coordination: new YDBDebugLogger('ydbjs:coordination'),
	driver: new YDBDebugLogger('ydbjs:driver'),
	error: new YDBDebugLogger('ydbjs:error'),
	grpc: new YDBDebugLogger('ydbjs:grpc'),
	query: new YDBDebugLogger('ydbjs:query'),
	retry: new YDBDebugLogger('ydbjs:retry'),
	topic: new YDBDebugLogger('ydbjs:topic'),
	tx: new YDBDebugLogger('ydbjs:tx'),
}