Networking/Computing Tips/Tricks

Rate this content:
5 of 5 - 1 votes
Thank you for rating this article.

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!

Add comment

Submit

Did you learn something?
Did I save you time? 

Buy me a coffeeBuy me a coffee!

Find by Tag

5G Networks 6LoWLAN 6LoWPAN 802.11 802.11ah 802.11ax 802.11ay 802.11az ACL Addressing Analysis Ansible Architecture ARP Assessment AToM Backup Bandwidth BGP Bibliography Biography Briefings CBRS CellStream Cellular Central Office Cheat Sheet Chrome Cisco Clock Cloud Computer Consulting CPI Data Center Data Networking Decryption DHCPv4 DHCPv6 Display Filter DNS Documentation ECMP EIGRP Ethernet Flipping the Certification Model Follow Me Fragmentation Git GNS3 Google GQUIC Hands-On History Home Network HTTPS ICMP ICMPv6 IEEE 802.11p IEEE 802.15.4 In A Day Internet IOS Classic IoT IPv4 IPv6 L2 Switch L2VPN L3VPN LDP Learning Services Linux LLN Logging LoL M-BGP MAC MAC OSx Macro Microsoft mininet Monitoring Monitor Mode MPLS Multicast Name Resolution Netflow NetMon netsh Networking Network Science nmap Npcap nslookup Online Learning Online School OpenFlow OSPF OSPFv2 OSPFv3 OSX Parrot pcap pcap-ng PIM Ping Policy POTS POTS to Pipes PPP Profile Profiles Programming Project Management Python QoS QUIC Requirements RFC RIP Routing RPL RSVP Rural SAS SDN Security Self Certification Service Provider Sharepoint Small Business Smartport SONET SSH SSL Subnetting T-Shark TCP TCP/IP Telco Telecom 101 Telecommunications Telephone Telnet Terminal TLS Tools Traceroute Traffic Analysis Traffic Engineering Training Travel Troubleshooting Tunnel Utility Video Virtualbox Virtualization Voice VoIP VXLAN Webex Wi-Fi Wi-Fi 4 Wi-Fi 5 Wi-Fi 6 Wi-Fi 6/6E Windows Wireless Wireless 5G Wireshark Wireshark Tip WLAN ZigBee Zoom

Twitter Feed