Linux常用命令-logger

命令

logger

描述

a shell command interface to the syslog(3) system log module
输出shell日志到系统日志

用法

1
logger [options] [message]

选项

1
2
3
4
5
6
7
8
9
10
11
12
13
14
Options:
-T, --tcp 只使用TCP
-d, --udp 只使用UDP
-i, --id 每次记录logger进程ID
-f, --file <file> 将文件中的内容追加到日志中
-S, --size <num> 单个消息的最大值,默认1K
-n, --server <name> 指定远程syslog服务器
-P, --port <port> 指定端口
-p, --priority <prio> 指定优先级
-s, --stderr 输出消息到标准错误
-t, --tag <tag> 为每行添加标签
-u, --socket <socket> 指定socket

默认保存到/var/log/messages文件中

注意

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# 追加系统重启信息到messages
$ logger system rebooted
$ tail -1 /var/log/messages
Mar 3 17:56:16 centos7 root: system rebooted
# 追加指定文件内容到message
$ echo 'this is a test log' > a
$ logger -f a
$ tail -1 /var/log/messages
Mar 3 18:03:25 centos7 root: this is a test log
# -t指定日志标记logger
$ logger -t loggertag aaa
$ tail -1 /var/log/messages
Mar 3 18:06:36 centos7 loggertag: aaa
# -i指定logger的pid
$ logger -it mylog test log
$ tail -1 /var/log/messages
Mar 3 18:16:42 centos7 mylog[1775]: test log
# -s输出错误信息到控制台,类似于echo
$ logger -it mylog -s error log
mylog[1844]: error log