Skip to main content

lib/logging

Logging and telemetry system for True Life.

This module provides a comprehensive logging and tracing solution that works across all runtimes (web, client, server) and exports telemetry to OpenTelemetry.

Architecture

┌──────────┐     ┌──────────┐     ┌──────────┐     ┌──────────────────┐
│ Web UI │ ──► │ Client │ ──► │ Server │ ──► │ OTEL Collector │
└──────────┘ └──────────┘ └──────────┘ └──────────────────┘


┌──────────────────┐
│ Tempo / Loki │
└──────────────────┘


┌──────────────────┐
│ Grafana │
└──────────────────┘

Quick Start

import { log, createLogger, withSpan } from "@core/logging";

// Simple logging
log.info("Application started");
log.error("Something failed", new Error("oops"));

// Module-specific logger
const logger = createLogger("banking");
logger.info("Transaction complete", { amount: 100 });

// Traced operations
await withSpan("process-payment", async (span) => {
span.attributes = { orderId: "123" };
log.info("Processing payment...");
await processPayment();
});

Trace Context Propagation

Trace context flows from web → client → server, creating a unified trace:

[web:button-click]
└── [client:handle-interaction]
└── [server:process-request]

Enumerations

EnumerationDescription
LogLevelLog severity levels aligned with OpenTelemetry severity numbers.

Classes

ClassDescription
LoggerStructured logger with trace context support.

Interfaces

InterfaceDescription
LoggerConfigConfiguration for the logger.
LogRecordA structured log record compatible with OTLP Log Data Model.
SpanA span representing a unit of work in a trace.
TelemetryBatchBatch of telemetry data for forwarding between runtimes. Used for the web → client → server pipeline.
TraceContextW3C Trace Context compatible trace context.
TraceStackStack of trace contexts for nested operations. Allows building a trace hierarchy as requests flow through the system.

Type Aliases

Type AliasDescription
AttributesArbitrary attributes that can be attached to log records and spans.
LogLevelNameString representation of log levels for human readability.
RuntimeRuntime environment identifier.

Variables

DEFAULT_LOGGER_CONFIG

const DEFAULT_LOGGER_CONFIG: LoggerConfig;

Defined in: src/lib/logging/types.ts:275

Default logger configuration.


log

const log: Logger;

Defined in: src/lib/logging/logger.ts:376

Global logger instance. Use this for quick logging without creating a module-specific logger.


LOG_LEVEL_NAMES

const LOG_LEVEL_NAMES: Record\<LogLevel, LogLevelName\>;

Defined in: src/lib/logging/types.ts:45

Maps LogLevel enum to string names.


LOG_LEVEL_VALUES

const LOG_LEVEL_VALUES: Record\<LogLevelName, LogLevel\>;

Defined in: src/lib/logging/types.ts:57

Maps string names to LogLevel enum values.

Functions

FunctionDescription
buildTraceStackBuilds a trace stack from the current context stack.
clearContextStackClears the entire context stack. Useful for cleanup between tests or on shutdown.
createContextFromStackCreates a trace context from a trace stack received from another runtime. This continues the trace by creating a new span as a child of the last span.
createLoggerCreates a logger for a specific module.
createTraceContextCreates a new trace context, optionally as a child of an existing context.
endSpanEnds a span and pops it from the context stack.
generateSpanIdGenerates a new span ID (16 hex characters).
generateTraceIdGenerates a new trace ID (32 hex characters).
getActiveSpanGets an active span by its span ID.
getCurrentContextGets the current trace context, if any.
getRuntimeGets the current runtime.
onLogRecordRegisters a listener for log records. Used by the telemetry pipeline to forward logs.
onSpanEndRegisters a listener for when spans end.
onSpanStartRegisters a listener for when spans start.
parseTraceparentParses a W3C traceparent header into a trace context.
popContextPops the current trace context from the stack.
pushContextPushes a trace context onto the stack.
serializeTraceparentSerializes a trace context to a W3C traceparent header format.
setLogLevelSets the minimum log level for the global logger.
setRuntimeSets the current runtime. Called during initialization.
startSpanStarts a new span and pushes it onto the context stack.
withSpanExecutes a function within a new span context. Automatically ends the span when the function completes.
withSpanSyncSynchronous version of withSpan for non-async operations.