npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@eserstack/logging

v5.1.0

Published

Readme

📝 @eserstack/logging

Note: JSR is frozen at 4.5.x. From 5.0.0, @eserstack/logging is published on npm under the same name (pnpm add @eserstack/logging); the JSR versions stay available but get no updates.

eserstack Library — eser/stack on GitHub Install: pnpm add @eserstack/logging

@eserstack/logging is a hierarchical, category-based logging library with context propagation, multiple sinks, filters, and OpenTelemetry integration. It is inspired by logtape and runs in TypeScript and JavaScript applications.

Sinks integrate with @eserstack/streams for rendering — choose ANSI, Markdown, or plain text output by plugging in the appropriate renderer.

Features

  • Hierarchical Categories - Organize loggers in a tree structure like ["app", "http", "request"]
  • Context Propagation - Automatically include request IDs, trace IDs, and other context in logs
  • Multiple Sinks - Route logs to stdout, writable streams, files, or custom destinations via @eserstack/streams
  • Filters - Filter logs by severity, category, properties, or custom logic
  • OpenTelemetry Integration - Automatic correlation with distributed traces
  • OpenTelemetry Severity Levels - 9 standard severity levels (Trace to Emergency)
  • Lazy Evaluation - Expensive log messages only computed when needed
  • Span-based Formatting - Structured Span[] output that adapts to any renderer (ANSI, Markdown, plain)
  • Namespace Exports - Clean API with logging.config, logging.sinks, etc.
  • TypeScript First - Full type safety with proper type definitions

Quick Start

import * as logging from "@eserstack/logging";
import * as streams from "@eserstack/streams";

// Create an Output with a renderer and sink
const out = streams.output({
  renderer: streams.renderers.ansi(),
  sink: streams.sinks.stdout(),
});

// Configure once at app startup
await logging.config.configure({
  sinks: {
    console: logging.sinks.getOutputSink(out),
  },
  loggers: [
    {
      category: ["myapp"],
      lowestLevel: logging.Severities.Debug,
      sinks: ["console"],
    },
  ],
});

// Get a logger by category
const logger = logging.logger.getLogger(["myapp", "http"]);
await logger.info("Server started on port 3000");

// Create child loggers
const requestLogger = logger.getChild("request");
await requestLogger.debug("Processing request");

Severity Levels (OpenTelemetry)

| Level | Value | Description | | --------- | ----- | ---------------------------------- | | Trace | 1 | Most fine-grained diagnostic info | | Debug | 5 | Detailed troubleshooting data | | Info | 9 | Normal operational messages | | Notice | 10 | Normal but significant condition | | Warning | 13 | Potential issues needing attention | | Error | 17 | Functionality-breaking problems | | Critical | 21 | Non-recoverable critical failures | | Alert | 22 | Action must be taken immediately | | Emergency | 23 | System is unusable (most severe) |

Higher severity numbers indicate more severe conditions (OpenTelemetry model).

Usage Examples

Hierarchical Categories

import * as logging from "@eserstack/logging";
import * as streams from "@eserstack/streams";

const out = streams.output({
  renderer: streams.renderers.ansi(),
  sink: streams.sinks.stdout(),
});

await logging.config.configure({
  sinks: { console: logging.sinks.getOutputSink(out) },
  loggers: [
    {
      category: ["app"],
      sinks: ["console"],
      lowestLevel: logging.Severities.Info,
    },
    { category: ["app", "db"], lowestLevel: logging.Severities.Debug }, // inherits console sink
  ],
});

const appLogger = logging.logger.getLogger(["app"]);
const dbLogger = logging.logger.getLogger(["app", "db"]);
const queryLogger = dbLogger.getChild("query");

await appLogger.info("Application started");
await dbLogger.debug("Database connected");
await queryLogger.debug("Executing query: SELECT * FROM users");

Context Propagation

import * as logging from "@eserstack/logging";
import * as streams from "@eserstack/streams";

const out = streams.output({
  renderer: streams.renderers.ansi(),
  sink: streams.sinks.stdout(),
});

await logging.config.configure({
  sinks: { console: logging.sinks.getOutputSink(out) },
  loggers: [
    {
      category: ["app"],
      sinks: ["console"],
      lowestLevel: logging.Severities.Debug,
    },
  ],
});

// Stands in for your own request handling
const processRequest = (_req: Request): Promise<void> => Promise.resolve();

async function handleRequest(req: Request) {
  // All logs within this callback include requestId automatically
  await logging.context.withContext(
    { requestId: req.headers.get("x-request-id") },
    async () => {
      const logger = logging.logger.getLogger(["app", "handler"]);
      await logger.info("Processing request");
      await processRequest(req);
      await logger.info("Request completed");
    },
  );
}

Logger with Properties

import * as logging from "@eserstack/logging";

const logger = logging.logger.getLogger(["app"]);

// Create a logger with preset properties
const userLogger = logger.with({ userId: 123, role: "admin" });

await userLogger.info("User action"); // Includes userId and role
await userLogger.warn("Permission denied");

Logging Errors

Pass an Error as a property instead of formatting it into the message. The record carries its name, message, stack, its own fields (such as a code) and every cause below it, on the Go backend and on the console fallback alike. An Error among the extra arguments is written the same way.

import * as logging from "@eserstack/logging";

const logger = logging.logger.getLogger(["app", "build"]);

const error = new Error("bundle client entry", {
  cause: new TypeError("module ./app.tsx not found"),
});

// Includes error.name, error.message, error.stack and error.cause
await logger.with({ error }).warn("Client bundle skipped");

Multiple Sinks

Each sink can render records differently. Here the terminal gets ANSI colors, and audit records also go as plain text to a file through a writable stream that appends each rendered line to audit.log.

import * as logging from "@eserstack/logging";
import * as streams from "@eserstack/streams";
import { runtime } from "@eserstack/standards/cross-runtime";

// ANSI-colored output for the terminal
const terminalOut = streams.output({
  renderer: streams.renderers.ansi(),
  sink: streams.sinks.stdout(),
});

// Appends each rendered chunk to audit.log, one write at a time
const fileStream = new WritableStream<streams.Chunk>({
  write: (chunk) =>
    runtime.fs.writeTextFile("audit.log", String(chunk.data), { append: true }),
});

// Plain-text output routed to a writable stream (e.g. a file)
const fileOut = streams.output({
  renderer: streams.renderers.plain(),
  sink: streams.sinks.writable(fileStream),
});

await logging.config.configure({
  sinks: {
    console: logging.sinks.getOutputSink(terminalOut),
    file: logging.sinks.getOutputSink(fileOut),
  },
  loggers: [
    {
      category: ["app"],
      sinks: ["console"],
      lowestLevel: logging.Severities.Debug,
    },
    {
      category: ["app", "audit"],
      sinks: ["console", "file"],
      lowestLevel: logging.Severities.Info,
    },
  ],
});

Filters

import * as logging from "@eserstack/logging";
import * as streams from "@eserstack/streams";

const out = streams.output({
  renderer: streams.renderers.ansi(),
  sink: streams.sinks.stdout(),
});

await logging.config.configure({
  sinks: { console: logging.sinks.getOutputSink(out) },
  filters: {
    production: logging.filters.getLevelFilter(logging.Severities.Warning),
    httpOnly: logging.filters.getCategoryFilter(["app", "http"]),
  },
  loggers: [{ category: ["app"], sinks: ["console"], filters: ["production"] }],
});

OpenTelemetry Integration

import * as logging from "@eserstack/logging";

const orderId = "your_order_id";
// Stands in for your own order handling
const processOrder = (_id: string): Promise<void> => Promise.resolve();

// Logs automatically include traceId and spanId
await logging.tracer.withSpan("process-order", async (span) => {
  const logger = logging.logger.getLogger(["app", "orders"]);
  span.setAttribute("order.id", orderId);

  await logger.info("Processing order");
  await processOrder(orderId);
  await logger.info("Order completed");
});

Lazy Evaluation

import * as logging from "@eserstack/logging";

const logger = logging.logger.getLogger(["app"]);

// Stands in for work you only want to do when debug logs are on
const computeExpensiveDebugInfo = () => ({ cacheEntries: 1024 });

// Function only called if debug level is enabled
await logger.debug(() => {
  const expensiveData = computeExpensiveDebugInfo();
  return `Debug data: ${JSON.stringify(expensiveData)}`;
});

Custom Formatters

getOutputSink() formats each record with the built-in logging.formatters.spanFormatter unless you pass your own span formatter. A span formatter returns Span[], and the Output's renderer turns the spans into ANSI, Markdown or plain text.

import * as logging from "@eserstack/logging";
import * as streams from "@eserstack/streams";

// Use a custom span formatter with a plain renderer
const out = streams.output({
  renderer: streams.renderers.plain(),
  sink: streams.sinks.stdout(),
});

const categoryFirst: logging.formatters.SpanFormatterFn = (record) => [
  streams.span.cyan(`[${record.category.join(".")}]`),
  streams.span.text(` ${record.message}`),
];

await logging.config.configure({
  sinks: {
    console: logging.sinks.getOutputSink(out, { formatter: categoryFirst }),
  },
  loggers: [{ category: ["app"], sinks: ["console"] }],
});

A string-based formatter (logging.FormatterFn) returns one line of text per record. Call it from a sink of your own:

import * as logging from "@eserstack/logging";
import * as streams from "@eserstack/streams";

const out = streams.output({
  renderer: streams.renderers.plain(),
  sink: streams.sinks.stdout(),
});

const customFormatter: logging.FormatterFn = (record) => {
  return `[${record.datetime.toISOString()}] ${record.category.join(
    ".",
  )} - ${record.message}\n`;
};

const textSink: logging.Sink = (record) => {
  out.write(customFormatter(record));
};

await logging.config.configure({
  sinks: { text: textSink },
  loggers: [{ category: ["app"], sinks: ["text"] }],
});

Testing with Record Collector

import * as assert from "node:assert/strict";
import * as logging from "@eserstack/logging";

const { sink, records } = logging.sinks.getRecordCollectorSink();

await logging.config.configure({
  sinks: { test: sink },
  loggers: [
    {
      category: ["app"],
      sinks: ["test"],
      lowestLevel: logging.Severities.Debug,
    },
  ],
});

const logger = logging.logger.getLogger(["app"]);
await logger.info("hello");

assert.equal(records.length, 1);
assert.equal(records[0]?.message, "hello");

API Reference

Namespaces

The library exports these namespaces:

| Namespace | Description | | -------------------- | --------------------------------------------------- | | logging.config | Configuration: configure(), reset() | | logging.logger | Logger: getLogger(), Logger class | | logging.context | Context: withContext(), getContext() | | logging.sinks | Sinks: getOutputSink(), getBufferedSink(), etc. | | logging.filters | Filters: getLevelFilter(), getCategoryFilter() | | logging.formatters | Formatters: spanFormatter, jsonFormatter, etc. | | logging.tracer | OpenTelemetry: withSpan() | | logging.category | Category utilities | | logging.types | Type definitions |

Configuration (logging.config)

configure(options: ConfigureOptions): Promise<void>

Configures the logging system with sinks, filters, and loggers.

import type * as logging from "@eserstack/logging";

type ConfigureOptions = {
  readonly sinks: logging.SinkConfig; // sinks by name
  readonly filters?: logging.FilterConfig; // filters by name
  readonly loggers: readonly logging.LoggerConfig[];
  readonly contextLocalStorage?: logging.ContextLocalStorage;
  readonly reset?: boolean;
};

contextLocalStorage defaults to AsyncLocalStorage, and reset: true clears the existing configuration before applying this one.

Each entry of loggers configures a category and the categories below it: lowestLevel, the names of its sinks and filters, and parentSinks. A category takes the level of the most specific entry that sets one, and the sinks and filters of its own entry and of every entry above it. An entry that names sinks and sets parentSinks: "override" stops that: the category takes no sinks or filters from the entries above the override entry.

Where a record goes:

  • When the category has sinks, a record at or above its level goes to those sinks only, after every one of its filters passes it.
  • When the category has no sinks, the record goes to the Go logging backend of @eserstack/ajan, or as a line on stderr when the native library is not available.

log() calls the sinks before it returns and does not wait for a sink that returns a promise. A sink or filter that throws or rejects does not fail the log call: it is reported once on stderr, and a failing filter drops the record.

reset(): Promise<void>

Resets the logging configuration.

Logger (logging.logger)

getLogger(category: Category | string): Logger

Gets or creates a logger for the given category.

Logger Class

Properties

  • category: Category - Hierarchical category array
  • parent: Logger | null - Parent logger reference
  • loggerName: string - Category as dot-separated string

Methods

  • log(severity, message, ...args) - Log at specified severity
  • trace(message, ...args) - Log trace message (most verbose)
  • debug(message, ...args) - Log debug message
  • info(message, ...args) - Log info message
  • notice(message, ...args) - Log notice message
  • warn(message, ...args) - Log warning message
  • error(message, ...args) - Log error message
  • critical(message, ...args) - Log critical message
  • alert(message, ...args) - Log alert message
  • emergency(message, ...args) - Log emergency message
  • getChild(subcategory) - Create child logger
  • with(properties) - Create logger with preset properties

Context (logging.context)

withContext<T>(context: Record<string, unknown>, fn: () => T): T

Runs a function with additional context properties.

getContext(): Record<string, unknown>

Gets the current logging context.

withCategoryPrefix<T>(prefix: string | Category, fn: () => T): T

Runs a function with a category prefix applied to all loggers.

Sinks (logging.sinks)

getOutputSink(output, options?): Sink

Creates a sink that writes log records to a @eserstack/streams Output. The Output's renderer determines the final format (ANSI, Markdown, plain text).

import * as logging from "@eserstack/logging";
import * as streams from "@eserstack/streams";

const out = streams.output({
  renderer: streams.renderers.ansi(),
  sink: streams.sinks.stdout(),
});
const sink = logging.sinks.getOutputSink(out);

// Optionally pass a custom span formatter
const mySpanFormatter: logging.formatters.SpanFormatterFn = (record) => [
  streams.span.text(record.message),
];
const sink2 = logging.sinks.getOutputSink(out, { formatter: mySpanFormatter });

getRecordCollectorSink(): { sink: Sink; records: LogRecord[] }

Creates a sink that collects records into an array (for testing).

getTestSink(): { sink: Sink; records: LogRecord[] } (deprecated)

Alias for getRecordCollectorSink(). Use getRecordCollectorSink() instead.

getBufferedSink(sink, options?): Sink

Creates a sink that buffers records and flushes in batches.

fingersCrossedSink(sink, options?): Sink

Creates a sink that buffers low-severity logs and flushes them all when a high-severity log (e.g. Error) occurs.

withFilter(sink, filter): Sink

Wraps a sink with a filter. Only records passing the filter reach the sink.

Filters (logging.filters)

getLevelFilter(level: Severity): Filter

Creates a severity level filter.

getCategoryFilter(category): Filter

Creates a category filter.

combineFilters(...filters): Filter

Combines filters with AND logic.

Formatters (logging.formatters)

spanFormatter: SpanFormatterFn

Default span-based formatter. Returns Span[] that adapts to any @eserstack/streams renderer (ANSI colors, Markdown, plain text). Used automatically by getOutputSink().

jsonFormatter: FormatterFn

JSON formatter — outputs structured JSON log lines.

textFormatter(options?): FormatterFn

Human-readable text formatter with configurable timestamp format, category separator, and level display.

jsonLinesFormatter: FormatterFn

Compact JSON Lines formatter (one JSON object per line).

SpanFormatterFn (type)

Type for formatter functions that produce Span[] instead of strings.

Tracer (logging.tracer)

withSpan<T>(name: string, fn: (span: Span) => Promise<T>): Promise<T>

Wraps an async function with automatic span and context correlation.

tracer: Tracer

Default OpenTelemetry tracer instance.


🔗 For further details, visit the eserstack repository.