Skip to content

@smitejs/logs ​

Structured logging cross-cutting concern for Smite applications

bash
yarn add @smitejs/logs

19 documented symbols · version 2.0.0-SNAPSHOT.0 · source

Aspects ​

functionaroundLogger ​

ts
function aroundLogger(options: HttpAspectOptions): HttpAspect;

Creates an AOP aspect middleware that logs entry and exit of a callback, registering a scope-anchored logger.

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

packages/logs/src/aspects.ts:178

functionerrorLoggingGuard ​

ts
function errorLoggingGuard(_options?: HttpAspectOptions): HttpAspect;

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

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

packages/logs/src/aspects.ts:147

functionjobExecutionLogger ​

ts
function jobExecutionLogger(options: HttpAspectOptions): HttpAspect;

Creates an AOP aspect middleware for job execution. Creates a logger scoped to the job context and makes it available via currentLogger throughout the job's async lifecycle.

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"

packages/logs/src/aspects.ts:111

functionjobLogger ​

ts
function jobLogger(options: HttpAspectOptions): HttpAspect;

Creates an AOP aspect middleware that provides a scope-anchored logger. The aspect wraps the pipeline and makes the logger available via currentLogger().

ts
describe("AOP aspects", () => {
  it("jobLogger creates a middleware aspect", () => {
    const aspect = logs.jobLogger({ level: "info" });
    expect(aspect.kind).toBe("middleware");
    expect(typeof aspect.fn).toBe("function");
  });
ts
"middleware"
"function"
"guard"
"function"
"middleware"
"function"

packages/logs/src/aspects.ts:76

Logger ​

functioncreateLogger ​

ts
function createLogger(options?: {
    readonly level?: string;
    readonly base?: Readonly<Record<string, unknown>>;
}): Logger;

Creates a pino-backed Logger implementing the generic interface. The resulting logger is automatically scope-anchored: it reads the active @smitejs/core scope and pins itself to it via registerLogger, so that any code in the same async context can retrieve it with currentLogger.

ts
describe("Logger interface", () => {
  it("createLogger returns an object with info/warn/error/debug/trace methods", () => {
    const logger = logs.createLogger({ level: "info" });
    expect(logger).toHaveProperty("info");
    expect(logger).toHaveProperty("warn");
    expect(logger).toHaveProperty("error");
    expect(logger).toHaveProperty("debug");
    expect(logger).toHaveProperty("trace");
    expect(typeof logger.info).toBe("function");
    expect(typeof logger.warn).toBe("function");
    expect(typeof logger.error).toBe("function");
    expect(typeof logger.debug).toBe("function");
    expect(typeof logger.trace).toBe("function");
  });

  it("createLogger defaults to level 'info'", () => {
    const logger = logs.createLogger();
    expect(logger).toBeDefined();
  });

  it("currentLogger returns undefined when no logger registered", () => {
    const logger = logs.currentLogger();
    expect(logger).toBeUndefined();
  });
ts
logger
logger

packages/logs/src/logger.ts:22

functioncreateScopedLogger ​

ts
function createScopedLogger(_context: ScopeContext, options?: {
    readonly level?: string;
    readonly base?: Readonly<Record<string, unknown>>;
}): Logger;

Creates a logger that is scoped to a custom context. The logger is registered into a new scope via runWithScope and retrieved via currentLogger within that scope.

ts
it("createScopedLogger registers logger in the current scope", () => {
  const logger = logs.createLogger({ level: "info" });
  runWithScope({}, () => {
    logs.registerScopeLogger(logger);
    const retrieved = logs.currentLogger();
    expect(retrieved).toBeDefined();
  });
});
ts
"middleware"
"function"
"middleware"
"function"
"guard"
"function"
"middleware"
"function"
logger

packages/logs/src/logger.ts:105

functioncurrentLogger ​

ts
function currentLogger(): Logger | undefined;

Returns the logger that was registered for the current scope, or undefined when no logger has been registered.

ts
it("currentLogger retrieves logger registered in the active scope", () => {
  const logger = logs.createLogger({ level: "info" });
  runWithScope({}, () => {
    logs.registerScopeLogger(logger);
    const retrieved = logs.currentLogger();
    expect(retrieved).toBe(logger);
  });
});

packages/logs/src/logger.ts:72

functionregisterScopeLogger ​

ts
function registerScopeLogger(logger: Logger): void;

Registers a logger into the current @smitejs/core scope so that currentLogger() can retrieve it later.

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

packages/logs/src/logger.ts:57

functionrunWithLogger ​

ts
function runWithLogger<T>(options: {
    readonly level?: string;
    readonly base?: Readonly<Record<string, unknown>>;
}, run: () => T): T;

Runs a callback with a scope-anchored logger. The logger is registered into the @smitejs/core scope for the duration of run, and is automatically cleaned up after.

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

packages/logs/src/logger.ts:83

Reference ​

functionaspect ​

ts
function aspect(kind: (typeof HttpAspectKind)[keyof typeof HttpAspectKind], fn: HttpAspect["fn"]): HttpAspect;

@internal

packages/logs/src/aspects.ts:68

interfaceHttpAspect ​

ts
interface HttpAspect {
    kind: (typeof HttpAspectKind)[keyof typeof HttpAspectKind];
    fn: HttpMiddleware | HttpGuard | HttpInterceptor | HttpFilter;
}

A named pipeline stage registered on an app via app.use. Built with the aspect factory and stored as an http.aspect IR node on the app.

packages/logs/src/aspects.ts:57

constHttpAspectKind ​

ts
const HttpAspectKind: {
    readonly MIDDLEWARE: "middleware";
    readonly GUARD: "guard";
    readonly INTERCEPTOR: "interceptor";
    readonly FILTER: "filter";
};

type HttpAspectKind = keyof typeof HttpAspectKind;

The kind of an aspect: which stage of the pipeline it runs at.

packages/logs/src/aspects.ts:4

typeHttpAspectKind ​

ts
const HttpAspectKind: {
    readonly MIDDLEWARE: "middleware";
    readonly GUARD: "guard";
    readonly INTERCEPTOR: "interceptor";
    readonly FILTER: "filter";
};

type HttpAspectKind = keyof typeof HttpAspectKind;

@internal

packages/logs/src/aspects.ts:13

interfaceHttpAspectOptions ​

ts
interface HttpAspectOptions {
    readonly level?: string;
    readonly base?: Readonly<Record<string, unknown>>;
    readonly label?: string;
}

Options shared by all logging aspects.

packages/logs/src/aspects.ts:16

typeHttpFilter ​

ts
type HttpFilter = (response: unknown, ctx: {
    scope: ScopeContext;
}) => unknown | Promise<unknown>;

A response post-processor running after the handler.

packages/logs/src/aspects.ts:49

typeHttpGuard ​

ts
type HttpGuard = (ctx: {
    scope: ScopeContext;
}, next: () => Promise<unknown>) => unknown | undefined | Promise<unknown | undefined>;

A request gate that may short-circuit with a response.

packages/logs/src/aspects.ts:33

typeHttpInterceptor ​

ts
type HttpInterceptor = (ctx: {
    scope: ScopeContext;
}, next: () => Promise<unknown>) => unknown | Promise<unknown>;

An outer wrapper around the whole pipeline that can rewrite the final response.

packages/logs/src/aspects.ts:41

typeHttpMiddleware ​

ts
type HttpMiddleware = (ctx: {
    scope: ScopeContext;
}, next: () => Promise<unknown>) => unknown | Promise<unknown>;

A pipeline stage that wraps the remainder of the chain.

packages/logs/src/aspects.ts:25

interfaceLogger ​

ts
interface Logger {
    info(obj: Record<string, unknown>, msg?: string, ...args: unknown[]): void;
    info(msg: string, ...args: unknown[]): void;
    warn(obj: Record<string, unknown>, msg?: string, ...args: unknown[]): void;
    warn(msg: string, ...args: unknown[]): void;
    error(obj: Record<string, unknown>, msg?: string, ...args: unknown[]): void;
    error(msg: string, ...args: unknown[]): void;
    debug(obj: Record<string, unknown>, msg?: string, ...args: unknown[]): void;
    debug(msg: string, ...args: unknown[]): void;
    trace(obj: Record<string, unknown>, msg?: string, ...args: unknown[]): void;
    trace(msg: string, ...args: unknown[]): void;
}

Generic structured logger interface. Implementations must be pure (no side effects beyond I/O) and transport-agnostic so callers are not coupled to pino internally.

packages/logs/src/logger.ts:4

Released under the MIT License.

/ search•[ ] prev/next•j/k scroll•h home•t theme•api/smitejs-logs