1.log4j进行日志切分
1)准备三个包:log4j-1.2.17.jar tomcat-juli.jar tomcat-juli-adapters.jar 放到tomcat的lib目录或者是工程的WEB_INF/lib下,
2)在lib目录下新建log4j.properties,加入以下内容
log4j.rootLogger = INFO, CATALINA # Define all the appenders log4j.appender.CATALINA = org.apache.log4j.DailyRollingFileAppender log4j.appender.CATALINA.File = ${catalina.base}/logs/catalina log4j.appender.CATALINA.Append = true log4j.appender.CATALINA.Encoding = UTF-8 # Roll-over the log once per day log4j.appender.CATALINA.DatePattern = '.'yyyy-MM-dd'.log' log4j.appender.CATALINA.layout = org.apache.log4j.PatternLayout log4j.appender.CATALINA.layout.ConversionPattern = %d [%t] %-5p %c- %m%n log4j.appender.LOCALHOST = org.apache.log4j.DailyRollingFileAppender log4j.appender.LOCALHOST.File = ${catalina.base}/logs/localhost log4j.appender.LOCALHOST.Append = true log4j.appender.LOCALHOST.Encoding = UTF-8 log4j.appender.LOCALHOST.DatePattern = '.'yyyy-MM-dd'.log' log4j.appender.LOCALHOST.layout = org.apache.log4j.PatternLayout log4j.appender.LOCALHOST.layout.ConversionPattern = %d [%t] %-5p %c- %m%n log4j.appender.MANAGER = org.apache.log4j.DailyRollingFileAppender log4j.appender.MANAGER.File = ${catalina.base}/logs/manager log4j.appender.MANAGER.Append = true log4j.appender.MANAGER.Encoding = UTF-8 log4j.appender.MANAGER.DatePattern = '.'yyyy-MM-dd'.log' log4j.appender.MANAGER.layout = org.apache.log4j.PatternLayout log4j.appender.MANAGER.layout.ConversionPattern = %d [%t] %-5p %c- %m%n log4j.appender.HOST-MANAGER = org.apache.log4j.DailyRollingFileAppender log4j.appender.HOST-MANAGER.File = ${catalina.base}/logs/host-manager log4j.appender.HOST-MANAGER.Append = true log4j.appender.HOST-MANAGER.Encoding = UTF-8 log4j.appender.HOST-MANAGER.DatePattern = '.'yyyy-MM-dd'.log' log4j.appender.HOST-MANAGER.layout = org.apache.log4j.PatternLayout log4j.appender.HOST-MANAGER.layout.ConversionPattern = %d [%t] %-5p %c- %m%n log4j.appender.CONSOLE = org.apache.log4j.ConsoleAppender log4j.appender.CONSOLE.Encoding = UTF-8 log4j.appender.CONSOLE.layout = org.apache.log4j.PatternLayout log4j.appender.CONSOLE.layout.ConversionPattern = %d [%t] %-5p %c- %m%n # Configure which loggers log to which appenders log4j.logger.org.apache.catalina.core.ContainerBase.[Catalina].[localhost] = INFO, LOCALHOST log4j.logger.org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/manager] = INFO, MANAGER log4j.logger.org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/host-manager] = INFO, HOST-MANAGER
3)再tomcat根目录下,conf文件夹下,删除或者重命名logging.properties文件。然后修改context.xml文件,在<Context> 标签修改为<Context swallowOutput="true">,以接管tomcat的日志输出。这样,使用log4j进行日志切分就完成了。
2、使用cronolog进行切分日志
Cronolog是一个过滤器程序,它从标准输入读取日志文件条目,并将每个条目写入由文件名模板和当前日志所指定的输出文件中。 当扩展文件名改变时,关闭当前文件,并打开一个新文件。 Cronolog是为了与Apache等Web服务器一起使用,将访问日志分为每日或每月日志。
# wget https://files.cnblogs.com/files/crazyzero/cronolog-1.6.2.tar.gz # tar -zxf cronolog-1.6.2.tar.gz # cd cronolog # ./configure # make && make install
# which cronolog # /usr/local/sbin/cronolog
编辑tomcat下的bin/catalina.sh(注:行数不一定是我这个,找到语句为主)
将以上两处内容替换为:
org.apache.catalina.startup.Bootstrap "$@" start 2>&1 |/usr/local/sbin/cronolog "$CATALINA_BASE/logs/catalina-%Y-%m-%d.out" &
完成后,重新启动tomcat即可。
3、使用logrotate进行切割。
在/etc/logrotate.d下,新建tomcatrotate,编辑tomatrotate,写入如下内容:
/usr/local/tomcat7.0.79/logs/catalina.out { daily rotate 15 missingok dateext compress notifempty copytruncate }
daily 指定转储周期为每天
rotate 15 指定日志文件删除之前转储的次数,0指没有备份,5指保留5个备份
missingok 如果日志不存在则忽略该警告信息
dateext 文件后缀是日期格式,也就是切割后文件是:xxx.log-20150828.gz
compress 通过gzip压缩转储以后的日志(gzip -d xxx.gz解压)
notifempty 如果是空文件的话,不转储
copytruncate 用于还在打开中的日志文件,把当前日志备份并截断
/usr/local/tomcat7.0.79/logs/catalina.out 指定catalina.out的路径
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。