loggingclojurejvmsystemd-journald

JVM to use Systemd log transport and output straight into JournalCTL instead of STDout


JVM to use Systemd log transport and output straight into JournalCTL instead of STDout. I am developing Clojure application ant it prints logs straight into stdout. From there SystemD service picks it and puts into log file. But it happens that exceptions are logged in multiple separate lines. How to use SystemD logs that it will have multiline log entries?


Solution

  • JNA allows this. Try adding the library net.java.dev.jna/jna (tested with 5.5.0) and then code along these lines works to print to the journal:

    (import '[com.sun.jna Function])
    
    (def sd-journal-print (Function/getFunction "systemd" "sd_journal_print")
    (def sd-journal-send (Function/getFunction "systemd" "sd_journal_send")
    
    (def emerg-priority 0)
    
    ;; Plain print with priority and message
    (.invoke sd-journal-print (to-array [emerg-priority "test\nmessage"]))
    
    ;; Structured logging
    (.invoke sd-journal-send (to-array ["MESSAGE=%s" "another\ntest\nmessage" "PRIORITY=%d" emerg-priority "WHATEVER=stuff" nil])) ; nil required!
    

    Monitoring with journalctl -f -o json-pretty is an easy way to validate this is working...

    How you integrate this in to your existing logging is up to you.

    An intro to this API is at http://0pointer.de/blog/projects/journal-submit.html