一、日志分割所需jar包
1.下载tomcat apache-tomcat-7.0.79.tar.gz
地址:http://www.apache.org/dist/tomcat/tomcat-7/
2、 压缩包中有三个jar包:
log4j-1.2.16.jar
tomcat-juli-adapters.jar 下载地址:http://www.apache.org/dist/tomcat/tomcat-7/v7.0.79/bin/extras/
tomcat-juli.jar 下载地址:http://www.apache.org/dist/tomcat/tomcat-7/v7.0.79/bin/extras/
说明:如果不是tomcat7版本,请去官网下载对应版本的tomcat-juli.jar,tomcat-juli-adapters.jar类包,
下载地址: http://www.apache.org/dist/tomcat 在对应版本中的bin/extras 路径下
二、修改相关配置文件
1、 修改${catalina.home}/conf/context.xml
修改:<Context >为<Context swallowOutput="true" >只有这样才能完全的把tomcat的stdout给接管过来。
2、 复制log4j-1.2.16.jar和tomcat-juli-adapters.jar 到tomcat的lib下
3、 复制tomcat-juli.jar到${catalina.home}/bin下,在该目录会存在该文件,覆盖即可
4、 建立log4j.properties,并把其放到到tomcat的/lib下
Log4j.properties的内容如下:相关参数根据需要可以相应修改
log4j.rootLogger=INFO, CATALINA
# Define all the appenders
log4j.appender.CATALINA=org.apache.log4j.DailyRollingFileAppender
#修改tomcat 日志存放路径
log4j.appender.CATALINA.File=/home/richmail/logs/tomcat/catalina.out
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
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
#修改tomcat 日志存放路径
log4j.appender.LOCALHOST.File=/home/richmail/logs/tomcat/localhost.log
log4j.appender.LOCALHOST.Append=true
log4j.appender.LOCALHOST.Encoding=UTF-8
log4j.appender.LOCALHOST.DatePattern='.'yyyy-MM-dd
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
#修改tomcat 日志存放路径
log4j.appender.MANAGER.File=/home/richmail/logs/tomcat/manager.log
log4j.appender.MANAGER.Append=true
log4j.appender.MANAGER.Encoding=UTF-8
log4j.appender.MANAGER.DatePattern='.'yyyy-MM-dd
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
#修改tomcat 日志存放路径
log4j.appender.HOST-MANAGER.File=/home/richmail/logs/tomcat/host-manager.log
log4j.appender.HOST-MANAGER.Append=true
log4j.appender.HOST-MANAGER.Encoding=UTF-8
log4j.appender.HOST-MANAGER.DatePattern='.'yyyy-MM-dd
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
5. 修改bin/catalina.sh
a. 屏蔽或删除如下内容:
(1) if [ -z "$CATALINA_OUT" ] ; then
CATALINA_OUT=$CATALINA_BASE/logs/catalina.out
(2) touch "$CATALINA_OUT"
(3) >> "$CATALINA_OUT" 2>&1 "&" (两处)
b. 修改org.apache.catalina.startup.Bootstrap "$@" start \ 内容 (两处)
修改为:org.apache.catalina.startup.Bootstrap "$@" start & \
6. 修改conf/server.xml中localhost_access_log日志路径
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="/home/richmail/logs/tomcat "
prefix="localhost_access_log." suffix=".txt"
pattern="%h %l %u %t "%r" %s %b" />
7. 删除 conf/logging.properties 文件