linux】利用logger,logrotate处理脚本日志

来源:互联网 发布:锐捷网络校园招聘行程 编辑:程序博客网 时间:2024/05/22 17:17

更多的时候我处理我们的程序(脚本)的日志是通过重定向的方法去实现,而日志的大小和切割有的时候就会忽略掉(哦,也许你加条find命令检测一下大小或者超过一定时间执行删除或者切割操作),何必自己再去写这样的程序呢,我们的系统就自带这样的程序;
logger
logger 是一个shell 命令接口,可以通过该接口使用Syslog的系统日志模块,还可以从命令行直接向系统日志文件(或者自定义的文件)写入一行信息。
下面就是logger的man信息
用法

logger [-isd] [-f file] [-p pri] [-t tag] [-u socket] [message …]

描述

Logger 用于往系统中写入日志. 它提供了一个shell命令接口到syslog系统模块

选项:

-i 逐行记录每一次logger的进程ID.

-s 记录消息到标准错误, 包括系统日志.

-f file 记录特定的文件.

-p pri 输入消息的特定优先级. 优先级可以是自定义的数值或者诸如 “facility.level” 的格式. 举例: “-p local3.info” local3 facility这个设备的消息级别为info. 默认是 “user.notice.”

-t tag 打上特定的标志.

-u sock 以特定的socket代替内嵌系统常规工作

-d 使用一个数据进程代替一个流连接到这个socket.

— 结束参数列表. 这个允许消息以一个“-”开始

message 写入log文件的内容消息,可以与-f配合使用

logger 以0退出表示成功, 大于0表示失败.

可用的facility名有: auth, authpriv (for security information of a
sensitive nature), cron, daemon, ftp, kern, lpr, mail, news, security
(deprecated synonym for auth), syslog, user, uucp, and local0 to local7,
inclusive.

可用的level名用: alert, crit, debug, emerg, err, error (deprecated
synonym for err), info, notice, panic (deprecated synonym for emerg),
warning, warn (deprecated synonym for warning).

举例:

logger System rebooted

logger -p local0.notice -t HOSTIDM -f /dev/idmc

举个例子好了,如何在我们的脚本中利用logger命令:
[root@qing ~]# cat my_test.sh
#!/bin/bash
#some other scripts goes here …
logger -i -t my_test.sh -p local3.notice ” my_test.sh find some error in …”
echo “Hello world”
[root@qing ~]# vim /etc/rsyslog.conf #在Centos6之前的版本这个文件对应的应该是/etc/syslog.conf,对应的进程为syslogd
修改这一行:增加local3.none,表示local3上的任何消息都不记录到/var/log/messages 中
*.info;mail.none;authpriv.none;cron.none;local3.none /var/log/messages
增加这一行:意思是来自local3的所有消息都记录到/var/log/my_test.log ,前面加个“-”,说明要启用写入缓冲
local3.* -/var/log/my_test.log

[root@qing ~]# service rsyslog restart
Shutting down system logger: [ OK ]
Starting system logger: [ OK ]
[root@qing ~]# cat my_test.sh
#/bin/bash
#some other scripts goes here …
logger -i -t my_test.sh -p local3.notice ” my_test.sh find some error in …”
echo “Hello world”
[root@qing ~]# bash my_test.sh
Hello world
[root@qing ~]# cat /var/log/my_test.log
Aug 14 23:15:19 localhost my_test.sh[31996]: my_test.sh find some error in …
Aug 14 23:32:41 localhost my_test.sh[32096]: my_test.sh find some error in …

哈哈,看看怎么样,有了吧。。
那要是我日志变的越来越大,怎么办,我们的系统日志是怎么变成messages.0,messages.1….的呢,这就是我们接下来所说的logrotate
这个东东可是管理着我们系统的大大小小的日志的切割与轮替哦。。。
[root@qing ~]# cat /etc/logrotate.conf
# see “man logrotate” for details
# rotate log files weekly
weekly

# keep 4 weeks worth of backlogs
rotate 4

# create new (empty) log files after rotating old ones
create

# use date as a suffix of the rotated file
dateext

# uncomment this if you want your log files compressed
#compress

# RPM packages drop log rotation information into this directory
include /etc/logrotate.d

# no packages own wtmp and btmp — we’ll rotate them here
/var/log/wtmp {
monthly
create 0664 root utmp
minsize 1M
rotate 1
}

/var/log/btmp {
missingok
monthly
create 0600 root utmp
rotate 1
}

具体的意思大家可以网上搜下,我们在/etc/logrotate.d/下创建我们自己的一个配置文件:
[root@qing ~]# vim /etc//logrotate.d/my_test
/var/log/my_test.log #日志处理的目标
{
nocompress #不采用压缩
daily #每天轮替
copytruncate
ifempty #文件为空则不切割
olddir /root/chenqing/log #切割后的文件所保存的目录
rotate 4 #保存最近切割的4份文件
}
最好重启下syslog。。。。

这样就可以像我们的系统日志一样定时的进行切割了,再也不用关心我们所输出的日志了,这里面还有其它的更详细的设置,大家不妨自己好好研究下了。。。


转自 : 乡村运维

0 0