Open In App

Creating Custom Logs with logger

Last Updated : 15 Nov, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The logger command is a shell interface to the syslog system logging utility, allowing users to add custom messages to system logs. It helps record script activities, application status, or system events for later analysis and auditing.

  • To record custom messages from users, applications, or scripts in system logs.
  • To track automation or scheduled job execution outcomes.
  • To log errors and informational messages for debugging scripts.
  • To maintain audit trails of user or system activities.
  • To integrate custom log data with centralized logging systems like rsyslog or journald.

Common Syntax Examples

logger "System backup completed successfully."
logger -p local0.info "Custom log from backup script."
logger -t BackupScript "Backup process started at $(date)"

Syntax of logger Command

logger [options] [message]

Installation and Setup

The logger command is usually pre-installed on most Linux distributions as part of the util-linux package.

Step 1: Verify if logger is Installed

Command:

which logger

Output:

logger
  • If it shows a path like /usr/bin/logger, it’s already installed.

Step 2: Install logger (if not available)

For Debian/Ubuntu-based systems:

Command:

sudo apt update
sudo apt install util-linux

For RedHat/CentOS/Fedora systems:

sudo yum install util-linux

Step 3: Verify Installation

logger --version

Basic Usage and Examples of logger Command

The following examples demonstrate how to create and manage custom log entries using logger.

1. Creating a Simple Log Message

Creates a custom message entry in the system log.

Command:

logger "System backup completed successfully."

Output:

rr
  • Check the entry using:

Command:

sudo tail -n 5 /var/log/syslog

Output:

hh

Explanation:

  • Logs the message “System backup completed successfully.” into /var/log/syslog.
  • Helps record user-defined messages for auditing or tracking.
  • Useful in cron jobs and automation scripts.

2. Adding a Tag to Log Messages

Adds a custom tag to easily identify log entries.

Command:

logger -t BackupScript "Backup started at $(date -d '2025-11-12 10:30:00')"

Output (check with):

sudo tail -n 5 /var/log/syslog
back

Explanation:

  • -t adds a tag (here: “BackupScript”) to the log entry.
  • Makes it easier to find messages related to specific scripts or applications.
  • Useful for categorizing logs when multiple scripts are writing to syslog.

3. Logging with Priority and Facility

Defines the severity level and source (facility) of the log.

Command:

logger -p local0.info "Network monitoring service started successfully."

Output:

net

Explanation:

  • -p specifies priority and facility (e.g., local0.info, user.err).
  • The facility indicates the log source category (e.g., kernel, user, mail).
  • The priority defines message importance (info, warn, err, crit).

4. Logging Output from a Command

Logs the output of another command directly into system logs.

Command:

df -h | logger -t DiskUsage

Output (in syslog):

usage

Explanation:

  • Captures output from df -h and writes it to syslog.
  • Useful for recording system statistics or status periodically.
  • Ideal for automation and monitoring scripts.

5. Writing Logs to a Custom File

Redirects logs to a specific file instead of the default system log.

Command:

logger -t CustomLog "Custom logging started successfully."

Output:

  • Check logs with:
custom

Explanation:

  • -f writes log messages to a specified file.
  • Allows you to create and manage dedicated log files for applications or scripts.
  • Useful for maintaining separate logs without cluttering system logs.

6. Logging Script Events

Used inside shell scripts to record actions or errors.

Script Example:

#!/bin/bash
logger -t BackupScript "Backup started at $(date)"
tar -czf /backup/home_backup.tar.gz /home 2>/dev/null
if [ $? -eq 0 ]; then
logger -t BackupScript "Backup completed successfully at $(date)"
else
logger -p user.err -t BackupScript "Backup failed at $(date)"
fi

Explanation:

  • Automatically logs important events during script execution.
  • Helps track success or failure of automated tasks.
  • Essential for debugging or post-execution review.

7. Checking Logged Messages

Verifies where logger messages are stored.

Command:

sudo tail -n 10 /var/log/syslog

Output:

tail
  • or for Red Hat systems:
sudo tail -n 10 /var/log/messages

Explanation:

  • Displays the most recent log entries added by logger.
  • Confirms successful logging and log file location.
  • Helps verify correct facility and priority levels.

Common Options of logger Command

OptionDescriptionExample
-t <tag>Adds a custom tag to identify messageslogger -t BackupScript "Started..."
-p <priority>Defines facility and severity (e.g., user.err, local0.info)logger -p local0.warn "Low disk space"
-f <file>Logs output from a specified filelogger -f /var/log/custom.log
-iIncludes the process ID (PID) in the loglogger -i "Process started"
-sLogs to both syslog and the standard error streamlogger -s "Critical error occurred"

Practical Use Cases

  • System Monitoring: Log when system resources reach critical levels.
  • Automation Scripts: Track start and end times of cron jobs.
  • Custom Application Logging: Integrate logger into applications for event tracking.
  • Security Auditing: Log failed operations, configuration changes, or access attempts.
  • Troubleshooting: Combine with commands like grep and tail for targeted analysis.

Viewing and Managing Custom Logs

Viewing and managing custom logs allows users to monitor, analyze, and troubleshoot application or system activities by tracking specific events and messages in real time.

View All logger Entries:

sudo grep "BackupScript" /var/log/syslog

View Real-Time Updates:

sudo tail -f /var/log/syslog

Difference Between logger and echo

Featureloggerecho
PurposeWrites messages to system logsDisplays output to terminal
PersistenceLogs are stored in /var/log/syslog or /var/log/messagesOutput disappears after session ends
Use CaseLogging events for auditing and monitoringTemporary display or debugging
IntegrationWorks with syslog/journaldIndependent command

Article Tags :

Explore