`

Using Log4j in Tomcat 6.0.x & 7.0.x

 
阅读更多

The paper is part of http://tomcat.apache.org/tomcat-6.0-doc/logging.html

 

Using Log4j

This section explains how to configure Tomcat to use log4j rather than java.util.logging for all Tomcat's internal logging.

Note: The steps described in this section are needed when you want to reconfigure Tomcat to use Apache log4j for its own logging. These steps are not needed if you just want to use log4j in your own web application. — In that case, just put log4j.jar and log4j.properties into WEB-INF/lib and WEB-INF/classes of your web application.

The following steps describe configuring log4j to output Tomcat's internal logging.

  1. Create a file called log4j.properties with the following content and save it into $CATALINA_BASE/lib
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
  1. Download Log4J (v1.2 or later).
  2. Download or build tomcat-juli.jar and tomcat-juli-adapters.jar that are available as an "extras" component for Tomcat. See Additional Components documentation for details.

    This tomcat-juli.jar differs from the default one. It contains the full Apache Commons Logging implementation and thus is able to discover the presense of log4j and configure itself.

  3. 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".
  4. 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:

    • 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.
    • The old tomcat-juli.jar in $CATALINA_HOME/bin is still referenced by manifest of bootstrap.jar and thus will be implicitly present on Tomcat's classpath. The startup scripts configure$CATALINA_BASE/bin/tomcat-juli.jar to be earlier on the classpath than bootstrap.jar, and so it should have higher priority. Thus it should be OK, but consider removing the unneeded copy of tomcat-juli.jar (copy it into all other $CATALINA_BASEs that do not use log4j). Tomcat 7 does not have this issue.
  5. Delete $CATALINA_BASE/conf/logging.properties to prevent java.util.logging generating zero length log files.

  6. Start Tomcat

This log4j configuration mirrors the default java.util.logging setup that ships with Tomcat: both the manager and host-manager apps get an individual log file, and everything else goes to the "catalina.log" log file. Each file is rolled-over once per day.

You can (and should) be more picky about which packages to include in the logging. Tomcat defines loggers by Engine and Host names. For example, for a more detailed Catalina localhost log, add this to the end of the log4j.properties above. Note that there are known issues with using this naming convention (with square brackets) in log4j XML based configuration files, so we recommend you use a properties file as described until a future version of log4j allows this convention.

log4j.logger.org.apache.catalina.core.ContainerBase.[Catalina].[localhost]=DEBUG
log4j.logger.org.apache.catalina.core=DEBUG
log4j.logger.org.apache.catalina.session=DEBUG

Be warned: a level of DEBUG will produce megabytes of logging and slow startup of Tomcat. This level should be used sparingly when debugging of internal Tomcat operations is required.

Your web applications should certainly use their own log4j configuration. This is valid with the above configuration. You would place a similar log4j.properties file in your web application's WEB-INF/classes directory, and log4jx.y.z.jar into WEB-INF/lib. Then specify your package level logging. This is a basic setup of log4j which does *not* require Commons-Logging, and you should consult the log4j documentation for more options. This page is intended only as a bootstrapping guide.

Additional notes

  • This exposes log4j libraries to the web applications through the Common classloader. See class loadingdocumentation for details.

    Because of that, the web applications and libraries using Apache Commons Logging library are likely to automatically choose log4j as the underlying logging implementation.

  • The java.util.logging API is still available, for those web applications that use it directly. The${catalina.base}/conf/logging.properties file is still referenced by Tomcat startup scripts.

    Removal of ${catalina.base}/conf/logging.properties file, mentioned as one of the steps, just causesjava.util.logging to fallback to the default configuration as configured in JRE, which is to use a ConsoleHandler and do not create any files.

分享到:
评论
发表评论

文章已被作者锁定,不允许评论。

相关推荐

    Adapting AWStats for IBM WebSphere Portal 6.0.x and virtual portals

    In IBM WebSphere Portal 6.0.x, the functionality to track site usage remains intact, but the Tivoli Web Site Analyzer, which was used in version 5.1 to chart the log files, has been deprecated....

    log4cxx For VC6.0

    log4cxx是Apache软件基金会的开源项目,它是log4j的C++实现,旨在提供与Java平台上的log4j相当的功能。该框架提供了灵活的日志级别,包括DEBUG、INFO、WARN、ERROR和FATAL,允许开发者根据需要调整日志输出的详细...

    Csharp.6.0.and.the.NET.4.6.Framework.7th.Edition.1484213335.epub

    This new 7th edition of Pro C# 6.0 and the .NET 4.6 Platform has been completely revised and rewritten to reflect the latest changes to the C# language specification and new advances in the .NET ...

    Eltima.Software.Virtual.Serial.Port.Driver.v6.0.1.115

    Virtual Serial Port Driver is an advanced utility, which emulates unlimited number of RS232 serial ports connected via virtual null-modem cable using special driver, which can be easily included in ...

    apache-log4j-2.13.3-bin.zip

    ERROR StatusLogger Log4j2 could not find a logging implementation. Please add log4j-core to the classpath. Using SimpleLogger to log to the console 解决上述问题,可以下载此包。

    PROXMOX VE 6.0 ADMINISTRATION GUIDE.pdf

    Proxmox VE is a platform to run virtual machines and ... All management tasks can be done using our web-based management interface, and even a novice user can setup and install Proxmox VE within minutes.

    log4j-1.2.14.jar

    1.2.15有个Bug,会提示log4j:ERROR LogMananger.repositorySelector was null likely due to error in class reloading, using NOPLoggerRepository,大家使用1.2.14替换1.2.15即可解决!

    CustomRollingFileLogger_Log4报表封装_

    1用NuGet安装log4net2将CustomRollingFileLogger.cs这个类文件导入到工程3使用方法using log4net;...//创建一个log对象 假设变量hwnd是窗口句柄ILog log = Common.CustomRollingFileLogger.GetCustomLogger(Thread....

    解析Tomcat的启动脚本–catalina.bat

    Tomcat 的三个最重要的启动脚本: startup.bat catalina.bat setclasspath.bat 上一篇咱们分析了 startup.bat 脚本 这一篇咱们来分析 catalina.bat 脚本. 至于 setclasspath.bat 这个脚本, 相信看完这一篇, 就可以...

    Game.Programming.using.Qt.5.x.Beginners.Guide.2nd

    Game.Programming.using.Qt.5.x.Beginners.Guide.2nd

    using multivariate statistics (5th Edition) 多元统计 part1

    This long-awaited revision reflects extensive updates throughout, especially in the areas of Data Screening (Chapter 4), Multiple Regression (Chapter 5), and Logistic Regression (Chapter 12)....

    Packt.Game.Programming.using.Qt.5.x.Beginners.Guide.2nd.Edition.2018

    Packt.Game.Programming.using.Qt.5.x.Beginners.Guide.2nd.Edition.2018

    net6 控制台+ log4net+写数据库+自定义字段

    在.NET 6框架下,构建一个控制台应用并结合log4net进行日志记录,同时将日志数据存储到数据库并支持自定义字段,是一项常见的系统监控和故障排查任务。以下将详细介绍如何实现这一功能。 首先,log4net是Apache的一...

    Meadow, F. B., Jr., Wallbrown, F. H., & Litwack, L. (Eds.). (1982). Using guidance skills in the classroom. Springfield, IL: Charles C Thomas, 300 pp., [dollar]29.75

    Using guidance skills in the classroom. Springfield, IL: Charles C Thomas, 300 pp., [dollar]29.75 346 Book Reviews ineffective social commitments, jobs and poverty, and cultural misconceptions; ...

    Log4Net相关类库文件.rar

    Log4Net是一个强大的日志记录框架,广泛应用于.NET平台,为开发者提供了丰富的功能来记录应用程序中的事件、错误、警告和调试信息。它以其灵活性、可配置性和可扩展性著称,使得开发人员能够轻松地控制日志输出的...

    Iphlpapi.h&ipHlpApi.lib

    Using this set of APIs can easily change the computer' s network settings or extract the relevant information. It also provides a messaging mechanism to notify the application when the change in the ...

    using lua in VC6.0

    标题“using lua in VC6.0”提示我们这个压缩包包含了一个关于在Visual C++ 6.0(简称VC6.0)环境下使用Lua脚本语言的实例。Lua是一种轻量级、解释型的编程语言,常用于游戏开发、脚本编写、配置管理等场合。在VC6.0...

    教程-APP.using.HTML5,.CSS3.&.JavaScript.zip

    Beginning iPad and iPhone Apps with HTML5 CSS3 and JAVAScript.pdf Pro.Android.Web.Apps.Develop.for.Android.using.HTML5,.CSS3.&.JavaScript.pdf

    Log4net自定义信息(变量或属性或字段)存入数据库

    using log4net; ... private static readonly ILog log = LogManager.GetLogger(typeof(MyClass)); ... log4net.ThreadContext.Properties["CustomVar"] = myVariable; log.Info("This is an informative message.")...

    org.in2bits.myxls.dll

    Asp.Net使用org.in2bits.MyXls.dll操作excel首先下载org.in2bits.MyXls.dll 添加命名空间: using org.in2bits.MyXls; using System.IO; 思路: 添加引用 (using org.in2bits.MyXls)→ 创建空xls文档...

Global site tag (gtag.js) - Google Analytics