use logrotate manage log file

简介:
logrotate一般用于管理日志文件, 例如 : 
[root@db-192-168-173-42 tmp]# ll /var/log/messages*
-rw------- 1 root root 599922 Jan  2 16:06 /var/log/messages
-rw------- 1 root root    138 Dec 27 19:20 /var/log/messages.1
-rw------- 1 root root     58 Dec 14 04:02 /var/log/messages.2
-rw------- 1 root root     58 Dec  7 04:02 /var/log/messages.3
-rw------- 1 root root    138 Dec  3 01:09 /var/log/messages.4
例如 : 
对需要管理的日志文件  /tmp/sky_pg_clusterd.log  , 创建一个配置文件.
vi /etc/logrotate.d/sky_pg_cluster
/tmp/sky_pg_clusterd.log   
{   
         size 10M   
         create   
         start 10  
         rotate 4  
         compress   
         copytruncate   
}
根据以上配置, 如果文件超过10M, 将压缩存放, 并截断.
# logrotate /etc/logrotate.d/sky_pg_cluster 

# ll
total 1756
-rw-rw-r-- 1 postgres postgres  4755 Jan  2 17:10 sky_pg_clusterd.log
-rw-rw-r-- 1 postgres postgres 1082000 Jan  2 16:24 sky_pg_clusterd.log.10.gz
再次调用, 因为文件还小于10M, 所以不进行处理.
# logrotate /etc/logrotate.d/sky_pg_cluster 
[root@db-192-168-173-42 tmp]# ll
total 1772
-rw-rw-r-- 1 postgres postgres  708613 Jan  2 17:10 sky_pg_clusterd.log
-rw-rw-r-- 1 postgres postgres 1082000 Jan  2 16:24 sky_pg_clusterd.log.10.gz

一般可以搞成crontab自动处理.
*/5 * * * * /usr/sbin/logrotate  /etc/logrotate.d/sky_pg_cluster


[参考]
1. man logrotate
Syslog-ng服务只提供对log进行接收以及再处理(继续分发或者存储),但并不对产生的log文件进行管理,这样导致的后果就是可能这个文件越来越大,在我们这个项目中,由于忘了对日志文件进行管理,结果导致这个日志文件20多G,囧,很有可能导致系统崩溃。

日志文件管理这项工作其实是由logrotate模块来负责。

对于logrotate可以参考官网, 上面有详尽的介绍,最常用的三个方式为: 
logrotate /etc/logrotate.conf:重新读取配置文件,并对符合条件的文件文件进行rotate。 
logrotate -d /etc/logrotate.conf:调试模式,输出调试结果,但并不执行。 
logrotate -f /etc/logrotate.conf:强制模式,对所有相关文件进行rotate。

至于logrotate对文件进行管理时的所要执行的规则,可以在/etc/logrotate.d文件夹下创建文件,logrotate会在执行的时候自动读取相应的规则,比如,我在/etc/logrotate.d/路径下新建了一个名为syslog的文件,www.linuxidc.com其中的内容为:

/log/test.log   
{   
         size 10M   
         create   
         start 10  
         rotate 4  
         compress   
         copytruncate   
}  
上面代码的意思为当文件/log/test.log的大小超过10M时就开始循环,允许logrotate创建文件,循环文件的开始为10,即文件为为test.log.10.gz,最多循环创建4个文件,即为test.log.13.gz,多于四个后开始在第一个上面循环,依次迭代。

这样在配置logrotate的规则的时候就相对非常灵活,当我需求改动,需要添加其他新的规则的时候,我就不必修改syslog文件中的规则,可以新创建一个文件,添加相应的规则即可。这种灵活的配置方式对编程来说是一大便利,对于编程来说就是更容易实现对规则的自动控制了。

另外的一个问题,logrotate规则配置好后怎么定时去执行呢?这个就可以交给Linux中的crontab来控制了,具体的crontab的配置可以参考官网,我这里给出我使用的demo:

执行:vi /etc/crontab

配置如下:

*/5 * * * * root /sbin/logrotate /etc/logrotate.conf  

表示每隔5分钟执行检查一次。

这样整个配置就基本上完成了。
相关实践学习
日志服务之使用Nginx模式采集日志
本文介绍如何通过日志服务控制台创建Nginx模式的Logtail配置快速采集Nginx日志并进行多维度分析。
相关文章
|
4月前
如何关掉Parsed mapper file日志打印
如何关掉Parsed mapper file日志打印
67 1
|
3月前
|
运维 应用服务中间件 网络安全
利于群晖的File Station+SFTP实现第三方人员快速获取服务器应用日志
利于群晖的File Station+SFTP实现第三方人员快速获取服务器应用日志
68 0
|
6月前
|
关系型数据库 MySQL 数据库
阿里云Mysql数据库物理全备文件恢复到自建数据库Mysql报错:InnoDB: Log file ./...xtrabacku
阿里云Mysql数据库物理全备文件恢复到自建数据库Mysql报错:InnoDB: Log file ./...xtrabacku
|
11月前
|
Oracle 前端开发 关系型数据库
log file sync 和 log file parallel write等待事件的区别和联系
log file parallel write 和log file sync这两个等待事件关系密切,很多人对这两个等待事件有一些误解,我们先来看看Oracle官方文档的解释:
|
存储 缓存 数据处理
完全揭秘log file sync等待事件
什么是log file sync等待事件呢?在一个提交(commit)十分频繁的数据库中,一般会出现log file sync等待事件,当这个等待事件出现在top5中,这个时侯我们需要针对log file sync等待事件进行优化,一定要尽快分析并解决问题,否则当log file sync等待时间从几毫秒直接到20几毫秒可能导致系统性能急剧下降,甚至会导致短暂的挂起。
完全揭秘log file sync等待事件
|
Kubernetes 容器
logdna过滤log file
本文在使用logdna的基础上,将logdna-agent-master.yml文件直接部署到k8s cluster中. 通过设置yml文件或者在logdna UI界面上的过滤操作,达到了预期的效果。
2217 0
|
Oracle 关系型数据库 Apache
|
关系型数据库 Oracle
|
监控 调度