Skip to content

Logging ​

Structured logging cross-cutting concern for Smite applications

package @smitejs/logs

Smite.js Logging ​

The @smitejs/logs package provides a generic structured logging abstraction that integrates with Smite's cross-cutting concern mechanisms. It is transport-agnostic and scope-anchored, meaning loggers are tied to the current execution context via AsyncLocalStorage and are automatically available anywhere in the same async boundary.

Core Concepts ​

Scope-anchored loggers ​

Loggers are not standalone objects — they are registered into the current @smitejs/core scope via registerLogger. This means:

  • A logger created in one part of the codebase is automatically available in deeply nested async calls within the same boundary.
  • currentLogger() retrieves the logger for the current scope, or undefined if none has been registered.
  • runWithLogger(options, run) creates a logger, registers it, runs a callback, and cleans up after.

Transport-agnostic interface ​

The public Logger interface is independent of any specific backend:

ts
it("runWithLogger makes logger available during the callback", () => {
  const logger = logs.runWithLogger({ level: "info" }, () => {
    return logs.currentLogger();
  });
  expect(logger).toBeDefined();
});

The underlying implementation uses pino, but the interface contracts do not expose pino types. This allows swapping the transport (pino → winston → bunyan) without changing consumer code.

Built-in AOP Aspects ​

The package provides ready-to-use aspects for common cross-cutting logging scenarios:

logs.jobLogger(options) ​

A middleware aspect that provides a scope-anchored logger throughout the pipeline. If no logger exists in the scope, one is created with the given options and registered.

Use as: app.use(logs.jobLogger({ level: "debug" }))

logs.jobExecutionLogger(options) ​

A middleware aspect that creates a logger scoped to a job execution context. It also stores a label in the scope for identification.

Use as: app.use(logs.jobExecutionLogger({ label: "order-processing" }))

logs.errorLoggingGuard(options) ​

A guard aspect that short-circuits the pipeline on handler error, logging the error before returning a 500 response.

Use as: app.use(logs.errorLoggingGuard())

logs.aroundLogger(options) ​

A middleware aspect that logs the duration of the pipeline run, from entry to exit.

Use as: app.use(logs.aroundLogger({ level: "trace" }))

Usage Patterns ​

HTTP Request Logging (beyond the built-in requestLogger) ​

The built-in http.requestLogger() from @smitejs/http already provides request scoped logging for HTTP. Use @smitejs/logs when you need logging that spans across multiple contexts (HTTP + jobs + messaging, for example):

ts
it("aroundLogger creates a middleware aspect", () => {
  const aspect = logs.aroundLogger({ level: "trace" });
  expect(aspect.kind).toBe("middleware");
  expect(typeof aspect.fn).toBe("function");
});

Job Logging ​

ts
it("jobExecutionLogger creates a middleware aspect with label", () => {
  const aspect = logs.jobExecutionLogger({ label: "test-job" });
  expect(aspect.kind).toBe("middleware");
  expect(typeof aspect.fn).toBe("function");
});
ts
"guard"
"function"
"middleware"
"function"

Scope Integration ​

ts
it("registerScopeLogger makes logger available within an active scope", () => {
  const logger = logs.createLogger({ level: "debug" });
  runWithScope({}, () => {
    logs.registerScopeLogger(logger);
    const retrieved = logs.currentLogger();
    expect(retrieved).toBe(logger);
  });
});
ts
logger

Released under the MIT License.

/ search•[ ] prev/next•j/k scroll•h home•t theme•guide/techniques/logging