How to Send Messages Directly to systemd Journal from Command Line (Modern logger Alternative)


3 views

With the widespread adoption of systemd in modern Linux distributions, logging has fundamentally changed from the traditional syslog approach. The journald daemon now handles system logging in most contemporary Linux systems.

The most direct method is using systemd-cat, which is part of systemd itself. It provides several ways to interact with the journal:

# Basic message
echo "Test message" | systemd-cat

# With priority (similar to syslog levels)
echo "Critical error" | systemd-cat -p emerg

# Tagging messages
echo "Database connection failed" | systemd-cat -t myapp

After sending messages, verify they're in the journal:

# View recent messages
journalctl -n 5

# Filter by tag
journalctl -t myapp

# Show messages from last 10 minutes
journalctl --since "10 minutes ago"

For more advanced use cases, you can use the native journal API via sd-journal:

# C example using libsystemd
#include <systemd/sd-journal.h>

int main() {
    sd_journal_print(LOG_NOTICE, "Hello from C application");
    return 0;
}

Many systems still maintain compatibility with the traditional logger command through socket forwarding:

# Check if logger is forwarding to journald
logger "Test message"
journalctl -n 1 | grep "Test message"

Journald provides several powerful features worth exploring:

# Structured logging with fields
echo "MESSAGE=Disk full" | systemd-cat
echo "DISK_USAGE=95%" | systemd-cat

# Binary data logging
dd if=/dev/urandom bs=16 count=1 | systemd-cat --binary

By default, journald stores logs in a volatile location. For persistent storage:

# Create persistent journal directory
mkdir -p /var/log/journal
systemctl restart systemd-journald

Access control for journald is handled via Unix policies:

# Allow specific user to read logs
usermod -aG systemd-journal username

# Check current permissions
ls -l /run/systemd/journal/

In traditional Linux systems, the logger command was the standard way to send messages to syslog. However, with the widespread adoption of systemd, logging has largely transitioned to journald. While logger still works in many cases through compatibility layers, there are more direct approaches for modern systems.

The systemd-cat command is the most direct method to log to journald:

echo "This goes directly to journald" | systemd-cat

You can verify the message was logged with:

journalctl -n 5

To include priority levels (similar to syslog levels):

echo "Critical error occurred" | systemd-cat -p emerg

Available priority levels include:

  • emerg (0)
  • alert (1)
  • crit (2)
  • err (3)
  • warning (4)
  • notice (5)
  • info (6)
  • debug (7)

For systems where you need both journald and traditional syslog compatibility:

logger --journald "This message goes to both syslog and journald"

Journald supports structured logging with key-value pairs:

systemd-cat -t myapp -p info <

To see entries from your current session:

journalctl -b -n 10 --grep="myapp"

Or filter by priority:

journalctl -p err -t myapp

To ensure all logger messages are properly captured by journald, check your system's configuration:

cat /etc/systemd/journald.conf

Key parameters to consider adjusting:

  • Storage=persistent
  • Compress=yes
  • MaxFileSec=1month