Skip to main content

Logging Overview

NATS Server provides flexible logging capabilities to monitor server operations, debug issues, and audit activity.

Log Levels

NATS supports multiple log levels:
  1. Info - Normal operational messages (default)
  2. Warning - Warning messages for potential issues
  3. Error - Error messages for failures
  4. Fatal - Fatal errors that cause server shutdown
  5. Debug - Detailed debugging information
  6. Trace - Protocol-level tracing

Log Output Configuration

Standard Error (Default)

By default, logs go to stderr:
Output:

File Output

Redirect logs to a file:
Or via command line:

Log File Rotation

Automatic log rotation based on size:
From logger/log.go implementation, when the log file reaches the size limit:
  1. Current log renamed to nats-server.log.2026.03.04.10.15.30
  2. New log file created
  3. Configurable maximum number of archived log files

Disable Timestamps

Remove timestamps from logs:
Or via command line:

Debug Logging

Enable detailed debug output:
Or via command line:
Debug logs show:
  • Client connections and disconnections
  • Subscription operations
  • Route establishment
  • Account operations
  • Authentication attempts
Example debug output:

Trace Logging

Enable protocol-level tracing:
Or via command line:
Trace logs show raw protocol messages:

Verbose Trace

Trace including system account (from main.go:51):
Or debug and verbose trace:

Limit Traced Message Length

Limit message payload length in trace logs:
Or via command line:
Trace logging significantly impacts performance. Use only for debugging.

Syslog Support

Send logs to syslog or Windows Event Log:

Local Syslog

Or via command line:

Remote Syslog

Send to remote syslog server:
Or via command line:

Syslog Configuration

From logger/syslog.go, NATS supports:
  • Unix socket: /dev/log, /var/run/syslog
  • UDP: udp://host:port
  • TCP: tcp://host:port
Example:

Log Format

Standard Format

Default log format (from logger/log.go:74-95):
Example:

Components

  • [PID] - Process ID (optional, enabled with pid: true)
  • YYYY/MM/DD HH:MM:SS.microseconds - Timestamp with microsecond precision
  • [LEVEL] - Log level: INF, WRN, ERR, FTL, DBG, TRC
  • message - Log message

Colored Output

When outputting to terminal, logs can be colored:
Colors are disabled when logging to file or piping output.

Structured Logging

NATS logs include structured information:

Client Connection Logs

Fields:
  • IP address and port
  • Connection ID (cid)
  • Event description

Account Logs

Authentication Logs

Error Logs

Log Configuration Examples

Production Configuration

Development Configuration

High-Volume Production

Debugging Issues

Log Rotation Strategy

Size-Based Rotation

From logger/log.go implementation:
Behavior:
  1. When log reaches 100MB, rotate
  2. Old log renamed with timestamp
  3. New log file created
  4. Process continues without interruption

External Rotation (logrotate)

Use system log rotation: /etc/logrotate.d/nats-server:
Reopen log file:

Runtime Log Control

Change log levels without restart:
From main.go:39, supported signals:
  • ldm - Toggle debug and trace logging
  • reopen - Reopen log file
  • reload - Reload configuration

Performance Considerations

Log Level Impact

  • Info/Warn/Error: Minimal performance impact
  • Debug: Low to moderate impact (~5-10% overhead)
  • Trace: Significant impact (~20-50% overhead)

Recommendations

  1. Production: Use info level only
  2. Staging: Enable debug for issues
  3. Development: Use debug or trace freely
  4. Performance testing: Disable debug and trace

Async Logging

From logger/log.go implementation, file logging is buffered:
  • Writes are buffered to reduce I/O
  • Automatic flush on rotation
  • Minimal blocking of main thread

Monitoring Logs

Log Aggregation

Forward logs to aggregation systems:
Or use file monitoring:

Alerting

Monitor for critical patterns:
  • [ERR] - Error conditions
  • [FTL] - Fatal errors
  • Authorization violation - Security issues
  • slow consumer - Performance issues

Best Practices

1. Use Appropriate Log Levels

2. Rotate Logs

3. Limit Trace Payload

4. Centralize Logs

5. Monitor Log Growth

6. Secure Log Files