Tomcat lib和每个Webapp中的log4j [英] log4j in Tomcat lib and in Each Webapp

查看:83
本文介绍了Tomcat lib和每个Webapp中的log4j的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将Apache Jackrabbit用作内容存储库,用于将两个Web应用程序部署到单个tomcat容器中.为了将jackrabbit作为jndi资源,我在tomcat/lib中添加了jackrabbit jar及其所有依赖项(包括slf4j和log4j),以使其可用于我的所有Web应用程序.但是,日志记录存在问题...

I am using Apache Jackrabbit as a content repository for a couple of web applications deployed to a single tomcat container. To get jackrabbit as a jndi resource I have added the jackrabbit jar and all it's dependencies (which includes slf4j and log4j) in tomcat/lib to make it available to all my web apps. However there is a problem with logging...

在启动时,log4j抱怨找不到附件:

On start up log4j complains that there are no appends found:

log4j:WARN No appenders could be found for logger (org.apache.jackrabbit.core.config.ConfigurationErrorHandler).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

现在,jackrabbit和我的Web应用程序都不会生成任何日志记录语句.

Now neither jackrabbit nor my web applications produce any logging statements.

我尝试将log4j.xml配置添加到lib目录,但不行.还尝试了将其直接添加到野兔罐中而没有运气.

I tried adding a log4j.xml config to the lib directory, no go. Also tried adding it directly in the jackrabbit jar with no luck.

如何为tomcat/lib中的依赖项以及tomcat中的每个Web应用程序配置log4j?

How can I configure log4j for a dependency in the tomcat/lib as well as for each web application in tomcat?

推荐答案

从log4j警告中很明显,您的应用程序中的log4j没有初始化,我不确定您在应用程序中的表现如何,但是现在您知道可以尝试一下. 我在应用程序中使用了相同的方法,其中所有日志记录jar都存在于服务器lib目录中.

From the log4j warning it is very clear that in your application log4j is not initialized, I am not sure earlier how you was doing in your application but for now you can try this. I have used the same approach in my appication where all the logging jars was present in server lib directory.

  1. 将log4j.properties放入项目的WEB-INF目录中,并在应用程序启动期间要求Servlet对其进行初始化.您可以编写一个启动servlet,并可以在init方法中将此属性文件作为参数传递给init.

例如:在您的web.xml中是这样的.

e.g : in your web.xml something like this.

    <servlet>
    <servlet-name>MyLog4JInitServlet</servlet-name>
    <servlet-class>test.MyLog4JInitServlet</servlet-class>
    <init-param>
        <param-name>log4j-properties-location</param-name>
        <param-value>WEB-INF/log4j.properties</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

您可以像这样在servlet中使用init方法来初始化log4j.

And you can have init method in your servlet like this to initialize log4j.

public void init(ServletConfig config) throws ServletException {
    String log4jLocation = config.getInitParameter("log4j-properties-location");

    ServletContext sc = config.getServletContext();

    if (log4jLocation == null) {
        BasicConfigurator.configure();
    } else {
        String webAppPath = sc.getRealPath("/");
        String log4jProp = webAppPath + log4jLocation;
        File logFile= new File(log4jProp);
        if (logFile.exists()) {
            System.out.println("Initializing log4j with: " + log4jProp);
            PropertyConfigurator.configure(log4jProp);
        } else {
            System.err.println("*** " + log4jProp + " file not found, so initializing log4j with BasicConfigurator");
            BasicConfigurator.configure();
        }
    }
    super.init(config);
}

欢呼

这篇关于Tomcat lib和每个Webapp中的log4j的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆