logrotate 日志滚动的使用

简介: logrotate 日志滚动的使用
最近由于最近上一个了项目,但是项目的本身没有做日志的滚动,导致日志一直在增长,这样下去肯定会撑爆磁盘,导致不可预测的结果   
这个适合logrotate就出场了

作用

 logrotate is designed to ease administration of systems that generate large numbers of log files. It allows automatic rotation, compression, removal,   
  and mailing of log files. Each log file may be handled daily, weekly, monthly, or when it grows too large.

正如man logrotate说的那样,logrotate可以实现自动轮替、压缩、删除日志、并且发邮件的功能

使用

root@49335c6e5ee3:/var/log# logrotate --help
Usage: logrotate [OPTION...] <configfile>
  -d, --debug               Don't do anything, just test (implies -v)
  -f, --force               Force file rotation
  -m, --mail=command        Command to send mail (instead of `/usr/bin/mail')
  -s, --state=statefile     Path of state file
  -v, --verbose             Display messages during rotation
  -l, --log=STRING          Log file or 'syslog' to log to syslog
      --version             Display version information
Help options:
  -?, --help                Show this help message
      --usage               Display brief usage message

也就是说,可以手动的执行 logrotate configfile 文件,那配置文件里的内容怎么设置呢,如下demo:

/var/log/cron.log {
  daily
  rotate 7
  notifempty
  create
  size 1K
  nocompress
  nodateext
  missingok
}

解释说明

image.png

具体的详细说明,参照man logrotate

运行机制

对安装了cron的系统来说,crontab会每天定时执行/etc/cron.daily目录下的脚本,而这个目录下有个文件叫logrotate :

 #!/bin/sh
test -x /usr/sbin/logrotate || exit 0
/usr/sbin/logrotate /etc/logrotate.conf

我们看到 每天会运行/usr/sbin/logrotate /etc/logrotate.conf 这个命令,

在linux上 文件/etc/logrotate.conf内容如下:

# see "man logrotate" for details
# rotate log files weekly
weekly
# use the syslog group by default, since this is the owning group
# of /var/log/syslog.
su root syslog
# keep 4 weeks worth of backlogs
rotate 4
# create new (empty) log files after rotating old ones
create
# uncomment this if you want your log files compressed
#compress
# packages drop log rotation information into this directory
include /etc/logrotate.d
# no packages own wtmp, or btmp -- we'll rotate them here
/var/log/wtmp {
    missingok
    monthly
    create 0664 root utmp
    rotate 1
}
/var/log/btmp {
    missingok
    monthly
    create 0660 root utmp
    rotate 1
}
# system-specific logs may be configured here

我们看到include /etc/logrotate.d,会把/etc/logrotate.d目录下的文件include进来,

所以对于用户来说在/etc/logrotate.d 配置文件,就可以,总结一下

logrotate的配置文件:

/etc/logrotate.d/*   
/etc/logrotate.conf
相关实践学习
日志服务之使用Nginx模式采集日志
本文介绍如何通过日志服务控制台创建Nginx模式的Logtail配置快速采集Nginx日志并进行多维度分析。
相关文章
|
2月前
|
监控 Shell Linux
【Shell 命令集合 系统管理 】Linux 自动轮转(log rotation)日志文件 logrotate命令 使用指南
【Shell 命令集合 系统管理 】Linux 自动轮转(log rotation)日志文件 logrotate命令 使用指南
58 0
|
存储 Unix Linux
CentOS7下日志轮转logrotate简单入门与实践
CentOS7下日志轮转logrotate简单入门与实践
362 0
CentOS7下日志轮转logrotate简单入门与实践
|
3月前
|
应用服务中间件
weblogic配置、修改日志保存目录、配置滚动日志
weblogic配置、修改日志保存目录、配置滚动日志
|
4月前
|
存储 监控 Ubuntu
日志切割工具-Logrotate实现nginx日志切割
日志切割工具-Logrotate实现nginx日志切割
42 0
|
Linux
linux下实现日志切割的两种方法 logrotate
linux下实现日志切割的两种方法 logrotate
151 0
|
SQL druid Java
三步实现maven工程集成logback日志框架(日志按天滚动生成文件)并附源码
三步实现maven工程集成logback日志框架(日志按天滚动生成文件)并附源码
90 0
|
10月前
|
存储 监控 Ubuntu
日志切割工具-Logrotate实现nginx日志切割
日志切割工具-Logrotate实现nginx日志切割
163 0
|
11月前
|
Linux 开发工具
Linux日志管理logrotate日志轮转
上篇文章学习了系统日志管理,对于日志来讲他是占内存的,当有大量的日志产生的时候,会有一天占满我们的内存,所以出现了日志轮转,轮转日志会删除时间久远的日志,来节省空间,这篇文章就是带大家了解学习日志轮转,通过本篇文章了解日志轮转的相关配置文件,学习怎么配置日志轮转,根据我们的需求进行配置,下面来进行学习吧。
155 0
|
12月前
|
Linux
linux下如何使用logrotate进行日志的切分
linux下如何使用logrotate进行日志的切分
9014 0
|
12月前
|
NoSQL Linux MongoDB
一日一技:使用 Linux 自带的 logrotate 管理你的所有日志
一日一技:使用 Linux 自带的 logrotate 管理你的所有日志
188 0