1. Home
  2. Tools / Utilities
  3. Terminal Commands
  4. The logger Command: Logging Messages to the System Log

The logger Command: Logging Messages to the System Log

The logger command is a versatile tool used in Unix-like operating systems to send messages to the system log (commonly managed by syslogd or rsyslogd). It enables users, scripts, and applications to log custom messages, making it an essential utility for debugging, monitoring, and system administration.


Key Features

  1. Sends Messages to the System Log: The primary function of logger is to write messages to the system log, which can be accessed via log files like /var/log/syslog, /var/log/messages, or custom locations defined in the syslog configuration.
  2. Customizable Logging Options: It allows specifying log levels, facilities, and tags to categorize and filter messages efficiently.
  3. Script Integration: Integrates seamlessly into scripts to provide contextual logging for automation or error tracking.

Basic Syntax

logger [options] [message]
  • [message]: The message to log. If no message is provided, logger reads input from standard input.

Common Options

OptionDescription
-p prioritySets the log priority (facility.level). Example: user.info, auth.error.
-t tagAdds a tag to the log message, typically the name of the application or script.
-iIncludes the process ID (PID) of the logger command in the log message.
-f fileLogs the content of the specified file.
--stderrOutputs the message to standard error as well as the system log.
--idAppends a numerical identifier to the message.

Examples

1. Log a Simple Message

logger "System maintenance scheduled at midnight."
  • This logs a simple message, viewable in /var/log/syslog or equivalent.

2. Log with a Custom Tag

logger -t "CRON" "Daily backup job completed."
  • Adds the tag CRON to the log entry for easier identification.

3. Log with a Specific Priority

logger -p auth.warning "Failed login attempt detected."
  • Logs a message with the facility auth and level warning.

4. Log File Contents

logger -f /var/log/error_log
  • Reads and logs the contents of the specified file.

5. Log with PID

logger -i "Application started successfully."
  • Logs the message and includes the process ID of logger.

6. Use Logger in a Script

#!/bin/bash
logger -t "BackupScript" "Backup job started."
# Backup command
logger -t "BackupScript" "Backup job completed."
  • Logs messages to track the progress of a backup script.

System Log Context and Benefits of Using logger

The logger command integrates with the system log, organizing messages based on facilities and severity levels. Understanding these classifications can help in effectively managing and analyzing log data. Additionally, the logger command offers several benefits, especially for system administrators and developers.

System Log Context

AspectDetails
FacilitiesCategories that group log messages, such as auth, cron, user, daemon, kern, etc.
LevelsSeverity levels that indicate the importance of a log message:
debug: Debugging information
info: General informational messages
notice: Normal but significant events
warning: Warnings about potential issues
err: Non-critical errors
crit: Critical issues
alert: Immediate attention required
emerg: Emergency situations
CombinationThe combination of facilities and levels (e.g., auth.warning) determines how and where the message is logged, based on syslog configuration.

Benefits of Using logger

BenefitDescription
Centralized LoggingEnsures all logs are stored in a unified location, simplifying management and analysis.
Enhanced TroubleshootingHelps identify issues quickly by allowing logs to be tagged and categorized effectively.
Automation-FriendlyIntegrates seamlessly into scripts, enabling contextual logging and easier debugging in automated workflows.

By leveraging the structured organization of system logs and the features of the logger command, users can ensure efficient logging and monitoring for a wide range of applications.


The logger command is a lightweight yet powerful tool for integrating custom messages into the system log. By leveraging its options, you can organize, filter, and analyze logs effectively, making it invaluable for maintaining robust system operations.

Updated on November 20, 2024
Was this article helpful?

Related Articles