http://blog.csdn.net/wackycrazy/article/details/48947673
Kafka0.8版本长时间运行过程中,在kafka_2.8.0-0.8.0/logs目录下产生了大量的kafka-request.log.*和server.log.*文件,其中*代表日期和时间,比如kafka-request.log.2014-12-08-03和server.log.2014-12-06-03,这些文件对磁盘空间的消耗非常大,需要定期备份或者清理。目前没有发现kafka自身提供了这些操作日志备份或者清理的方法,需要用户自己实现。
kafka0.8版本中,config/log4j.properties中日志的级别设置的是TRACE,在长时间运行过程中产生的日志大小吓人,所以如果没有特殊需求,强烈建议将其更改成INFO级别。具体修改方法如下所示,将config/log4j.properties文件中最后的几行中的TRACE改成INFO,修改前如下所示:
log4j.logger.kafka.network.RequestChannel$=TRACE, requestAppender
log4j.additivity.kafka.network.RequestChannel$=false
#log4j.logger.kafka.network.Processor=TRACE, requestAppender
#log4j.logger.kafka.server.KafkaApis=TRACE, requestAppender
#log4j.additivity.kafka.server.KafkaApis=false
log4j.logger.kafka.request.logger=TRACE, requestAppender
log4j.additivity.kafka.request.logger=false
log4j.logger.kafka.controller=TRACE, controllerAppender
log4j.additivity.kafka.controller=false
log4j.logger.state.change.logger=TRACE, stateChangeAppender
log4j.additivity.state.change.logger=false
修改后如下所示:
log4j.logger.kafka.network.RequestChannel$=INFO, requestAppender
log4j.additivity.kafka.network.RequestChannel$=false
#log4j.logger.kafka.network.Processor=INFO, requestAppender
#log4j.logger.kafka.server.KafkaApis=INFO, requestAppender
#log4j.additivity.kafka.server.KafkaApis=false
log4j.logger.kafka.request.logger=INFO, requestAppender
log4j.additivity.kafka.request.logger=false
log4j.logger.kafka.controller=INFO, controllerAppender
log4j.additivity.kafka.controller=false
log4j.logger.state.change.logger=INFO, stateChangeAppender
log4j.additivity.state.change.logger=false
备份操作日志的方式是定期拷贝操作日志到指定大容量存储设备,然后将其删除。我在项目中采取的方式是直接清理过期操作日志,方法非常简单,利用linux操作系统提供的crontab功能,定期清理操作日志,方法如下:
在/etc/crontab文件中添加删除操作日志的语句,然后执行crontab -f /etc/crontab命令即可,我的/etc/crontab文件如下所示,语句的含义是每4天在23:59执行清理操作:
[root@h10p11 logs]# vim /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/
# For details see man 4 crontabs
# Example of job definition:
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * user-name command to be executed
59 23 */4 * * rm -fr /root/kafka/kafka_2.8.0-0.8.0/logs/*-*-*
直接修改/etc/crontab文件重启机器不用重复设置。