How to Log Multiline Messages as a Single Entry Using Linux Logger Command


2 views

When working with the Linux logger command, many developers encounter an unexpected behavior where multiline messages get split into separate log entries. This happens because:

echo -e "line1\nline2" | logger

Produces two separate syslog entries instead of one combined message. This behavior stems from how logger processes input by default.

The most reliable method is to use process substitution to pass the entire message as a single argument:

logger "$(echo -e "line1\nline2")"

Or for reading from a file:

logger "$(cat multiline.txt)"

Using printf

printf "line1\nline2" | logger --size 2048

The --size parameter helps ensure large messages aren't truncated.

Here Document Syntax

logger <<EOF
This is line 1
This is line 2
EOF

For messages containing special characters or formatting:

logger -t "MYAPP" "$(printf "Alert:\n• Item 1\n• Item 2")"

When logging to systemd journal, you can preserve multiline format with:

echo -e "line1\nline2" | systemd-cat -t myapp
  • Always quote your message variables to prevent word splitting
  • Consider using a custom tag (-t) for better log filtering
  • For very large messages, check your syslog daemon's message size limits

When working with the Linux logger command, many developers encounter an unexpected behavior: multiline messages get split into separate log entries. This occurs because the system logger typically treats newline characters as entry delimiters.

The standard approach:

echo -e "first line\\nsecond line" | logger

Produces two separate log entries in /var/log/syslog:

May 15 10:00:00 hostname logger: first line
May 15 10:00:00 hostname logger: second line

Method 1: Using Command Substitution

Replace newlines with a visible separator:

logger "$(echo -e "line1\\nline2" | tr '\\n' ' ')"

Method 2: With printf and Newline Escaping

Properly escape newlines for single-entry output:

printf '%s\\n' "multi" "line" "message" | logger --id=$$

Method 3: Using Here Documents

A cleaner approach for complex messages:

logger <<END
This is a multiline
message that will appear
as a single log entry
END

For better log management:

logger -t MYAPP --id=$$ "Multi-part message: $(echo -e "part1\\npart2" | sed ':a;N;$!ba;s/\\n/ | /g')"

Verify with:

tail -f /var/log/syslog | grep logger

Or for systemd systems:

journalctl -f -t logger
  • Be mindful of maximum log entry size limits (typically 8KB)
  • Consider using structured logging formats like JSON
  • For complex applications, investigate dedicated logging libraries