Skip to content

Add logToSyslog option for journald/syslog logging#3

Open
anpryl wants to merge 3 commits into
menixator:mainfrom
anpryl:add-syslog-logging
Open

Add logToSyslog option for journald/syslog logging#3
anpryl wants to merge 3 commits into
menixator:mainfrom
anpryl:add-syslog-logging

Conversation

@anpryl
Copy link
Copy Markdown

@anpryl anpryl commented Mar 8, 2026

Summary

Add a logToSyslog boolean option (default false) that sends all nginx log output to syslog via the unix socket /dev/log instead of files. On systemd-based systems like NixOS, journald captures syslog messages automatically — providing automatic rotation, structured querying via journalctl, and eliminating the need for logrotate configuration.

Changes

  • New option services.lancache.logToSyslog — when true, all 6 nginx log directives use syslog:server=unix:/dev/log,facility=local6,tag=<tag> instead of file paths
  • Changed logPrefix type from types.str to types.nullOr types.str — allows omitting it when syslog is enabled
  • Added assertion — ensures at least one logging method (logPrefix or logToSyslog) is configured
  • Refactored log targets — extracted logTarget helper function to reduce duplication across the 6 log directives
  • Per-context syslog tags for easy filtering:
    • lancache — primary cache engine
    • lancache_upstream — upstream redirect handler
    • lancache_stream — TLS SNI passthrough

Backward compatibility

  • logToSyslog defaults to false — existing configs are unaffected
  • nullOr str is a superset of str — every existing logPrefix value remains valid
  • File-based log filenames are preserved exactly (access.log, upstream-access.log, stream-access.log, etc.)

Usage

services.lancache = {
  enable = true;
  logToSyslog = true;
  # logPrefix is no longer needed
};

Filter logs with:

journalctl _COMM=nginx --grep="lancache:"
journalctl _COMM=nginx --grep="lancache_upstream:"
journalctl _COMM=nginx --grep="lancache_stream:"

Test plan

  • Build NixOS configuration with logToSyslog = true — succeeds
  • Deploy to NixOS server — nginx starts without errors
  • Verify syslog entries appear in journald after HTTP request
  • Verify backward compatibility: logToSyslog = false with logPrefix set produces identical nginx config to upstream

anpryl and others added 3 commits March 8, 2026 22:44
Lancache currently only supports file-based logging via the logPrefix
option. On systemd-based systems like NixOS, logging to journald is
preferable — it provides automatic rotation, structured querying via
journalctl, and eliminates the need for logrotate configuration.

Add a logToSyslog boolean option (default false) that sends all nginx
log output to syslog via the unix socket /dev/log. Different nginx
contexts use distinct syslog tags for easy filtering:
- lancache (primary cache engine)
- lancache-upstream (upstream redirect handler)
- lancache-stream (TLS SNI passthrough)

Also changes logPrefix type from str to nullOr str so it can be omitted
when logToSyslog is enabled. An assertion ensures at least one logging
method is configured.

Fully backward compatible — existing configs with logPrefix continue
to work unchanged.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
nginx rejects syslog tags containing hyphens with:
  syslog "tag" only allows alphanumeric characters and underscore

Replace lancache-upstream → lancache_upstream and
lancache-stream → lancache_stream.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Replace 6 individual log target variables with a single logTarget
function that generates both access and error log paths for each
nginx context. Preserves the original file names for backward
compatibility.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@anpryl anpryl marked this pull request as ready for review March 8, 2026 20:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant