Bootstrap FreeKB - Linux Commands - logger (add records to a log file)
Linux Commands - logger (add records to a log file)

Updated:   |  Linux Commands articles

The logger command can be used to manually add a record to a log file. For example, to add "Example message" to a log file.

[root@server1 ~]# logger Example message

 

"Example message" is in the /var/log/messages file. Syslog, rsyslog, or syslog-ng will probably be confirgred to add messages to the /var/log/messages file.

[root@server1 ~]# tail -1 /var/log/messages
Nov 21 14:54:35 root root: Example message

 

 


PID

The -i (or --id) option will record the PID.

[root@server1 ~]# logger -i Example message

 

In this example, PID 650 is recorded.

[root@server1 ~]# tail -1 /var/log/messages
Nov 21 14:57:29 root root[650]: Example message

 


Standard error

The -s (or --stderr) option will display the message in the shell and also write the message to the log file.

[root@server1 ~]# logger -s Example message
root: Example message

 


Redirect from a file

The -f (or --file) option can be used to use the contents of a file as the message. For example, let's say test.file contains the text "Hello world".

[root@server1 ~]# logger -f test.file

 

"Hello world" is listed in the log.

[root@server1 ~]# tail -1 /var/log/messages
Nov 21 14:57:29 root root: Hello world

 


Priority

The -p (or --priority) option can be used to specify the priority.  In this example, the priority of the message is notice.

[root@server1 ~]# logger -p notice Example message

 


Tag

The -t (or --tag) option can be used to add a tag to the message. In this example, tag XYZ is included.

[root@server1 ~]# logger -t XYZ Example message

 

In this example, tag XYZ is included in the log.

[root@server1 ~]# tail -1 /var/log/messages
Nov 21 14:57:29 root XYZ: Hello world

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter f73605 in the box below so that we can be sure you are a human.