Creating Custom Logs with logger
Last Updated :
15 Nov, 2025
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:
- 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:
Command:
sudo tail -n 5 /var/log/syslog
Output:
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
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:
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):
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:
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:
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
| Option | Description | Example |
|---|
| -t <tag> | Adds a custom tag to identify messages | logger -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 file | logger -f /var/log/custom.log |
| -i | Includes the process ID (PID) in the log | logger -i "Process started" |
| -s | Logs to both syslog and the standard error stream | logger -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
| Feature | logger | echo |
|---|
| Purpose | Writes messages to system logs | Displays output to terminal |
| Persistence | Logs are stored in /var/log/syslog or /var/log/messages | Output disappears after session ends |
| Use Case | Logging events for auditing and monitoring | Temporary display or debugging |
| Integration | Works with syslog/journald | Independent command |
Explore
Linux/Unix Tutorial
5 min read
Getting Started with Linux
Installation with Linux
Linux Commands
Linux File System
Linux Kernel
Linux Networking Tools
Linux Process
Linux Firewall
Shell Scripting & Bash Scripting