问题描述
我正在使用Apache JackRabbit作为内容存储库,用于部署到一个Tomcat容器的几个Web应用程序.为了使JackRabbit作为JNDI资源,我添加了tomcat/lib中的JackRabbit Jar及其依赖关系(包括SLF4J和Log4J),以使其可用于我所有的Web应用程序.但是,记录存在问题...
在启动log4j上抱怨找不到附录:
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应用程序都不会产生任何记录语句.
我尝试将log4j.xml配置添加到lib目录中,没有.还尝试在没有运气的情况下直接将其添加到jackrabbit罐子中.
如何为tomcat/lib以及tomcat中的每个Web应用程序配置log4j?
推荐答案
从log4j警告中很明显,在您的应用程序中,log4j尚未初始化,我不确定您在应用程序中的状况,但是现在您可以尝试使用此操作. 我在应用程序中使用了所有登录罐中都存在的应用程序中的方法.
- 将log4j.properties放入您的项目Web-Inf目录中,并要求您的Servlet在应用程序启动时间初始化它.您可以编写一个启动的servlet,在初始化方法中,您可以将此属性文件作为参数传递给初始化.
例如:在您的web.xml中.
<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.
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); }
欢呼
问题描述
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...
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.
Now neither jackrabbit nor my web applications produce any logging statements.
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.
How can I configure log4j for a dependency in the tomcat/lib as well as for each web application in tomcat?
推荐答案
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.
- Put the log4j.properties in your project WEB-INF directory and ask your servlet to initialize it during the application startup time. You can write a start up servlet and in the init method you can pass this properties file to the init as params.
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>
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); }
Cheers