php对日志文件切割,日志管理(2) 用logrotate切割php和nginx日志

1、简介

logrotate 程序是一个日志文件管理工具。用来把旧的日志文件删除,并创建新的日志文件,我们把这个过程叫做“转储”。

我们可以根据日志文件的大小,也可以根据其天数来转储,这个过程一般通过 cron 程序来执行,比如系统每天的定时任务会执行一次logrotate操作来完成系统日志的转储。

logrotate 的默认配置文件是/etc/logrotate.conf,一般自定义一个新的配置文件来完成相应日志切割管理。

2、配置参数详解

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26compress 通过gzip 压缩转储以后的日志

nocompress 不需要压缩时,用这个参数

copytruncate 用于还在打开中的日志文件,把当前日志备份并截断

nocopytruncate 备份日志文件但是不截断

create mode owner group 转储文件,使用指定的文件模式创建新的日志文件

nocreate 不建立新的日志文件

delaycompress 和 compress 一起使用时,转储的日志文件到下一次转储时才压缩

nodelaycompress 覆盖

delaycompress 选项,转储同时压缩

errors address 专储时的错误信息发送到指定的Email 地址

ifempty 即使是空文件也转储,这个是 logrotate 的缺省选项

notifempty 如果是空文件的话,不转储

mail address 把转储的日志文件发送到指定的E-mail 地址

nomail 转储时不发送日志文件

olddir directory 转储后的日志文件放入指定的目录,必须和当前日志文件在同一个文件系统

noolddir 转储后的日志文件和当前日志文件放在同一个目录下

prerotate/endscript 在转储以前需要执行的命令可以放入这个对,这两个关键字必须单独成行

postrotate/endscript 在转储以后需要执行的命令可以放入这个对,这两个关键字必须单独成行

daily 指定转储周期为每天

weekly 指定转储周期为每周

monthly 指定转储周期为每月

rotate count 指定日志文件删除之前转储的次数,0 指没有备份,5 指保留5 个备份

tabootext [+] list 让logrotate 不转储指定扩展名的文件,缺省的扩展名是:.rpm-orig, .rpmsave, v, 和 ~

size size 当日志文件到达指定的大小时才转储,Size 可以指定 bytes (缺省)以及KB (sizek)或者MB (sizem)

dateext 切换后的日志文件会附加上一个短横线和YYYYMMDD格式的日期,没有这个配置项会附加一个小数点加一个数字序号

dateformat 配合dateext使用可以为切割后的日志加上YYYYMMDD格式的日期

3、缺省的配置详解/etc/logrotate.conf

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24# see "man logrotate" for details

# rotate log files weekly

weekly

# keep 4 weeks worth of backlogs

rotate 4

# send errors to root

errors root

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

create

# uncomment this if you want your log files compressed

#compress

1

# RPM packages drop log rotation information into this directory

include /etc/logrotate.d

# no packages own lastlog or wtmp --we'll rotate them here

/var/log/wtmp {

monthly

create 0664 root utmp

rotate 1

}

/var/log/lastlog {

monthly

rotate 1

}

缺省的配置一般放在logrotate.conf 文件的最开始处,影响整个系统。在本例中就是前面14行。

weekly 指定所有的日志文件每周转储一次。

rotate 4 指定转储文件的保留 4份。

errors root 指定错误信息发送给root。

create 指定 logrotate 自动建立新的日志文件,新的日志文件具有和原来的文件一样的权限。

compress 指定不压缩转储文件,如果需要压缩,去掉注释就可以了。

include 默认include文件夹下的所有配置文件都生效。

4、系统自带的系统日志切割配置/etc/logrotate.d/syslog

该配置只是对messages、maillog、messages、secure、spooler系统日志有效。

1

2

3

4

5

6

7

8

9

10

11

12$ cat /etc/logrotate.d/syslog

/var/log/cron

/var/log/maillog

/var/log/messages

/var/log/secure

/var/log/spooler

{

sharedscripts

postrotate

/bin/kill -HUP \`cat /var/run/syslogd.pid 2> /dev/null\` 2> /dev/null || true

endscript

}

5、按日期对nginx、php的日志进行切割

新建配置文件/etc/logrotate_php_nginx.conf

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50$ cat /etc/logrotate_php_nginx.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

# ... ...

# ... ...

# system-specific logs may be also be configured here.

/opt/log/php-fpm/php_errors.log

/opt/log/php-fpm/php-fpm.log

/opt/log/php-fpm/www.log.slow

/opt/log/nginx/nginx_error.log

{

size 0

missingok

rotate 4

nocompress

dateext

dateformat -%Y%m%d

copytruncate

}

/opt/log/nginx/xxxxx.xxxxxxx.com

/opt/log/nginx/xxx.xxxx.com

/opt/log/nginx/xxxx.xxx.com

/opt/log/nginx/xxxxxx.xxxxxxxxxx.com

/opt/log/nginx/xxxx.xxxxxxx.com

/opt/log/nginx/xxxxx.xxxxxx.com

/opt/log/nginx/x.xxxxxx.com

/opt/log/nginx/xxxxxx.xxxxxxx.com

{

size 0

missingok

rotate 4

nocompress

dateext

dateformat -%Y%m%d

copytruncate

}

然后再新增加一条定时任务,每天23:59执行一次特定的logrorate操作。

注意上面的rotate的值为4。只能保存最新的5份日志即5天的日志,如果只想将日志落地到本地需要将该参数调大(1年就是365)。

159 23 * * * root sh /usr/sbin/logrotate /etc/logrotate_php_nginx.conf > /dev/null 2>&1

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值