The logger command provides an easy way to add messages to the /var/log/syslog file from the command line or from other files. Credit: Bureau of Land Management Oregon and Washington The Linux logger command provides an easy way to add log files to /var/log/syslog — from the command line, from scripts, or from other files. In today’s post, we’ll take a look at how it works. How easy is easy? This easy. Just type logger on the command line and your message will be added to the end of the /var/log/syslog file. $ logger comment to be added to log $ tail -1 /vvar/log/syslog May 21 18:02:16 butterfly shs: comment to be added to log Command output You can also add the output from commands by enclosing the commands in backticks. $ logger `who` $ tail -1 /var/log/syslog May 21 18:02:43 butterfly shs: shs pts/0 2018-05-21 15:57 (192.168.0.15) Content from a file 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. $ cat msg Backups to off-site facility will run this coming weekend. System availability will not be affected. $ logger -f msg $ tail -2 /var/log/syslog May 21 18:06:01 butterfly shs: Backups to off-site facility will run this coming weekend. May 21 18:06:01 butterfly shs: System availability will not be affected. Using logger in scripts You can add logger commands to scripts to make it easier to track the completion of important tasks. $ grep logger /bin/runme logger "$0 completed at `date`" $ sudo runme $ tail -1 /var/log/syslog May 21 17:57:36 butterfly shs: ./runme completed at Mon May 21 17:57:36 EDT 2018 Limiting the size of logger entries If you’re concerned about how much data 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 this example, the size is artificially small to make a point. $ logger --size 10 12345678901234567890123456789012345678901234567890 $ tail -1 /var/log/syslog May 21 18:18:02 butterfly shs: 1234567890 This option works differently than you might expect in that, given input that includes blanks, it will constrain the content on a per-line basis rather than an overall length basis. $ logger --size 5 `date` $ tail -5 /var/log/syslog May 22 08:35:51 butterfly shs: May May 22 08:35:51 butterfly shs: 22 May 22 08:35:51 butterfly shs: 08:35 May 22 08:35:51 butterfly shs: EDT May 22 08:35:51 butterfly shs: 2018 Don’t be misled by these simple examples. The –size option is generally used to limit large amounts of text. The default maximum is 1KiB (1024 bytes). Ignoring blank lines The -e option allows you to avoid dumping empty lines into your log file. They will simply be ignored. Note, however, that a line that contains blanks will not be considered blank. $ cat appts Appts Other options The logger tool offers others as well — such as writing to a log on another server using -n or --no-act for testing. Check your man page for more details. Related content how-to Getting started on the Linux (or Unix) command line, Part 4 Pipes, aliases and scripts make Linux so much easier to use. By Sandra Henry-Stocker Nov 27, 2023 4 mins Linux how-to Getting started on the Linux (or Unix) command line, Part 3 Our Linux cheat sheet includes some of the most commonly used commands along with brief explanations and examples of what the commands can do. By Sandra Henry Stocker Nov 21, 2023 6 mins Linux how-to Getting started on the Linux (or Unix) command line, Part 2 Commands that provide help are essential. Here's a look at some of the help you can get from the Linux system itself. By Sandra Henry Stocker Nov 20, 2023 5 mins Linux how-to Getting started on the Linux (or Unix) command line, Part 1 This series of posts will help Linux/Unix newbies to feel comfortable on the command line. By Sandra Henry Stocker Nov 16, 2023 8 mins Linux Podcasts Videos Resources Events NEWSLETTERS Newsletter Promo Module Test Description for newsletter promo module. Please enter a valid email address Subscribe