Linux logger Command
logger is a simple yet powerful command-line tool in Linux, used to add log entries to the system log (syslog). It allows users and script programs to write custom messages to system log files, making it an important tool for system administration and troubleshooting.
Typical Use Cases:
- Record key operations during script execution
- Track the status of system maintenance tasks
- Log records for custom applications
- Remote logging (with a syslog server)
Basic Syntax and Parameters
The basic syntax of the logger command is as follows:
logger [选项] [消息]
Common Option Parameters
| Option | Description |
|---|---|
-p, --priority <优先级> |
Specify log priority (facility.level) |
-t, --tag <标签> |
Tag log entries with a specified label |
-i, --id |
Include process ID in each log line |
-f, --file <文件> |
Read log content from a specified file |
-s, --stderr |
Also output logs to standard error |
-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 UDP protocol (default) |
-T, --tcp |
Send logs using TCP protocol |
-h, --help |
Display help information |
-V, --version |
Display version information |
Priority Explained in Detail
Log priority consists of two parts: facility and level, formatted asfacility.level。
Facility Types
| Facility Code | Description |
|---|---|
| auth | Security/authorization messages |
| authpriv | Private authorization messages |
| cron | Cron daemon |
| daemon | System daemon |
| kern | Kernel messages |
| lpr | Printing system |
| 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 conditions | 5 |
| info | Informational messages | 6 |
| debug | Debug-level messages | 7 |
Practical Examples
Basic Usage Examples
-
Simple log recording:
logger "系统备份任务开始执行"
This will record a message in the system log, using the user.notice priority by default.
-
Log with a tag:
logger -t BACKUP "数据库备份完成"
The log entry will be displayed as:
BACKUP: 数据库备份完成 -
Specify priority:
logger -p local0.notice "自定义应用程序启动"
Advanced Usage Examples
-
Read log content from a file:
logger -f /var/log/myapp.log
-
Record script execution status:
#!/bin/bash logger -t SCRIPT -p user.info "脚本开始执行" # 执行任务... if [ $? -eq 0 ]; then logger -t SCRIPT -p user.info "任务执行成功" else logger -t SCRIPT -p user.err "任务执行失败" fi -
Remote log recording:
logger -n 192.168.1.100 -P 514 "发送到远程服务器的日志"
-
Output to terminal and log simultaneously:
logger -s "这条消息会同时出现在终端和日志中"
Viewing and Managing Logs
After recording logs, you can use the following commands to view them:
-
View system logs:
journalctl # systems using systemd or tail -f /var/log/syslog # traditional systems -
Filter logs by a specific tag:
journalctl -t BACKUP
-
View logs by time:
journalctl --since "2023-01-01" --until "2023-01-02"
Best Practices and Precautions
-
Log tag conventions:
- Use meaningful tags (such as application name or script name)
- Keep tags consistent for easier filtering and analysis later
-
Priority selection:
- Use different levels appropriately; avoid using the same level for all logs
- Use warning or higher for critical operations
-
Log content suggestions:
- Include sufficient context information
- Avoid recording sensitive information (such as passwords)
- Keep logs concise but complete
-
Performance considerations:
- High-frequency logging may affect performance
- Consider using buffered or asynchronous methods for logging large volumes
-
Log rotation:
- Regularly clean up old logs
- Configure logrotate to manage log file sizes
FAQ
Q1: Why does my log not appear in /var/log/syslog?
A1: This may be because:
- The facility you are using is configured to write to another file
- The log level is lower than the system-configured minimum recording level
- The system uses journald instead of traditional syslog
Q2: How do I determine which logging system my system uses?
A2: Run the following command to check:
ps aux | grep -E "syslog|journald"
Q3: Can I use multiple options at the same time?
A3: Yes, for example:
logger -t MYAPP -p local0.err -i "关键错误发生"
Q4: How do 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 practical system administration and application development, helping you establish a complete logging mechanism. It is recommended to practice more in your actual work and flexibly use various option parameters according to specific needs.
Other Extensions
Linux Command Reference