首页 > 代码库 > tomcat 6.x + log4j日志配置并按天(或大小)生成文件

tomcat 6.x + log4j日志配置并按天(或大小)生成文件

 
tomcat日志,默认路径在${catalina.home}/logs目录下,默认使用的是tomcat自己封装的logging工具类,默认配置文件使用的${catalina.home}/conf/logging.properties
默认的配置不满足按天或按大小将日志区分,用以备份或转移或删除,经常会出现日志文件超大,磁盘空间不够的情况(就算磁盘够,一段时间后的日志也应该及时清理和压缩起来)。
考虑到这种需求,使用log4j的配置,将tomcat的logging拦截起来,配置如下:
采用log4j配置,适用版本为:6.x(4.x或7.x的差别还是有的 不过不大),有配置属性的改动,步骤如下:
1. 修改${catalina.home}/conf/context.xml
修改:<Context >为<Context swallowOutput="true" >
2. 拷贝log4j.jar到${catalina.home}/lib下
3. 拷贝tomcat-juli-adapters.jar到${catalina.home}/lib下
4. 拷贝tomcat-juli.jar到${catalina.home}/bin下,在该目录会存在该文件,覆盖 即可
5. 拷贝log4j.properties到${catalina.home}/lib下,配置如下:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
log4j.rootLogger=INFO,CATALINA
log4j.logger.org.apache.catalina=WARN,CONSOLE
 
#log4j.logger.org.apache.catalina.core.ContainerBase.[Catalina].[localhost]=INFO
log4j.logger.org.apache.catalina.core=WARN,CATALINA
log4j.logger.org.apache.catalina.session=WARN,CATALINA
 
#log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender 
# Define all the appenders
log4j.appender.CONSOLE=org.apache.log4j.DailyRollingFileAppender
log4j.appender.CONSOLE.File=${catalina.home}/logs/catalina.out
log4j.appender.CONSOLE.Append=true
log4j.appender.CONSOLE.Encoding=UTF-8
# Roll-over the log once per day
log4j.appender.CONSOLE.DatePattern=‘.‘yyyy-MM-dd‘.log‘
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss.SSS} %-5p %c- %m%n
      
# Define all the appenders
log4j.appender.CATALINA=org.apache.log4j.DailyRollingFileAppender
log4j.appender.CATALINA.File=${catalina.home}/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{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5p %c- %m%n
 
log4j.appender.LOCALHOST=org.apache.log4j.DailyRollingFileAppender
log4j.appender.LOCALHOST.File=${catalina.home}/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{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5p %c- %m%n
 
log4j.appender.MANAGER=org.apache.log4j.DailyRollingFileAppender
log4j.appender.MANAGER.File=${catalina.home}/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{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5p %c- %m%n
 
log4j.appender.HOST-MANAGER=org.apache.log4j.DailyRollingFileAppender
log4j.appender.HOST-MANAGER.File=${catalina.home}/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{yyyy-MM-dd HH:mm:ss.SSS} [%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中的应用的log配置,若是不希望将日志输出到catalina.out文件中,可以去掉应用内部输出的stdout ,该logger会默认输出到catalina.out文件。
其余配置项按正常配置走起。