logger Command Examples in Linux

logger command is used to log messages in the system log or syslog. You can log a messages string on the command line or provide a file as input that can contain the message to be logged.

To view the available switches for the logger command:

# logger --help

Usage:
 logger [options] [message]

Options:
 -T, --tcp             use TCP only
 -d, --udp             use UDP only
 -i, --id              log the process ID too
 -f, --file [file]     log the contents of this file
 -h, --help            display this help text and exit
 -S, --size [num]      maximum size for a single message (default 1024)
 -n, --server [name]   write to this remote syslog server
 -P, --port [port]     use this port for UDP or TCP connection
 -p, --priority [prio] mark given message with this priority
 -s, --stderr          output message to standard error as well
 -t, --tag [tag]       mark every line with this tag
 -u, --socket [socket] write to this Unix socket
 -V, --version         output version information and exit

logger command examples

1. To log the message to standard error and system logs

# logger -s "This is a test message" 

2. To log to message to the specified file

# logger -f file "This is a test message"

3. To log the message with specified priority

# logger -p 1 "This is a test message"

4. To mark every line with specified tag

# logger -t TAG "This is a test message"

5. To allow the message to start with a hyphen

# logger -- "This is a test message"
Related Post