The simplest way to enable logging is to set the CNLOGLEVEL environment variable:
CNLOGLEVEL=info node your-app.jsThis will output SDK logs at INFO level and above (info, warn, error) to the console.
NOTE: This is separate from the underlying C++ core console logger that is created with the
CBPPLOGLEVELenvironment variable. Integration of the core logger will be implemented in a future version of the SDK.
The SDK automatically checks the CNLOGLEVEL environment variable when connecting to a cluster. If set to a valid log level and no logger is explicitly provided, the SDK will create a console logger at the specified level.
The following values are accepted (case-insensitive):
trace- Most verbose, logs everythingdebug- Detailed debugging informationinfo- General informational messageswarn- Warning messages and aboveerror- Error messages only (least verbose)
Invalid values will be ignored, and the SDK will default to no logging.
The SDK uses five standard log levels in ascending order of severity:
| Level | Value | Description | When to Use |
|---|---|---|---|
| TRACE | 0 | Finest-grained debugging | Tracing code execution paths |
| DEBUG | 1 | Detailed debugging info | Debugging application issues |
| INFO | 2 | Informational messages | Tracking application progress |
| WARN | 3 | Warning messages | Potentially harmful situations |
| ERROR | 4 | Error messages | Errors allowing continued execution |
You can create a console logger programmatically using the createConsoleLogger() function.
import { connect, createConsoleLogger, LogLevel } from 'couchbase'
const cluster = await connect('couchbase://localhost', {
username: 'Administrator',
password: 'password',
logger: createConsoleLogger(LogLevel.INFO)
})const couchbase = require('couchbase')
const cluster = await couchbase.connect('couchbase://localhost', {
username: 'Administrator',
password: 'password',
logger: couchbase.createConsoleLogger(couchbase.LogLevel.INFO)
})Add a prefix to all log messages for easier identification:
import { connect, createConsoleLogger, LogLevel } from 'couchbase'
const logger = createConsoleLogger(LogLevel.DEBUG, '[Couchbase]')
const cluster = await connect('couchbase://localhost', {
username: 'Administrator',
password: 'password',
logger: logger
})
logger.info('Connection established!')Output:
[Couchbase] Connection established!The SDK uses a simple logging interface that makes it easy to integrate with popular logging libraries.
interface Logger {
trace?(message: string, ...args: any[]): void
debug?(message: string, ...args: any[]): void
info?(message: string, ...args: any[]): void
warn?(message: string, ...args: any[]): void
error?(message: string, ...args: any[]): void
}All methods are optional. The SDK safely handles loggers that only implement a subset of methods.
class CustomLogger implements Logger {
trace(msg: string, ...args: any[]): void {
console.log('[TRACE]', msg, ...args)
}
debug(msg: string, ...args: any[]): void {
console.log('[DEBUG]', msg, ...args)
}
info(msg: string, ...args: any[]): void {
console.log('[INFO]', msg, ...args)
}
warn(msg: string, ...args: any[]): void {
console.warn('[WARN]', msg, ...args)
}
error(msg: string, ...args: any[]): void {
console.error('[ERROR]', msg, ...args)
}
}
customLogger = new CustomLogger()
const cluster = await connect('couchbase://localhost', {
username: 'Administrator',
password: 'password',
logger: customLogger
})const customLogger = {
trace: (msg, ...args) => console.log('[TRACE]', msg, ...args),
debug: (msg, ...args) => console.log('[DEBUG]', msg, ...args),
info: (msg, ...args) => console.log('[INFO]', msg, ...args),
warn: (msg, ...args) => console.warn('[WARN]', msg, ...args),
error: (msg, ...args) => console.error('[ERROR]', msg, ...args)
}
const cluster = await connect('couchbase://localhost', {
username: 'Administrator',
password: 'password',
logger: customLogger
})Pino is a fast, low-overhead logging library (npm install pino). Its API naturally matches the Couchbase Logger interface.
import pino from 'pino'
import { connect } from 'couchbase'
const logger = pino({ level: 'info' })
const cluster = await connect('couchbase://localhost', {
username: 'Administrator',
password: 'password',
logger: logger
})import pino from 'pino'
import { connect } from 'couchbase'
const logger = pino({
level: 'debug',
formatters: {
level: (label) => {
return { level: label }
}
},
transport: {
target: 'pino-pretty', // need the pino-pretty package
options: {
colorize: true,
translateTime: 'SYS:standard'
}
}
})
const cluster = await connect('couchbase://localhost', {
username: 'Administrator',
password: 'password',
logger: logger
})import pino from 'pino'
import { connect } from 'couchbase'
const baseLogger = pino({ level: 'info' })
const couchbaseLogger = baseLogger.child({ component: 'couchbase' })
const cluster = await connect('couchbase://localhost', {
username: 'Administrator',
password: 'password',
logger: couchbaseLogger
})Winston is a versatile logging library with support for multiple transports (npm install winston). Its API also matches the Couchbase Logger interface.
import winston from 'winston'
import { connect } from 'couchbase'
const logger = winston.createLogger({
level: 'info',
format: winston.format.combine(
winston.format.timestamp(),
winston.format.json()
),
transports: [
new winston.transports.Console()
]
})
const cluster = await connect('couchbase://localhost', {
username: 'Administrator',
password: 'password',
logger: logger
})import winston from 'winston'
import { connect } from 'couchbase'
const logger = winston.createLogger({
level: 'debug',
format: winston.format.combine(
winston.format.timestamp(),
winston.format.errors({ stack: true }),
winston.format.json()
),
transports: [
// Write all logs to console
new winston.transports.Console({
format: winston.format.combine(
winston.format.colorize(),
winston.format.simple()
)
}),
// Write all logs to a file
new winston.transports.File({
filename: 'couchbase.log',
level: 'info'
}),
// Write error logs to a separate file
new winston.transports.File({
filename: 'couchbase-error.log',
level: 'error'
})
]
})
const cluster = await connect('couchbase://localhost', {
username: 'Administrator',
password: 'password',
logger: logger
})The debug package uses a different API pattern, but you can create a simple adapter.
import createDebug from 'debug'
import { connect, Logger } from 'couchbase'
const debugLogger = createDebug('couchbase')
// Create an adapter that implements the Logger interface
class CustomLogger implements Logger {
trace(msg: string, ...args: any[]): void {
debugLogger('[TRACE]', msg, ...args)
}
debug(msg: string, ...args: any[]): void {
debugLogger('[DEBUG]', msg, ...args)
}
info(msg: string, ...args: any[]): void {
debugLogger('[INFO]', msg, ...args)
}
warn(msg: string, ...args: any[]): void {
debugLogger('[WARN]', msg, ...args)
}
error(msg: string, ...args: any[]): void {
debugLogger('[ERROR]', msg, ...args)
}
}
const logger = new CustomLogger()
const cluster = await connect('couchbase://localhost', {
username: 'Administrator',
password: 'password',
logger: logger
})To enable debug output, set the DEBUG environment variable:
DEBUG=couchbase node your-app.jsimport createDebug from 'debug'
import { connect, Logger } from 'couchbase'
// Create separate debug instances for each log level
const debugTrace = createDebug('couchbase:trace')
const debugDebug = createDebug('couchbase:debug')
const debugInfo = createDebug('couchbase:info')
const debugWarn = createDebug('couchbase:warn')
const debugError = createDebug('couchbase:error')
class CustomLogger implements Logger {
trace(msg: string, ...args: any[]): void {
debugTrace(msg, ...args)
}
debug(msg: string, ...args: any[]): void {
debugDebug(msg, ...args)
}
info(msg: string, ...args: any[]): void {
debugInfo(msg, ...args)
}
warn(msg: string, ...args: any[]): void {
debugWarn(msg, ...args)
}
error(msg: string, ...args: any[]): void {
debugError(msg, ...args)
}
}
const logger = new CustomLogger()
const cluster = await connect('couchbase://localhost', {
username: 'Administrator',
password: 'password',
logger: logger
})Enable specific log levels with wildcards:
# Only errors and warnings
DEBUG=couchbase:error,couchbase:warn node your-app.js
# All levels
DEBUG=couchbase:* node your-app.js
# Everything except trace
DEBUG=couchbase:*,-couchbase:trace node your-app.js