tomcat8下用Log4j 按文件大小,生成catalina.out日誌文件

1、下載所需類包

一、下載log4j下載1.2之後的版本html

下載地址:http://logging.apache.org/log4j/1.2/download.htmljava

二、從extras目錄下載tomcat-juli.jar,tomcat-juli-adapters.jarapache

下載地址爲:http://www.apache.org/dist/tomcat/tomcat-8/v8.0.35/bin/extras/ (若是tomcat爲其餘版本,請下載對應版本的類包)tomcat

使用LOG4J來接管tomcat8的日誌文件必需要下載三個文件:log4j-1.2.17.jar,tomcat-juli.jar,tomcat-juli-adapters.jar。app

 

2、修改相關配置文件

一、若是你想全局配置tomcat使用log4j:spa

If you want to configure Tomcat to use log4j globally:日誌

  • Put log4j.jar and tomcat-juli-adapters.jar from "extras" into $CATALINA_HOME/lib.
  • Replace $CATALINA_HOME/bin/tomcat-juli.jar with tomcat-juli.jar from "extras".

二、若是你想配置$CATALINA_HOME 和$CATALINA_BASE爲不一樣的目錄(即多實例配置方式),而且只有$CATALINA_BASE實例運行採用log4j:code

If you are running Tomcat with separate $CATALINA_HOME and $CATALINA_BASE and want to configure to use log4j in a single $CATALINA_BASE only:htm

  • Create $CATALINA_BASE/bin and $CATALINA_BASE/lib directories if they do not exist.
  • Put log4j.jar and tomcat-juli-adapters.jar from "extras" into $CATALINA_BASE/lib
  • Put tomcat-juli.jar from "extras" as $CATALINA_BASE/bin/tomcat-juli.jar(須要覆蓋原有的
  • If you are running with a security manager, you would need to edit the $CATALINA_BASE/conf/catalina.policy file to adjust it to using a different copy of tomcat-juli.jar.

三、刪除$CATALINA_BASE/conf/logging.propertiesci

Delete $CATALINA_BASE/conf/logging.properties to prevent java.util.logging generating zero length log files.

四、建立log4j.properties文件,並把該文件保存到$CATALINA_BASE/lib

log4j.properties的內容如 下:相關參數根據須要能夠相應修改

log4j.rootLogger=INFO, CATALINA

# Define all the appenders
log4j.appender.CATALINA=org.apache.log4j.RollingFileAppender
log4j.appender.CATALINA.File=${catalina.base}/logs/catalina.out
log4j.appender.CATALINA.MaxFileSize=10240KB
log4j.appender.CATALINA.MaxBackupIndex=100
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-HH-mm'.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.log
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.log
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.log
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

相關文章
相關標籤/搜索