log2journal
log2journal
and systemd-cat-native
can be used to convert a structured log file, such as the ones generated by web servers, into systemd-journal
entries.
By combining these tools you can create advanced log processing pipelines sending any kind of structured text logs to systemd-journald. This is a simple, but powerful and efficient way to handle log processing.
The process involves the usual piping of shell commands, to get and process the log files in realtime.
The result is like this: nginx logs into systemd-journal:
The overall process looks like this:
tail -F /var/log/nginx/*.log |\ # outputs log lines
log2journal 'PATTERN' |\ # outputs Journal Export Format
systemd-cat-native # send to local/remote journald
These are the steps:
-
tail -F /var/log/nginx/*.log
this command will tail all*.log
files in/var/log/nginx/
. We use-F
instead of-f
to ensure that files will still be tailed after log rotation. -
log2journal
is a Netdata program. It reads log entries and extracts fields, according to the PCRE2 pattern it accepts. It can also apply some basic operations on the fields, like injecting new fields or duplicating existing ones or rewriting their values. The output oflog2journal
is in Systemd Journal Export Format, and it looks like this:KEY1=VALUE1 # << start of the first log line
KEY2=VALUE2
# << log lines separator
KEY1=VALUE1 # << start of the second log line
KEY2=VALUE2 -
systemd-cat-native
is a Netdata program. I can send the logs to a localsystemd-journald
(journal namespaces supported), or to a remotesystemd-journal-remote
.