I was looking for a way to log from openhab2 into a graylog logging server.
After various tries I got the following solution working:
Enable the syslog listeners in rsyslogd
Add an syslog appender in Karaf to log to the local syslog daemon
Add a rsyslog configuration to propagate log entries to remote graylog server
All tries to directly log from Karaf to graylog where unsuccessful. For the direct connection the GELF-Appender jar is missing in Karaf. The direct connection from the SyslogAppender to graylog did not work perhaps for message format reasons.
Enable syslog listeners in /etc/rsyslogd.conf:
Add or uncomment
It is important to use a well-known log facility to rsyslog, as local0 in this example
Add a rsyslog configuration to propagate log entries to remote graylog server:
Add a file client.conf with following content to /etc/rsyslog.d/
# An "In-Memory Queue" is created for remote logging.
$WorkDirectory /var/spool/rsyslog # where to place spool files
$ActionQueueFileName queue # unique name prefix for spool files
$ActionQueueMaxDiskSpace 1g # spool space limit (use as much as possible)
$ActionQueueSaveOnShutdown on # save messages to disk on shutdown
$ActionQueueType LinkedList # run asynchronously
$ActionResumeRetryCount -1 # infinety retries if host is down
# Define custom logging templates
$template GRAYLOGRFC5424,"<%PRI%>%PROTOCOL-VERSION% %TIMESTAMP:::date-rfc3339% %HOSTNAME% %APP-NAME% %PROCID% %MSGID% %STRUCTURED-DATA% %msg%\n"
#Using default format for default logging format:
$ActionFileDefaultTemplate RSYSLOG_TraditionalFileFormat
# Sending logs that match *.* to graylog1.bausdorf.de via TCP on 514 using GRAYLOGRFC5424 format.
*.* @@graylog2.bausdorf.de:514;GRAYLOGRFC5424
This is great and I’m looking to implement. I’m getting an error in my syslog with the following:
Dec 1 19:35:38 OHTest start.sh[445]: log4j:ERROR Could not find metis. All logging will FAIL.
Dec 1 19:35:38 OHTest start.sh[445]: java.net.UnknownHostException: metis: Name or service not known
I did an apt install metis but that didn’t seem to make a difference. I’m on debian 8.9
Nice. I guess I didn’t read that well. I was tired.
I spent some time with this today and wanted to spit out some of my findings.
You don’t need to feed OH through rsyslog first. You can log directly to graylog server’s syslog input if you like. I didn’t want all the other debian default syslog stuff so instead of modifying how debian syslogs, I just bypassed rsyslog.
If you do use rsyslog, the template is no longer needed as newer rsyslog has it built in:
I also tweaked the appender definition as it was passing through source as localhost and I didn’t care for the message layout. So, if you simply want to take a plain Jane OH on debian and log to graylog on a remote host both root and events:
Since OH 2.2 changed the logging from logj to log4j2 here ist the update for the Karaf logging config in
/var/lib/openhab2/etc/org.ops4j.pax.logging.cfg
On my first try I had an error which said something like “… Plugin not found …”.
It turned out that the appender properties lines MUST NOT have trailing blanks or tabs. To prove if thats your problem too take a look at /var/lib/openhab2/config/org/ops4j/pax/logging.conf, which seems to be generated on the fly.
If you find an entry like
log4j2.appender.syslog.name="SYSLOG\ "
then you have a trailing-blank-problem.
Another problem I had during upgrade from 2.1 to 2.2 was that I decided to keep my old logging config. But they moved from log4j to log4j2 so my logging was completely broken after the first try.
PS: I upgraded my graylog to 2.3.2 - and I do no longer need to syslog to localhost first. Graylog has obviously fixed something in their Syslog input.
Ah yes. That is it. I can see this being an issue for others. If you copy your block of code and paste that… it will create a bunch of spaces. My config file had " / / / / / / / / / " for miles on each entry.
(I edited the root-logger and the event-logger and added a log appender to the syslog host.)
After the first start, I got errors in the console and nothing has been logged, but I found the trailing spaces in the config file (like mentioned above).
But after removing the spaces, there were no error messages in the console, but the log messages only appear in the standard log-files and no message is send to the syslog server.
My openhab instance runs in a docker container.
Can anybody see an error in the log-config?
Is there a way to test the config in the karaf-console?
Had that problem too - my servers hardware clock is using UTC, I had to adjust the locale setting either globally or at least for the user which is running openhab.
I tried my problem in a separate, new thread but didn’t get any valuable response. I have the same setup, encountered the same problem with time settings on the graylog side. Everything would be perfect if … when the graylog server is restarted openhab(ian) stops sending any UDP packets to the graylog server. It has to be restarted to resend them again. But I see nothing in any (openhabian) logs.
How could I make openhab retry if the connection to the graylog server breaks for any reason?
How could I identify this problem to be notified in advance?
While the GELF appender does not come with Karaf (as reported above), there is a GELF layout so you can use the Socket appender instead. I’ve tested this to work with GrayLog 5. Don’t forget to enable the GELF TCP Input.
See the URL in the comment above for full docs and a UDP example.