1、tomcat的访问日志AccessLogs定期或者定量删除
a 开启tomcat访问日志
编辑${catalina}/conf/server.xml文件.注:${catalina}是tomcat的安装目录
把以下的注释(<!-- -->)去掉即可。
<!--<ValveclassName="org.apache.catalina.valves.AccessLogValve"
directory="logs" prefix="localhost_access_log."suffix=".txt"
pattern="common" resolveHosts="false"/>-->
下图为配置文件开启访问日志,以及对应参数的解释:
b extend the AccessLogValve by override this function: log(),设置定期或定量删除访问日志
public void emptyFile() { String logPath = System.getProperty("catalina.base"); // rotatable=false,定量清除只有一个日志文件localhost_access_log.log if (this.rotatable == false) { synchronized (this) { System.out.println("------log size is------:" + this.currentLogFile.length()); if (this.currentLogFile.length() > 10 * 1024) { this.currentLogFile.delete(); open(); } } } else { // 含有日期的日志清除方式` // localhost_access_log.2016-12-16.23.37.log // localhost_access_log.2016-12-16.23.39.log if (new File(logPath).isDirectory()) { // 获取文件夹中的文件集合 File[] logs = new File(logPath).listFiles(); // 设置系统这里设置的日期格式,和配置文件里的参数保持一致 SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd.HH.mm"); // 遍历集合 for (int i = 0; i < logs.length; i++) { File log = logs[i]; // 获取到第i个日志的名称,截取中间的日期字段,转成long型s int start = log.getName().indexOf(".") + 1; int end = log.getName().lastIndexOf("."); // 获取到的日志名称中的时间(2016-12-16.23.37) String dateStr = logPath.substring(start, end); // 将字符串型的(2016-12-16.23.37)转换成long型 long lonInt = 0; try { lonInt = dateFormat.parse(dateStr).getTime(); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("------old-log date is------:" + lonInt); // 系统时间减去日志名字中获取的时间差大于配置文件中设置的时间删除 if ((System.currentTimeMillis() - lonInt) / (1000 * 60) > 10) { log.delete(); open(); } } } } }
2、tomcat的普通日志(catlina.out、localhost)配置log4j日志框架实现日志回滚
Log4j配置tomcat日志,定期删除:
首先,需要下载3个jar包.
tomcat-juli.jar 和 tomcat-juli-adapters.jar、log4j.jar
http://www.apache.org/dist/logging/log4j/1.2.17/
http://www.apache.org/dist/tomcat/tomcat-7/v7.0.69/bin/extras/
3个jar包一次放入的目录为:
tomcat-juli-adapters.jar 和 log4j.jar放入 ($CATALINA_BASE/lib)目录下
tomcat-juli.jar放入 $CATALINA_BASE/bin/目录,直接覆盖原有的tomcat-juli.jar即可
删除$CATALINA_BASE/conf/logging.properties文件,防止java.util.logging生成零长度的日志文件。
修改Tomcat中的conf/context.xml文件,将<Context>为<Context swallowOutput="true">。
最后,在tomcat6.0.43/lib目录下创建一个log4j.properties, 加入如下内容并保存:
(这里以定期清理catalina.out为例)
******************************************************************************
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
重启tomcat,就可以看到java -Djava.util.logging.config.file变成了java -Dnop -Djava.util.logging.config.file,说明配置成功了
效果如下所示:
参考:
http://code.taobao.org/p/tomcat/diff/3/tc8.0.x/java/org/apache/catalina/valves/AccessLogValve.java
http://www.tuicool.com/articles/ymyiuiu
http://sanwen8.cn/p/520j7Qi.html
http://www.cnblogs.com/dimmacro/p/5566480.html
http://blog.csdn.net/jackljf/article/details/17428335
Shell 分析tomcat访问日志 格式
http://www.cnblogs.com/zhwj184/archive/2013/03/28/3027422.html
java类写入日志和定时删除日志
http://blog.csdn.net/high2011/article/details/50451216
http://blog.csdn.net/jsky_studio/article/details/41686597?spm=5176.8246799.blogcont.4.WnVUiV>