smalllinux

Leveraging the Linux Logger

In this article I want to visit a “not used enough” Linux tool called logger.  As with managing any system, you want to accurately annotate historical events and adding entries into the system log file is a great way to accomplish this.  You can view the system log file with this command:

awalding@aw-vb:~$ cat /var/log/syslog | more

Be prepared for many pages!

What the logger command does is it provides an easy way to add to /var/log/syslog contents either from the command line, from your scripts, or from other files.  Just type logger {the message you want to add} on the command line and your message will be added to the end of the /var/log/syslog file.  Let’s try this:

awalding@aw-vb:~$ logger This is my first comment to add to the log!

Now display that using the following command:

awalding@aw-vb:~$ tail -1 /vvar/log/syslog

Here is my example:

2022 04 23 10 06 29

Adding Command Output to the Log

You can use logger to place the output of commands into the log as well by placing the command in single quotes or backticks.  Let’s add the ‘w’ command output to the log (you can find out more about the w command by looking at the man page for it):

awalding@aw-vb:~$ logger 'w'

Let’s look at the result with the same tail command (you may want to adjust the number of lines):

2022 04 23 10 12 10

Adding Content From a File to the Log

The contents of text files can be added by using the -f option. Put the name of the file to be added to the log following the -f option as shown below.  Let’s say I have a little text file called message.txt:

2022 04 23 10 19 16

Note it is 5 lines.

Now I can add this file to the log:

awalding@aw-vb:~$ logger  -f message.txt

2022 04 23 11 58 48

I went one extra line just for fun. 

You Can Add Logger Commands to Scripts

Know that in your script files, you can add logger commands to add a line of information like this:

logger "$0 completed system scan at 'date'"

Perhaps some of you can share great scripts that you use logger in below in the comments.

How Do I Limit the Logger Entry Size?

If you’re concerned about how much information will be added to your log file, especially if you’re dumping content from a file, you can use the –size option to limit it. In the following example I deliberately limit the size even though my text is longer.

awalding@aw-vb:~$ logger --size 18 Only the first eighteen characters of my text will be logged in each line.

2022 04 23 12 11 40

The default maximum is 1KiB (1024 bytes).

Avoiding Adding Blank Lines to Your Log

The -e option allows you to avoid placing/adding empty lines into your log file. They will simply be ignored. Note, however, that a line that contains blanks will not be considered blank.

A Couple of Other Options

The logger tool offers a couple of other options as well.  One such option is to a log on another system/server using -n or –no-act for testing. Check your man page for more details.

What other favorite options do you leverage when using logger?

Summing Up

In this article I took a little peek at leveraging the logger tool in Linux.  This was not all encompassing, and I am sure readers and visitors will have some additional commands they will suggest in comments.  We all welcome those pearls of wisdom on this subject.

I hope you find this article and its content helpful.  Comments are welcomed below.  If you would like to see more articles like this, please support us by clicking the patron link where you will receive free bonus access to courses and more, or simply buying us a cup of coffee!, and all comments are welcome!

Leave a Comment

Contact Us Here


Please verify.
Validation complete :)
Validation failed :(
 
Your contact request has been received. We usually respond within an hour, but please be patient. We will get back to you very soon.
Scroll to Top