Trojan Rust

Logging

Configuring logging output and levels

trojan-rs uses the tracing framework for structured logging.

Configuration

[logging]
level = "info"
format = "pretty"
output = "stderr"

Log Level

LevelDescription
errorErrors that affect operation
warnWarnings and degraded conditions
infoNormal operational events (connections, auth)
debugDetailed debugging information
traceVery verbose tracing (protocol bytes, state transitions)

Default: info

Log Format

FormatDescription
prettyHuman-readable, colorized output (default)
compactSingle-line human-readable format
jsonStructured JSON, one object per line

pretty — Best for development and interactive use:

  2024-01-15T10:30:00Z  INFO trojan_server: connection accepted client_ip=192.168.1.1

json — Best for log aggregation systems (ELK, Loki, etc.):

{"timestamp":"2024-01-15T10:30:00Z","level":"INFO","target":"trojan_server","message":"connection accepted","client_ip":"192.168.1.1"}

compact — Condensed single-line format:

2024-01-15T10:30:00Z INFO trojan_server: connection accepted client_ip=192.168.1.1

Output Target

TargetDescription
stderrStandard error (default)
stdoutStandard output

Per-Module Filters

Override the log level for specific modules:

[logging.filters]
trojan_auth = "debug"       # Verbose auth logging
trojan_server = "info"      # Normal server logging
rustls = "warn"             # Suppress rustls noise
h2 = "warn"                 # Suppress HTTP/2 noise

This is useful for debugging specific subsystems without flooding output from other modules.

CLI Override

The --log-level CLI flag overrides the config file:

trojan server -c config.toml --log-level debug

Production Recommendations

For production deployments:

[logging]
level = "info"
format = "json"
output = "stderr"

[logging.filters]
rustls = "warn"
  • Use json format for structured log aggregation
  • Set info level for normal operations
  • Suppress verbose third-party crate logging with filters
  • Route stderr to your log collection system (journald, fluentd, etc.)

Log Rotation

trojan-rs writes to stderr/stdout and does not manage log files directly. Use your system's log management:

systemd/journald

When running as a systemd service, logs go to journald automatically:

journalctl -u trojan -f           # Follow logs
journalctl -u trojan --since today # Today's logs
journalctl -u trojan -p err        # Errors only

File-based rotation

Redirect output and use logrotate:

trojan server -c config.toml 2>> /var/log/trojan/server.log
# /etc/logrotate.d/trojan
/var/log/trojan/*.log {
    daily
    rotate 14
    compress
    missingok
    notifempty
    copytruncate
}

On this page