linux日志管理之logger命令

logger命令是命令行向rsyslog服务发送日志

软件需求:
[root@centos6 ~]# rpm -qf /usr/bin/logger
util-linux-ng-2.17.2-12.7.el6.x86_64
[root@centos6 ~]#

发送的日志存放位置
logger发送命令行的消息给哪个文件是通过/etc/rsyslog.conf中*.开头的行决定的,默认情况下是任何日志日志发送给/var/log/messages里,也可以指定发送给其他文件里。

1、将来自不同地方的日志发送到/var/log/messages里

将命令行的消息发送给/var/log/messages里

[root@centos6 ~]# logger "this is a test"
Sep  9 08:57:53 centos6 root: this is a test

将命令行的消息发送给/var/log/messages里并在屏幕上显示日志内容

[root@centos6 ~]# logger -is "this is a test"
root[8846]: this is a test
[root@centos6 ~]# 
Sep  9 09:03:21 centos6 root[8846]: this is a test

将-f参数指定的文件里的内容发送给/var/log/messages里

[oracle@centos6 trace]$ logger -i -f /u01/app/diag/rdbms/orcl/orcl/trace/alert_orcl.log 
[oracle@centos6 trace]$ 

-p选项,用来将命令行的消息发送给指定日志设备以及日志级别来存

-p pri   Enter the message with the specified priority.  The priority may be specified numerically or as a
‘‘facility.level’’ pair.  For example, ‘‘-p local3.info’’ logs the message(s) as informational level in
the local3 facility.  The default is ‘‘user.notice.’’

在rsyslog中,日志内容是有优先级的,比如最低的是debug,最高级别是emerg。在rsyslog里通常用这种形式‘‘facility.level’’来说明日志优先级,其中facility是程序或者设备名称,也就是谁来产生日志的。level是rsyslog记录这个产生什么级别的日志。

在rsyslog中,local0-local7是自定义日志设备。
将命令行中的消息发送给local0设备日志存放的路径/var/log/mtest里并且注释掉*.开头的行
vim /etc/rsyslog.conf
local1.info /var/log/mtest

重启rsyslog服务
service rsyslog restart

发送命令行日志给local1.info
[root@centos6 ~]# logger -p local1.info -t mytest "this is a test@@@@"

[root@centos6 log]# tail -f /var/log/mtest
Sep  9 12:54:40 centos6 mytest: this is a test@@@@

2、日志内容格式的指定
给/var/log/message发送日志时候,带上logger进程ID,日志格式里带有进程ID

[root@centos6 ~]# logger -i "this is a test"
Sep  9 08:58:13 centos6 root[8646]: this is a test

在日志内容里进程号前面默认的是用户名称,通过-t参数,指定了特定的字符,这样可以用来标记特定日志内容

[root@centos6 ~]# logger -si "this is a -t test"
root[16836]: this is a -t test
[root@centos6 ~]# 
Sep  9 12:36:20 centos6 root[16836]: this is a -t test

[root@centos6 ~]# logger -si -t ABC  "this is a -t test"
ABC[16837]: this is a -t test
[root@centos6 ~]# 
Sep  9 12:36:58 centos6 ABC[16837]: this is a -t test

-u sock Write to socket as specified with socket instead of builtin syslog routines.
-d Use a datagram instead of a stream connection to this socket.

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值