JS Logs Interceptor

Elven Observability Official Logger

A high-performance log, event, and exception collector for Node.js apps that sends everything to Grafana Loki. It offers automatic console interception, support for dynamic labels, asynchronous sending with optimized buffering, and instant integration with the Elven Stack.

Features

High performance with asynchronous buffer, batch sending, and gzip compression.

Automatically captures:

  • console.log, console.error, console.warn, etc

  • Unhandled exceptions and rejected promises

  • Custom event via trackEvent

Support for static and dynamic labels (with trace/span ID via OpenTelemetry).

Smart buffer with configurable batch sending.

Resilient to network failures with automatic retry.

Compatible with both simple and complex apps (web servers, workers, CLI).

Delivery guarantee on process shutdown.

Gzip compression to reduce bandwidth usage.

Real-time metrics and integrated health check.

Installation

Basic Usage

Configuration Options

Transport

Parameter
Type
Description
Default

url

string

Loki endpoint (Push API)

tenantId

string

Used in the X-Scope-OrgID header

authToken

string

JWT token for authentication

undefined

timeout

number

HTTP timeout in ms

5000

maxRetries

number

Retries in case of failure

3

compression

boolean

Enables gzip compression

true

Application

Parameter
Type
Description
Default

appName

string

Application name (fixed label)

version

string

Application version

“1.0.0”

environment

string

Environment (prod, dev, staging)

“production”

labels

object

Fixed labels per log

{}

dynamicLabels

object

Dynamically generated labels

{}

Buffer & Performance

Parameter
Type
Description
Default

buffer.maxSize

number

Logs in the buffer before forcing send

100

buffer.flushInterval

number

Maximum time before sending (ms)

5000

buffer.autoFlush

boolean

Auto flush enabled

true

interceptConsole

boolean

Automatically captures console.*

false

enableMetrics

boolean

Collects performance metrics

true

Filtering & Sampling

Parameter
Type
Description
Default

filter.levels

array

Allowed log levels

[‘debug’,’info’,’warn’,’error’,’fatal’]

filter.samplingRate

number

Sampling rate (0.0–1.0)

1.0

filter.maxMessageLength

number

Maximum message size

8192

Example with Express.js

Auto-initialization with NODE_OPTIONS

For existing apps without code modification:

Metrics & Monitoring

Best Practices

Scenario
Recommendations

Short scripts (CLI, cron)

Use await logger.flush() before the exit.

Web apps or workers

The default setup already handles background sending.

High concurrency

Use maxSize: 500+ and flushInterval: 10000.

Environments with OpenTelemetry

Labels trace_id and span_id are captured automatically.

Production

Enable compression: true e samplingRate < 1.0 if needed.

Troubleshooting

Problem
Common cause
Solution

Logs are not showing up

tenantId, authToken or Incorrect URLs

Check the configuration and set debug: true."

Incomplete logs

Process terminating quickly

Use await logger.flush() before exiting

HTTP 400

Malformed payload

Check if Loki is running at the URL

Poor performance

Buffer too small

Increase maxSize and flushInterval

Manual Flush

Custom Events

In Loki, it will appear as:

Log Format in Loki

Each log sent includes:

  • Timestamp with nanosecond precision

  • Fixed and dynamic labels (e.g., app, env, trace_id, span_id)

  • Log type prefix (e.g.,[INFO], [ERROR], [EVENT], etc)

  • Content automatically serialized for objects and errors

Example of payload sent:

Integration with OpenTelemetry

This logger automatically integrates with OpenTelemetry, capturing the active trace and span IDs from the current context.

This enables seamless correlation between logs and traces in Grafana Tempo, allowing for complete end-to-end investigations.

What is already included:

  • trace_id and span_id are automatically added as dynamic labels.

  • No additional configuration is required, as long as the app already uses OpenTelemetry.

  • Compatible with any existing OpenTelemetry instrumentation.

Example with traces

In Loki:

Each log will automatically include:

You will be able to query Loki by filtering with trace_id, and with that, view the logs from the same trace shown in Grafana Tempo (or the trace view in the Elven Platform).

Docker & Kubernetes

Docker Compose

Kubernetes

License

This project is licensed under the MIT License.

How to contribute

  1. Fork this repository

  2. Create a branch with your improvement

  3. Submit a Pull Request

Any help is welcome to make the logger even more powerful!

Made with ❤️ by the Elven Observability team.

Last updated

Was this helpful?