1、tomcat的訪問日誌AccessLogs定期或者定量刪除 a 開啟tomcat訪問日誌 編輯${catalina}/conf/server.xml文件.註:${catalina}是tomcat的安裝目錄 把以下的註釋(<!-- -->)去掉即可。 <!--<ValveclassName="or ...
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>