Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Linux logger Command

Linux logger Command

Linux Command Reference Linux Command Reference


logger is a simple yet powerful command-line tool in Linux used to add entries to the system log (syslog). It allows users and scripts to write custom messages to the system log file, making it an important tool for system administration and troubleshooting.

Typical use cases:


Basic syntax and parameters

The basic syntax of the logger command is as follows:

logger [选项] [消息]

Commonly used options

Option Description
-p, --priority <优先级> Specify the log priority (facility.level)
-t, --tag <标签> Tag log entries with a specified label
-i, --id Include the process ID in each log line
-f, --file <文件> Read log content from a specified file
-s, --stderr Output logs to standard error as well
-n, --server <主机> Send logs to a remote syslog server
-P, --port <端口> Specify the remote syslog server port
-u, --socket <套接字> Specify a Unix domain socket instead of the default device
-d, --udp Send logs using the UDP protocol (default)
-T, --tcp Send logs using the TCP protocol
-h, --help Display help information
-V, --version Display version information

Priority Details

A log priority consists of two parts, facility and level, in the format facility.level.

Facility Types

Facility Code Description
auth Security/authorization messages
authpriv Private authorization messages
cron Scheduled task daemon
daemon System daemons
kern Kernel messages
lpr Printing system
mail Mail system
news News system
syslog syslogd internal messages
user User-level messages (default)
local0-7 Custom local use

Log Levels

Level Code Description Value
emerg System is unusable 0
alert Action must be taken immediately 1
crit Critical conditions 2
err Error conditions 3
warning Warning conditions 4
notice Normal but significant condition 5
info Informational messages 6
debug Debug-level messages 7

Practical Examples

Basic Usage Example

  1. Simple Logging:

    logger "系统备份任务开始执行"

    This records a message in the system log, using the user.notice priority by default.

  2. Tagged Logging:

    logger -t BACKUP "数据库备份完成"

    The log entry will appear as:BACKUP: 数据库备份完成

  3. Specify Priority:

    logger -p local0.notice "自定义应用程序启动"

Advanced Usage Examples

  1. Read Log Content from a File:

    logger -f /var/log/myapp.log
  2. Record Script Execution Status:

#!/bin/bash logger -t SCRIPT -p user.info "Script started"

Performing task...

if [ $? -eq 0 ]; then logger -t SCRIPT -p user.info "Task succeeded" else logger -t SCRIPT -p user.err "Task failed" fi 3.

Remote Logging:

logger -n 192.168.1.100 -P 514 "发送到远程服务器的日志"
4.

Output to Both Terminal and Log:

logger -s "这条消息会同时出现在终端和日志中"


Log Viewing and Management

After logging, you can view the logs with the following commands:

  1. View System Logs:

    journalctl # systems using systemd or tail -f /var/log/syslog # traditional systems
  2. Filter Logs by a Specific Tag:

    journalctl -t BACKUP
  3. View Logs by Time:

    journalctl --since "2023-01-01" --until "2023-01-02"

Best Practices and Considerations

  1. Log Tag Conventions:

  2. Use meaningful tags (such as application or script names)
  3. Keep tags consistent for easier filtering and analysis later
  4. - Use meaningful tags (such as application or script names) - Keep tags consistent for easier filtering and analysis later
    1. Priority Selection:

    2. Use different levels appropriately; avoid using the same level for all logs
    3. Use warning or higher for critical operations
    4. - Use different levels appropriately; avoid using the same level for all logs - Use warning or higher for critical operations
      1. Log Content Guidelines:

      2. Include enough context information
      3. Avoid recording sensitive information (such as passwords)
      4. Keep logs concise but complete
      5. - Include enough context information - Avoid recording sensitive information (such as passwords) - Keep logs concise but complete
        1. Performance considerations:

        2. High-frequency logging may affect performance
        3. Consider using buffering or asynchronous methods for large volumes of logs
        4. - High-frequency logging may affect performance - Consider using buffering or asynchronous methods for large volumes of logs
          1. Log Rotation:

          2. Regularly clean up old logs
          3. Configure logrotate to manage log file sizes
          4. - Regularly clean up old logs - Configure logrotate to manage log file sizes

            Frequently Asked Questions

            Q1: Why don't my logs appear in /var/log/syslog?

            A1: This may be because:

            Q2: How can I tell which logging system my system uses?

            A2: Run the following command to check:

            ps aux | grep -E "syslog|journald"
            
            

            Q3: Can multiple options be used together?

            A3: Yes, for example:

            logger -t MYAPP -p local0.err -i "关键错误发生"
            
            

            Q4: How can I test the remote log server configuration?

            A4: Use logger to send a test message:

            logger -n 远程服务器IP -P 端口 "测试消息"
            
            

            Through this article, you should have mastered the core usage of the logger command. This seemingly simple tool is very useful in real-world system administration and application development, helping you build a sound logging mechanism. It is recommended to practice more in real work and use the various options flexibly according to specific needs.


            Linux Command Reference Linux Command Reference