All Projects → bwaldvogel → Log4j Systemd Journal Appender

bwaldvogel / Log4j Systemd Journal Appender

Licence: bsd-3-clause
Log4j appender for systemd-journal that maintains structured log data

Programming Languages

java
68154 projects - #9 most used programming language

Labels

Projects that are alternatives of or similar to Log4j Systemd Journal Appender

Breath OS
An OS for breathing? My DIY OS
Stars: ✭ 17 (-26.09%)
Mutual labels:  systemd
Ip2unix
Turn IP sockets into Unix domain sockets
Stars: ✭ 295 (+1182.61%)
Mutual labels:  systemd
Rustysd
A service manager that is able to run "traditional" systemd services, written in rust
Stars: ✭ 416 (+1708.7%)
Mutual labels:  systemd
chef-systemd
resource-driven chef cookbook for managing linux systems via systemd
Stars: ✭ 43 (+86.96%)
Mutual labels:  systemd
Daemon
Simple example of daemon for Linux
Stars: ✭ 267 (+1060.87%)
Mutual labels:  systemd
Dmenu Extended
An extension to dmenu for quickly opening files and folders.
Stars: ✭ 334 (+1352.17%)
Mutual labels:  systemd
plug systemd example
Example Elixir application that integrates with systemd features
Stars: ✭ 15 (-34.78%)
Mutual labels:  systemd
Hardening
Hardening Ubuntu. Systemd edition.
Stars: ✭ 705 (+2965.22%)
Mutual labels:  systemd
Supervizer
NodeJS Application Manager
Stars: ✭ 278 (+1108.7%)
Mutual labels:  systemd
Kubernetes Ansible
🎄ansible多网卡机器上一键部署高可用Kubernetes(systemd)
Stars: ✭ 402 (+1647.83%)
Mutual labels:  systemd
HowLogindWorks
A collection of info about session management with logind
Stars: ✭ 22 (-4.35%)
Mutual labels:  systemd
modern-linux.info
Learning Modern Linux book website
Stars: ✭ 35 (+52.17%)
Mutual labels:  systemd
Ecominit
eComInit is a free init system and service manager designed to scale from lightweight desktops to web-scale cloud deployments. It aims to offer feature-parity with systemd but with a modular, portable architecture compliant with software engineering best-practice.
Stars: ✭ 352 (+1430.43%)
Mutual labels:  systemd
brand.systemd.io
Website with systemd brand assets
Stars: ✭ 12 (-47.83%)
Mutual labels:  systemd
Systemd Swap
Script for creating hybrid swap space from zram swaps, swap files and swap partitions.
Stars: ✭ 473 (+1956.52%)
Mutual labels:  systemd
execute-engine
基于Ansible API的任务执行引擎,支持adhoc和playbook两种任务的执行
Stars: ✭ 18 (-21.74%)
Mutual labels:  systemd
Restic Systemd Automatic Backup
My restic backup solution using Backblaze B2 storage, systemd timers (or cron) and email notifications on failure.
Stars: ✭ 314 (+1265.22%)
Mutual labels:  systemd
Python Systemd Tutorial
A tutorial for writing a systemd service in Python
Stars: ✭ 746 (+3143.48%)
Mutual labels:  systemd
Systemd Manager
A systemd service manager written in Rust with the GTK-rs wrapper and direct integration with dbus
Stars: ✭ 552 (+2300%)
Mutual labels:  systemd
Clight
A C daemon that turns your webcam into a light sensor. It will adjust screen backlight based on ambient brightness.
Stars: ✭ 371 (+1513.04%)
Mutual labels:  systemd

CI Maven Central BSD 3-Clause License Donate

Log4j appender that logs event meta data such as the timestamp, the logger name, the exception stacktrace, ThreadContext (aka MDC) or the Java thread name to fields in systemd journal (aka "the Journal") .

Read Lennart Poettering's blog post systemd for Developers III if you are not familar with systemd journal.

Usage with Log4j 2.x

Add the following Maven dependency to your project:

<dependency>
	<groupId>de.bwaldvogel</groupId>
	<artifactId>log4j-systemd-journal-appender</artifactId>
	<version>2.4.0</version>
	<scope>runtime</scope>
</dependency>

Usage with Log4j 1.x

See the 1.x branch of this project.

Runtime dependencies

  • Java 8 or later
  • Linux with systemd-journal
  • Log4j 2.x

Note:

JNA requires execute permissions in java.io.tmpdir (which defaults to /tmp). For example, if the folder is mounted with "noexec" for security reasons, you need to define a different temporary directory for JNA:

-Djna.tmpdir=/tmp-folder/with/exec/permissions

Configuration

The appender can be configured with the following properties

Property name Default Type Description
logSource false boolean Determines whether the log locations are logged. Note that there is a performance overhead when switched on. The data is logged in standard systemd journal fields CODE_FILE, CODE_LINE and CODE_FUNC.
logStacktrace true boolean Determines whether the full exception stack trace is logged. This data is logged in the user field STACKTRACE.
logThreadName true boolean Determines whether the thread name is logged. This data is logged in the user field THREAD_NAME.
logLoggerName true boolean Determines whether the logger name is logged. This data is logged in the user field LOG4J_LOGGER.
logAppenderName true boolean Determines whether the appender name is logged. This data is logged in the user field LOG4J_APPENDER.
logThreadContext true boolean Determines whether the thread context is logged. Each key/value pair is logged as user field with the threadContextPrefix prefix.
threadContextPrefix THREAD_CONTEXT_ String Determines how thread context keys should be prefixed when logThreadContext is set to true. Note that keys need to match the regex pattern [A-Z0-9_]+ and are normalized otherwise.
syslogIdentifier null String This data is logged in the syslog compatibility field SYSLOG_IDENTIFIER. If this is not set, the underlying system will use the command name (usually java) instead.
syslogFacility null Number This data is logged in the syslog compatibility field SYSLOG_FACILITY.

Example

log4j2.xml

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO" packages="de.bwaldvogel.log4j">
    <Appenders>
        <Console name="console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
        </Console>
        <SystemdJournal name="journal" logStacktrace="true" logSource="false" />
    </Appenders>
    <Loggers>
        <Root level="INFO">
            <AppenderRef ref="console" />
            <AppenderRef ref="journal" />
        </Root>
    </Loggers>
</Configuration>

This will tell Log4j to log to systemd journal as well as to stdout (console). Note that a layout is optional for SystemdJournal. This is because meta data of a log event such as the timestamp, the logger name or the Java thread name are mapped to systemd-journal fields and need not be rendered into a string that loses all the semantic information.

YourExample.java

import org.apache.logging.log4j.*;

class YourExample {

    private static Logger logger = LogManager.getLogger(YourExample.class);

    public static void main(String[] args) {
        ThreadContext.put("MY_KEY", "some value");
        logger.info("this is an example");
    }
}

Running this sample class will log a message to journald:

Systemd Journal

# journalctl -n
Okt 13 21:26:00 myhost java[2370]: this is an example

Use journalctl -o verbose to show all fields:

# journalctl -o verbose -n
Di 2015-09-29 21:07:05.850017 CEST [s=45e0…;i=984;b=c257…;m=1833…;t=520e…;x=3e1e…]
    PRIORITY=6
    _TRANSPORT=journal
    _UID=1000
    _GID=1000
    _CAP_EFFECTIVE=0
    _SYSTEMD_OWNER_UID=1000
    _SYSTEMD_SLICE=user-1000.slice
    _MACHINE_ID=4abc6d…
    _HOSTNAME=myhost
    _SYSTEMD_CGROUP=/user.slice/user-1000.slice/session-2.scope
    _SYSTEMD_SESSION=2
    _SYSTEMD_UNIT=session-2.scope
    _BOOT_ID=c257f8…
    THREAD_NAME=main
    LOG4J_LOGGER=de.bwaldvogel.log4j.SystemdJournalAppenderIntegrationTest
    _COMM=java
    _EXE=/usr/bin/java
    MESSAGE=this is a test message with a MDC
    CODE_FILE=SystemdJournalAppenderIntegrationTest.java
    CODE_FUNC=testMessageWithMDC
    CODE_LINE=36
    THREAD_CONTEXT_MY_KEY=some value
    SYSLOG_IDENTIFIER=log4j2-test
    LOG4J_APPENDER=Journal
    _PID=8224
    _CMDLINE=/usr/bin/java …
    _SOURCE_REALTIME_TIMESTAMP=1443553625850017

Note that the ThreadContext key-value pair {"MY_KEY": "some value"} is automatically added as field with prefix THREAD_CONTEXT.

You can use the power of systemd journal to filter for interesting messages. Example:

journalctl CODE_FUNC=testMessageWithMDC THREAD_NAME=main will only show messages that are logged from the Java main thread in a method called testMessageWithMDC.

Contributing

We are happy to receive pull-requests if you want to contribute code to the project.

If you want to thank the author for this library or want to support the maintenance work, we are happy to receive a donation.

Donate

Related Work

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].