1、用log4j接管tomcat的日志输出; 2、用cronolog来分隔. 个人偏向第一种, 无他, 因其简单, 且涉及几率问题, 改动的地方少, 改动得越少, 出错的几率越少, 最重要的是log4j更加熟悉, 也就是说更好维护, so, why not.
ok, 开动.
step 1:
需要为tomcat添加3个jar包, 分别为 log4j-1.2.9.jar、tomcat-juli.jar、tomcat-juli-adapters.jar. 第一个jar无需多说, 只需稍微在网上找一下就能搜索出一大堆, 后面那2个jar, 可以在tomcat的官网上找得到, 下载的时候需要注意tomcat版本: http://www.apache.org/dist/tomcat/tomcat-7/v7.0.69/bin/ extras/. 看到下载地址中的 extras没, 意思是说这个2个jar包不是在tomcat底下, 需要额外添加.
step 2:
把上面3个jar包拷贝到/opt/tomcat/lib目录下(新下载的tomcat在这个lib目录下是不会有这3个jar包的, 其他不敢说, 但是7.0.69这个版本是绝对没有的).
step 3:
修改context.xml中的
step 4:
好了, 到了这一步, 该为log4j定义一个日志输出的配置, 嗯, 就叫log4j.properties好了. 等等, 这个文件的名字还真不能胡改, 它名字必须是这个, 不然还需要改点别的东西, 至于改啥, 就不说了. 配置好了之后, 把log4j.properties文件放到/opt/tomcat/lib目录下. 以下为log4j.properties的配置内容:
log4j.rootLogger = INFO, CONSOLE, CATALINA log4j.logger.org.springframework.web.servlet.handler.SimpleUrlHandlerMapping = INFO log4j.logger.org.springframework.beans.factory.xml.XmlBeanDefinitionReader = INFO #log4j.logger.org.hibernate.cfg.annotations.EntityBinder = WARN #log4j.logger.org.hibernate.cfg.AnnotationBinder = WARN #log4j.logger.org.hibernate.cfg.annotations.CollectionBinder = WARN 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 log4j.logger.de.javakaffee.web.msm = ERROR log4j.logger.org.apache.catalina.core = INFO, LOCALHOST log4j.logger.org.apache.catalina.session = INFO, LOCALHOST 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 log4j.appender.CATALINA = org.apache.log4j.DailyRollingFileAppender log4j.appender.CATALINA.file = ${catalina.base}/logs/catalina. log4j.appender.CATALINA.encoding = UTF-8 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.CATALINA.append = true log4j.appender.LOCALHOST = org.apache.log4j.DailyRollingFileAppender log4j.appender.LOCALHOST.file = ${catalina.base}/logs/localhost. log4j.appender.LOCALHOST.encoding = UTF-8 log4j.appender.LOCALHOST.datePattern = '.'yyyy-MM-dd'.log' #log4j.appender.LOCALHOST.maxFileSize = 100MB #log4j.appender.LOCALHOST.maxBackupIndex = 10 log4j.appender.LOCALHOST.layout = org.apache.log4j.PatternLayout log4j.appender.LOCALHOST.layout.conversionPattern = %d [%t] %-5p %c - %m%n log4j.appender.LOCALHOST.append = true log4j.appender.MANAGER = org.apache.log4j.DailyRollingFileAppender log4j.appender.MANAGER.file = ${catalina.base}/logs/manager. 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.MANAGER.append = true log4j.appender.HOST-MANAGER = org.apache.log4j.DailyRollingFileAppender log4j.appender.HOST-MANAGER.file = ${catalina.base}/logs/host-manager. 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.HOST-MANAGER.append = true
值得注意的是日志保存的文件名要保留" ."号,否则不会分类.
step 5:
重命名/opt/tomcat/config/logging.properties, 变成/opt/tomcat/config/logging.properties.bak. 不建议删除, 万一以后要恢复呢
step 6:
重启服务, 然后晚上别关机, 第二天就可以看到好消息了. 好, 就这样.