A log file is a file that contains a set of records (or list of events) which have been logged by the system itself. Using the log files, the sysadmin can track what has happened on a particular day or time. Mostly log files are used by the admins for troubleshooting purpose. The log files are generated automatically and saved under a common directory - /var/log/
. We can also manually add messages to Linux system log files. For instance, after setting up a Log server, you might want to check if the log server is properly functioning or not. To do so, we can manually add some entries in the log files to test the log server. This is where are logger command comes in handy.
Table of Contents
Add Messages to Linux System Log Files using Logger Command
The logger command is part of the util-linux package, so don't bother installing it. Let me show you some logger
command examples.
Adding entries manually to the system log files is absolutely easy! The typical usage of logger
command is:
logger <message-to-add>
Example:
$ logger Welcome to OSTechNix
The above command will add the entry 'Welcome to OSTechNix' to the system log files.
Let us verify if the message has been added or not using tail
command:
$ tail -l /var/log/syslog
Sample Output:
Jan 31 07:19:23 ubuntuserver systemd[1705]: Listening on REST API socket for snapd user session agent. Jan 31 07:19:23 ubuntuserver systemd[1705]: Listening on GnuPG network certificate management daemon. Jan 31 07:19:23 ubuntuserver systemd[1705]: Listening on GnuPG cryptographic agent and passphrase cache. Jan 31 07:19:23 ubuntuserver systemd[1705]: Reached target Sockets. Jan 31 07:19:23 ubuntuserver systemd[1705]: Reached target Timers. Jan 31 07:19:23 ubuntuserver systemd[1705]: Reached target Basic System. Jan 31 07:19:23 ubuntuserver systemd[1]: Started User Manager for UID 1000. Jan 31 07:19:23 ubuntuserver systemd[1705]: Reached target Default. Jan 31 07:19:23 ubuntuserver systemd[1705]: Startup finished in 298ms. Jan 31 07:33:14 ubuntuserver sk: Welcome to OSTechNix
As you see in the output, the given message has been added to the syslog file.
Heads Up:
Different Linux operating systems stores log messages in different files. I suggest you to look into the /var/log/
directory to know in which files the logs are stored. In RPM-based systems like CentOS, the general log messages will be stored in /var/log/messages
file instead of /var/log/syslog
file.
You can also add the messages by enclosing them in single or double quotes.
$ logger "Welcome to OSTechNix blog"
Or,
$ logger 'Howdy, Welcome to OSTechNix blog'
Check if the entries have been added to the syslog file using tail
command:
$ tail -l /var/log/syslog [...] Jan 31 07:40:02 ubuntuserver sk: Welcome to OSTechNix blog Jan 31 07:40:31 ubuntuserver sk: Howdy, Welcome to OSTechNix blog
Add logger PID to system logs
We can even log the PID of the logger process with each line using -i
flag like below.
$ logger -i 'Howdy, Welcome to OSTechNix blog'
Verify the log messages using tail
command:
$ tail -l /var/log/syslog [...] Jan 31 07:49:22 ubuntuserver sk[1879]: Howdy, Welcome to OSTechNix blog
Here, 1879
is the process ID of logger.
Add tags to messages
As you may noticed in the above outputs, the newly added entries are tagged with the currently logged-in username i.e. sk. The default tag is the name of the user logged in on the terminal. We can, however, mark every line to be logged with a specific tag using -t
flag.
$ logger -i -t ostechnix 'Howdy, Welcome to OSTechNix blog'
Check the log entries:
$ tail -l /var/log/syslog [...] Jan 31 07:54:02 ubuntuserver ostechnix[1881]: Howdy, Welcome to OSTechNix blog
See? the last entry is tagged with ostechnix and its process ID is 1881
.
Add messages from a file to the log files
It is also possible to add entries from a file to our system log files.
Let us create a sample text file.
$ echo "This is test file" > file.txt
$ cat file.txt This is test file
Now add the contents of the file.txt
to system log files using command:
$ logger -f file.txt
Check log files:
$ tail -l /var/log/syslog [...] Jan 31 08:43:06 ubuntuserver sk: This is test file
If he the input file has any empty lines, you can exclude them from being added to the log files using -e
flag like below.
$ logger -e -f file.txt
Log messages with priority
It is possible to log messages with a given priority.
$ logger "Welcome To OSTechNix" --priority user.warning
Default priority is 'user.notice
'. See logger man page to know all available priority options.
Take input from 'stdin' and messages to system logs
We can pipe the input from stand input and push them to the syslog using command:
$ echo "Welcome to OSTechNix" | logger
Send messages to remote log server
To send the messages to a remote syslog server running at a specific port, run:
$ logger "Welcome to OSTechNix" --server <hostname-or-ip> --port <port-no>
Or,
$ logger "Welcome to OSTechNix" -n <hostname-or-ip> -p <port-no>
The default port no is 514
.
Limit the size of the messages
We can set the maximum permitted message size using --size
flag.
$ logger --size 5 abcdefghijklmnopqrswxyz
Check the log message size:
$ tail -l /var/log/syslog [...] Jan 31 09:09:02 ubuntuserver sk: abcde
The default is 1KiB
characters.
For more details check man pages.
$ man logger
Conclusion
In this brief tutorial, we discussed what is logger command, how you can use logger command to manually add messages to Linux system log files to test if a log server is properly working or not.
Suggested Read: