Linux logger Command
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:
- Record key operations during script execution
- Track the status of system maintenance tasks
- Logging for custom applications
- Remote logging (with a syslog server)
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 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
-
Simple Logging:
logger "系统备份任务开始执行"
This records a message in the system log, using the user.notice priority by default.
-
Tagged Logging:
logger -t BACKUP "数据库备份完成"
The log entry will appear 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 "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:
-
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 Considerations
-
Log Tag Conventions:
-
Priority Selection:
-
Log Content Guidelines:
-
Performance considerations:
-
Log Rotation:
Frequently Asked Questions
Q1: Why don't my logs appear in /var/log/syslog?
A1: This may be because:
- The facility you are using is configured to write to a different file
- The log level is lower than the minimum level configured in the system
- The system uses journald instead of traditional syslog
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