Skip to main content
Plain text logs are difficult to search, filter, and aggregate at scale. When every log line is a JSON object with consistent fields — level, timestamp, service name, and context — your log aggregator can index them, your dashboards can chart them, and your on-call engineer can query them in seconds. @figentra/logger gives every Figentra service the same structured JSON output format so that logs behave predictably no matter which service emits them.

Installation

Creating a Logger

Call createLogger once at module scope and pass at minimum a service name. The returned logger instance is safe to share across the entire service — it is stateless and not bound to any single request.

Logging Methods

Each method corresponds to a severity level and accepts an optional context object whose keys are merged into the emitted JSON.

logger.debug

Use debug for verbose information that helps trace execution flow during development. Debug logs are suppressed in production unless the log level is explicitly set to debug.

logger.info

Use info for normal operational events — requests received, records created, background jobs completed. This is the default level for production services.

logger.warn

Use warn for unexpected situations that the service recovered from but that might indicate a deeper problem — a slow database query, a deprecated API call, or a cache miss rate spike.

logger.error

Use error for failures that require attention — unhandled exceptions, failed external calls, or data integrity issues. Pass the original Error object as the second argument so the stack trace is captured in the log output.

Log Output Format

Every log line is a single-line JSON object written to stdout. The following fields are always present:
Error logs additionally include error.message and error.stack when an Error object is provided:

Child Loggers

Create a child logger to automatically attach persistent context fields — such as a request ID or authenticated user ID — to every log line emitted within that scope. Child loggers share the parent’s level and transport configuration.
Both lines above will include requestId and userId without you passing them manually each time.

Log Levels

Only messages at or above the configured level are emitted. Setting the level to warn, for example, suppresses all debug and info output.

API Reference

function
required
Creates a new logger instance. Accepts { service: string, level?: LogLevel } and returns a Logger object.
method
Returns a new Logger that inherits the parent’s configuration and merges the provided context object into every log line it emits.
method
Emits a log line at level debug. Signature: (message: string, context?: Record<string, unknown>) => void.
method
Emits a log line at level info. Signature: (message: string, context?: Record<string, unknown>) => void.
method
Emits a log line at level warn. Signature: (message: string, context?: Record<string, unknown>) => void.
method
Emits a log line at level error. Signature: (message: string, error?: Error, context?: Record<string, unknown>) => void.

Complete Example

The following example shows a typical Express request handler that creates a child logger per request, logs at multiple levels throughout the lifecycle, and captures errors with full context.
Create a child logger at the start of every request handler and pass it down to service and repository calls rather than using the root logger directly. Every log line will automatically carry the request ID, making it trivial to reconstruct the full trace for any single request in your log aggregator.
If @figentra/config is present in your project and exports a LOG_LEVEL variable, @figentra/logger will use that value as the default level when no explicit level option is passed to createLogger. This means you can control logging verbosity through your standard environment variable config without touching any code.