@logtape/adaptor-bunyan
@logtape/adaptor-bunyan is a LogTape adapter that forwards log records to Bunyan loggers, enabling seamless integration between LogTape-enabled libraries and applications using Bunyan for logging infrastructure.
Installation
deno add jsr:@logtape/adaptor-bunyan # for Deno
npm add @logtape/adaptor-bunyan # for npm
pnpm add @logtape/adaptor-bunyan # for pnpm
yarn add @logtape/adaptor-bunyan # for Yarn
bun add @logtape/adaptor-bunyan # for BunUsage
Bunyan does not provide a global default logger; create one with
bunyan.createLogger() and pass it to the adapter.
Using the install() function
The simplest way to integrate LogTape with Bunyan is to use the install()
function:
import bunyan from "bunyan";
import { install } from "@logtape/adaptor-bunyan";
const bunyanLogger = bunyan.createLogger({ name: "my-app" });
install(bunyanLogger);
// That's it! All LogTape logs will now be routed to your Bunyan logger
import { getLogger } from "@logtape/logtape";
const logger = getLogger("my-app");
logger.info("This will be logged through Bunyan");You can also pass configuration options:
import { install } from "@logtape/adaptor-bunyan";
install(bunyanLogger, {
category: {
position: "start",
decorator: "[]",
separator: "."
}
});Manual configuration
For full control over the Bunyan integration, configure LogTape manually:
import { configure } from "@logtape/logtape";
import { getBunyanSink } from "@logtape/adaptor-bunyan";
import bunyan from "bunyan";
const bunyanLogger = bunyan.createLogger({
name: "my-app",
level: "info"
});
await configure({
sinks: {
bunyan: getBunyanSink(bunyanLogger, {
category: {
position: "start",
decorator: "[]",
separator: "."
}
})
},
loggers: [
{ category: "my-library", sinks: ["bunyan"] }
]
});Category formatting
The adapter supports flexible category formatting options:
import { getBunyanSink } from "@logtape/adaptor-bunyan";
// Hide categories completely (default)
const sink1 = getBunyanSink(logger, { category: false });
// Use default formatting ("·" separator and ":" decorator)
const sink2 = getBunyanSink(logger, { category: true });
// Custom formatting
const sink3 = getBunyanSink(logger, {
category: {
position: "end", // "start" or "end"
decorator: "[]", // "[]", "()", "<>", "{}", ":", "-", "|", "/", ""
separator: "::" // custom separator for multi-part categories
}
});Customizing interpolated value formatting
By default the adapter renders interpolated values in the message template
with node:util.inspect() (breakLength: Infinity). Provide a
valueFormatter to override that, for example to use JSON.stringify
or a redaction-aware serializer:
import { getBunyanSink } from "@logtape/adaptor-bunyan";
const sink = getBunyanSink(logger, {
valueFormatter: (value) => JSON.stringify(value),
});Properties and Bunyan reserved fields
LogTape record.properties are passed to Bunyan as the merge-object of
the call. Bunyan automatically applies any serializers configured on
the logger to matching top-level fields. The adapter also sets the
merge-object's time field to a Date derived from record.timestamp
so the resulting Bunyan record reflects when LogTape created the record,
not when the sink call ran.
Bunyan's other reserved field names (name, hostname, pid, level,
msg, src, v) should not be used as keys in your LogTape properties.
Docs
See the API reference on JSR for further details.