logger

file managementLinux/Unix
The logger command is one of the most frequently used commands in Linux/Unix-like operating systems. logger Add entries to the system log

Quick Reference

Command Name:

logger

Category:

file management

Platform:

Linux/Unix

Basic Usage:

logger [options] [arguments]

Common Use Cases

    Syntax

    logger [options] [message]

    Options

    Option Description
    -d, --udp Use UDP for remote logging (default)
    -e, --skip-empty Do not log empty lines
    -f, --file file Log the contents of the specified file
    -i, --id Log the process ID too
    --id=id Use the specified ID instead of process ID
    -n, --server host Write to the specified remote syslog server
    -P, --port port Use the specified UDP port
    -p, --priority priority Mark the message with the specified priority
    --prio-prefix Look for a priority in the message
    -s, --stderr Output message to standard error as well
    -t, --tag tag Mark every line with the specified tag
    -T, --tcp Use TCP for remote logging
    -u, --socket socket Write to the specified socket instead of built-in syslog
    --socket-errors Print connection errors when using Unix sockets
    --journald[=file] Write journald entry
    -h, --help Display help text and exit
    -V, --version Display version information and exit

    Examples

    How to Use These Examples

    The examples below show common ways to use the logger command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.

    # Basic Examples Basic
    logger "System backup completed successfully"
    Add a simple message to the system log.
    logger -p auth.notice "User authentication attempt"
    Log a message with priority auth.notice.
    # Advanced Examples Advanced
    logger -t backup -p local0.info "Backup process completed" Log a message with a specific tag and priority. logger -i "Disk space alert" Include the process ID in the log message. logger -f /var/log/myapp.log "Application started" Log a message to a specific file. uptime | logger -t performance Pipe command output to logger with a tag. logger -n 192.168.1.100 -P 514 "Remote log entry" Send a log message to a remote syslog server. logger -s "Critical error" 2>&1 | mail -s "Error Alert" admin@example.com Log to syslog and stderr, then email the output. logger --id=42 --socket-errors=off "Custom message with options" Use more advanced options for specialized logging.

    Try It Yourself

    Practice makes perfect! The best way to learn is by trying these examples on your own system with real files.

    Understanding Syntax

    Pay attention to the syntax coloring: commands, options, and file paths are highlighted differently.

    Notes

    The 'logger' command is a shell utility that provides an easy way to add entries to the system log (syslog) from the command line or shell scripts. It serves as a simple interface to the syslog system log module, allowing users and scripts to generate log messages without having to interact directly with the syslog API. Key features of the logger command: 1. Simple Logging Interface: logger provides a straightforward way to create log entries, making system logging accessible to users and script writers without requiring specialized programming knowledge. 2. Priority and Facility Support: Through the -p option, logger allows specifying both the facility (subsystem) and priority (severity level) of messages, enabling proper categorization and filtering of log entries. 3. Message Tagging: The -t option lets users tag messages with an identifier, making it easier to distinguish log entries from different sources or applications. 4. Process ID Inclusion: With the -i flag, logger can include the process ID in log messages, which helps with troubleshooting by connecting log entries to specific process instances. 5. Remote Logging: logger supports sending log messages to remote syslog servers, facilitating centralized logging in networked environments. 6. File Content Logging: Using the -f option, entire file contents can be logged, which is useful for archiving configuration files or error reports in the system log. 7. Integration with Shell Pipelines: logger can accept input from standard input, allowing command outputs to be piped directly into the logging system. Common use cases for the logger command include: - Recording the execution and completion of system maintenance tasks and cron jobs - Logging security-related events from shell scripts - Tracking user-initiated system changes - Documenting application deployment or configuration updates - Sending alerts or notifications via the syslog infrastructure - Centralizing log information from multiple sources - Creating audit trails for compliance purposes The logged messages typically end up in files like /var/log/syslog, /var/log/messages, or other log files depending on the system's syslog configuration. Modern systems using systemd may direct these messages to the journal, which can be accessed using the journalctl command. For system administrators and developers, the logger command is an invaluable tool for enhancing the observability of systems and applications, providing a standardized way to integrate custom scripts and tools with the system's logging infrastructure. It's particularly useful in environments where comprehensive logging is essential for security auditing, performance monitoring, or troubleshooting.

    Related Commands

    These commands are frequently used alongside logger or serve similar purposes:

    Use Cases

    Learn By Doing

    The best way to learn Linux commands is by practicing. Try out these examples in your terminal to build muscle memory and understand how the logger command works in different scenarios.

    $ logger
    View All Commands