@ydbjs/debug
v6.0.5
Published
Centralized debug logging for YDB JavaScript SDK
Downloads
5,808
Maintainers
Readme
@ydbjs/debug
Centralized debug logging for YDB JavaScript SDK, inspired by Playwright's debug architecture.
Features
- Centralized logging: Single logger instance with category-based organization
- Color-coded output: Different colors for different categories
- Scoped loggers: Create focused loggers for specific components
- Standard debug interface: Compatible with the popular
debugpackage - TypeScript support: Full type safety for log categories
Quick Start
Installation
npm install @ydbjs/debugBasic Usage Example
import { loggers } from '@ydbjs/debug'
// Create a topic writer with debug logging
class TopicWriter {
private dbg = loggers.topic.extend('writer')
constructor(producerId: string) {
this.dbg.log('creating writer with producer: %s', producerId)
}
async connect() {
this.dbg.log('connecting to topic service')
try {
// ... connection logic
this.dbg.log('connected successfully')
} catch (error) {
this.dbg.log('error during connection: %O', error)
throw error
}
}
write(message: Uint8Array) {
if (this.dbg.enabled) {
this.dbg.log('writing message, size: %d bytes', message.length)
}
// ... write logic
}
destroy(reason?: Error) {
this.dbg.log('writer destroyed, reason: %O', reason)
}
}Running with Debug Output
# Enable all YDB debug output
DEBUG=ydbjs:* node app.js
# Enable only topic-related debugging
DEBUG=ydbjs:topic:* node app.js
# Enable specific writer debugging
DEBUG=ydbjs:topic:writer node app.jsSample Output
ydbjs:topic:writer creating writer with producer: my-producer +0ms
ydbjs:topic:writer connecting to topic service +2ms
ydbjs:topic:writer connected successfully +45ms
ydbjs:topic:writer writing message, size: 1024 bytes +1ms
ydbjs:topic:writer writer destroyed, reason: Error: shutdown +2sUsage
Basic Usage
import { loggers } from '@ydbjs/debug'
// Use predefined category loggers
loggers.topic.log('starting topic writer for producer %s', producerId)
loggers.auth.log('refreshing token')
loggers.grpc.log('%s %s', method, status)Creating Scoped Loggers
import { ydbLogger } from '@ydbjs/debug'
// Create a logger for a specific category and subcategory
let writerLogger = ydbLogger.createLogger('topic', 'writer')
writerLogger.log('writer initialized with producer %s', producerId)
// Extend an existing logger
let authLogger = loggers.auth.extend('metadata')
authLogger.log('fetching token from metadata service')Available Categories
api- API calls and responsesauth- Authentication and token managementgrpc- gRPC client operationsdriver- Driver lifecycle and connection managementdiscovery- Service discoverysession- Session managementquery- Query executiontopic- Topic operationstx- Transaction operationsretry- Retry logicerror- Error handlingperf- Performance metricserror- Error handlingperf- Performance metrics
Environment Variables
Enable debug logging using the DEBUG environment variable:
# Enable all YDB logging
DEBUG=ydbjs:* node app.js
# Enable specific categories
DEBUG=ydbjs:topic:*,ydbjs:auth:* node app.js
# Enable specific subcategories
DEBUG=ydbjs:topic:writer node app.jsIntegration with YDB SDK
This package is designed specifically for the YDB JavaScript SDK and provides consistent logging across all SDK components:
// Different components using consistent debug categories
loggers.auth.log('token refreshed successfully')
loggers.grpc.log('POST /Ydb.Topic.StreamWrite OK')
loggers.driver.log('driver initialized with %d endpoints', 3)
loggers.session.log('created new session, active: %d', 5)Architecture
The debug system follows Playwright's centralized approach:
- Single logger instance (
YDBDebugLogger) manages all debug output - Category-based organization with predefined categories
- Color coding for visual distinction in terminal output
- Efficient caching of debug instances to avoid recreation
This approach provides better performance and consistency compared to creating individual debug instances throughout the codebase.
