The Importance of System Logs
In a Linux environment, everything that happens—from a user plugging in a USB drive, to a web server crashing, to a hacker failing an SSH password guess—is recorded in a central system log file (typically located at /var/log/syslog or managed by systemd-journald). System administrators rely on these logs to troubleshoot critical hardware and software failures.
If you are writing a custom bash script to automate a massive nightly backup, and that script encounters a critical error at 3:00 AM, printing an error message to the terminal screen is useless, because no human is awake to see it. The script will fail silently.
To fix this, you need your custom bash scripts to inject their own custom error messages directly into the official Linux system log alongside the actual kernel and hardware errors. You can do this effortlessly using the logger command.
Step 1: Sending a Basic Log Message
The simplest way to use the logger command is to simply type it, followed by the text string you want to inject into the master log.
logger "Backup script started successfully."
The command will execute silently. However, if a system administrator uses the tail command to check the bottom of the system log (sudo tail /var/log/syslog), they will see an official, timestamped entry containing your exact message, attributed to your specific username.
Step 2: Tagging Your Log Entries
If your message just says “Error: File not found,” a system administrator reading the log will have absolutely no idea which specific program generated that error.
To make your logs professional and searchable, you must use the -t (tag) flag. This allows you to tag the message with the name of your specific script or application.
logger -t NightlyBackup "Error: The target database file could not be found."
Now, the log entry will clearly read: Aug 22 03:00:15 server-01 NightlyBackup: Error: The target database file could not be found.
An administrator can easily search the entire log specifically for the “NightlyBackup” tag using grep: grep "NightlyBackup" /var/log/syslog.
Step 3: Setting the Priority Level
Linux logs are sorted by priority. A harmless informational message should not trigger the same alarm bells as a critical hardware failure. You can assign a priority level to your custom messages using the -p (priority) flag.
The priority flag requires a facility (usually user for custom scripts) and a severity level (such as info, warning, err, or emerg).
To log a standard, harmless update:
logger -t NightlyBackup -p user.info "Database backup completed."
To log a catastrophic failure that should alert the IT department immediately:
logger -t NightlyBackup -p user.err "CRITICAL FAILURE: Remote storage disconnected!"
Step 4: Logging the Output of Another Command
The true power of logger is its ability to capture the output of other commands using shell piping (|).
If you want to ping a remote server to check its uptime, and you want the result of that ping saved directly into the system log, you can pipe the output of the ping command straight into logger.
ping -c 1 192.168.1.50 | logger -t PingTest
This will inject the actual millisecond response time of the ping command directly into the syslog, creating a permanent, timestamped historical record of your network latency.