When developing automation scripts, monitoring cron jobs, or administering multi-user servers in Linux, writing runtime error messages to arbitrary text files in /tmp quickly leads to lost logs and fragmented auditing. Centralised system logging is a foundational pillar of UNIX architecture: daemons, kernel subsystems, and administrative events write directly to system loggers like rsyslog, syslog-ng, or the modern systemd journal. When monitoring enterprise production environments, your custom shell scripts and administrative routines should follow this exact same standardised logging pipeline.
The definitive utility for inserting user-defined log entries into system journals is logger. Included by default in virtually every Linux distribution as part of the util-linux package, logger provides a straightforward shell interface to the system log module. Whether auditing automated database backups, recording hardware temperature triggers, or debugging daemon failures, logger ensures all events are timestamped, indexed, and queryable using standard utilities like journalctl. Here is a comprehensive guide on how to use the logger command in Linux.
Understanding Syslog Facilities and Severities
Before executing commands, it is essential to understand how Linux categorises log telemetry. The syslog protocol defines two key metrics for every event:
- Facility: Identifies the source subsystem submitting the message (e.g.,
auth,cron,daemon,kern,local0throughlocal7, oruser). By default, user-invoked logger commands use theuserfacility. - Severity (Priority): Represents the urgency level of the message. The eight standardised syslog severity levels, ordered from highest urgency to lowest, are:
emerg(System is unusable)alert(Action must be taken immediately)crit(Critical conditions)err(Error conditions)warning(Warning conditions)notice(Normal but significant condition)info(Informational message; default)debug(Debug-level message)
Basic Usage: Sending Your First Log Entry
To send an immediate text message to the system log, invoke logger followed by your message string:
logger "Routine maintenance backup script initiated successfully."
To verify that the log entry was recorded by the system journal, query journalctl:
journalctl -e -n 5
You will see your message printed with an exact system timestamp, the hostname, and your logged-in username.
Adding Custom Tags for Easy Filtering with -t
When multiple automated scripts run concurrently, inspecting raw log streams becomes difficult without structured tags. The -t switch allows you to assign a custom prefix identifier (tag) to every message:
logger -t DB_BACKUP "Database dump completed: postgres_prod.sql.gz (1.4 GB)"
You can then filter system logs specifically for your custom tag using journalctl or grep:
journalctl -t DB_BACKUP
Setting Priority and Facility with -p
To ensure system monitors (like Grafana, Datadog, or Fail2ban) recognise errors accurately, assign appropriate facilities and severities using the -p flag in facility.severity format:
# Record a critical local error
logger -p local0.crit -t RAID_MONITOR "Disk /dev/sdb reported uncorrectable sector read failure!"
# Record a standard informational notice
logger -p local0.notice -t DEPLOY_BOT "Deployment v2.4.1 committed to container cluster"
Essential logger Command Flags
| Flag / Option | Operational Description | Example Command |
|---|---|---|
-t <tag> |
Prepends a custom process identifier tag to the entry. | logger -t CRON_JOB "Job finished" |
-p <priority> |
Assigns a specific facility.severity rating. |
logger -p daemon.warn "High memory load" |
-i |
Logs the Process ID (PID) of the logger process. | logger -i -t DISK_CHECK "Checking mounts" |
-s |
Outputs the log entry to stderr in addition to system log. |
logger -s "Interactive console warning" |
-f <file> |
Logs the entire contents of a specified text file. | logger -t BUILD_LOG -f /tmp/build_summary.txt |
Integrating logger into Production Bash Automation
The true power of logger emerges when integrated into shell scripts to trap errors and log pipeline health dynamically:
#!/usr/bin/env bash
TAG="SYNC_SERVICE"
TARGET_DIR="/mnt/backup_nas"
logger -t "$TAG" -p local1.info "Starting nightly synchronization..."
if rsync -avz /var/www/html "$TARGET_DIR"; then
logger -t "$TAG" -p local1.notice "Rsync synchronization completed successfully."
else
logger -i -t "$TAG" -p local1.err "CRITICAL: Rsync failed with exit code $?!"
exit 1
fi
By piping script outputs and capturing exit codes with logger, system administrators gain bulletproof, centralised visibility across their entire Linux fleet.