@sanity/debug is a small debug logger for Node.js, Deno, Bun, browsers, workers, and edge runtimes. It is a fork of obug, which itself is a fork of debug.
The goal of this fork is to be compatible with all the environments/runtimes that our Sanity modules support, while maintaining a small footprint.
npm install @sanity/debugimport {createDebug, enable} from '@sanity/debug'
enable('app:*')
const debug = createDebug('app:request')
debug('received request %j', {method: 'GET', path: '/health'})Node.js, Deno, and Bun load the Node.js entry. This entry writes to stderr and supports ANSI colors.
Browsers, workers, edge runtimes, and React Server Components load the default entry. This entry does not add CSS or ANSI color codes. The color and useColors options have no effect there.
In Node.js, Deno, or Bun, set the DEBUG environment variable:
DEBUG=app:* node server.jsIn a browser, set localStorage.debug and reload the page:
localStorage.debug = 'app:*'Use * as a wildcard. Prefix a namespace with - to exclude it:
DEBUG='app:*,-app:health' node server.jsYou can also change the active namespaces at runtime:
import {disable, enable, enabled, namespaces} from '@sanity/debug'
enable('app:*')
enabled('app:request') // true
namespaces() // 'app:*'
const previous = disable()
enable(previous)import {createDebug} from '@sanity/debug'
const debug = createDebug('app')
const requestDebug = debug.extend('request')
requestDebug.namespace // 'app:request'The child logger keeps the parent logger's output and formatting options.
Pass options to createDebug() to change one logger:
import {createDebug} from '@sanity/debug'
const debug = createDebug('app:request', {
log: console.log,
humanize: (milliseconds) => `${milliseconds}ms`,
formatters: {
u: (value) => String(value).toUpperCase(),
},
})
debug.enabled = true
debug('request ID: %u', 'abc')The Node.js entry also supports these options:
useColorsenables or disables ANSI colors.colorsets the ANSI color number for the namespace.inspectOptssets options fornode:utilinspection and formatting.
The DEBUG_COLORS, DEBUG_DEPTH, DEBUG_HIDE_DATE, and DEBUG_SHOW_HIDDEN environment variables set the related Node.js inspection options.
This project includes work from obug and debug. See LICENSE for the copyright notices and MIT license.