You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Micael <ca...@harbornet.com> on 2002/12/25 20:45:09 UTC
log4j initialization: Tomcat, Struts Framework, or Application?
I am new to using log4j and am getting the standard error "log4j:ERROR No
appenders could be found for category (Actions). log4j:ERROR Please
initialize the log4j system properly." I don't know if this means that
Tomcat, Struts, or the Application (Chiki) is not initialized
properly. The manual has the following for Tomcat, and I don't know if I
should immediately get into that. Could someone kick me to the right place
to start? I did not have this problem with Tomcat 4.1.12, so I assume this
is some difference in Tomcat 4.1.18. However, the error seems to be
related to Struts actions, so I am not sure where to start. Thanks for any
assistance, which I don't really expect, since I have asked this question
before and have not gotten an answer.
package com.foo;
import org.apache.log4j.PropertyConfigurator;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.io.IOException;
public class Log4jInit extends HttpServlet {
public
void init() {
String prefix = getServletContext().getRealPath("/");
String file = getInitParameter("log4j-init-file");
// if the log4j-init-file is not set, then no point in trying
if(file != null) {
PropertyConfigurator.configure(prefix+file);
}
}
public
void doGet(HttpServletRequest req, HttpServletResponse res) {
}
}
Define the following servlet in the web.xml file for your web-application.
<servlet>
<servlet-name>log4j-init</servlet-name>
<servlet-class>com.foo.Log4jInit</servlet-class>
<init-param>
<param-name>log4j-init-file</param-name>
<param-value>WEB-INF/classes/log4j.lcf</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
LEGAL NOTICE
This electronic mail transmission and any accompanying documents contain
information belonging to the sender which may be confidential and legally
privileged. This information is intended only for the use of the
individual or entity to whom this electronic mail transmission was sent as
indicated above. If you are not the intended recipient, any disclosure,
copying, distribution, or action taken in reliance on the contents of the
information contained in this transmission is strictly prohibited. If you
have received this transmission in error, please delete the message. Thank
you
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>
Re: log4j initialization: Tomcat, Struts Framework, or
Application?
Posted by Jacob Kjome <ho...@visi.com>.
Ug, the first line is confusing. I had two different trains of thought
going there. It should read "why put the log4j configuration file in the
classpath".
Jake
At 10:04 PM 12/25/2002 -0600, you wrote:
>Why put the log4j configuration file in the classpath if you are
>initializing it in an init servlet? Just put it alongside your web.xml file in
>WEB-INF. Only put it in WEB-INF/classes if you want log4j to perform
>default initialization....and call it log4j.xml or log4j.properties
>otherwise default initialization won't find it (default initialization of
>log4j.xml was only added in log4j-.2.7, I believe).
>
>Also, you are expecting to find the config file via an absolute file
>path. This is very bad practice unless you check for getRealPath("/")
>returning null and provide alternate loading of the config file via an
>InputStream. The servlet API provides this capability. Use
>it. Actulally, there is no reason whatsoever to use getRealPath("/")
>unless you are going to use configureAndWatch() because that doesn't take
>an InputStream.
>
>In addition, you'd be wise to use a servlet context listener and do your
>log4j config in contextInitialized() and then clean up log4j in
>contextDestroyed() with LogManager.shutdown(). This guarantees only one
>call to either method during the entire lifecycle of the app. It is not
>guaranteed that an init() method of a servlet won't be called more than
>once. The container can destroy and re-initialize a servlet any time it wants.
>
>Sorry, I'm not sure why this config wouldn't be working for you in 4.1.18
>when it did in 4.1.12. But at least you can take some of the hints above
>and improve on your current log4j usage.
>
>Jake
>
>At 11:45 AM 12/25/2002 -0800, you wrote:
>>I am new to using log4j and am getting the standard error "log4j:ERROR No
>>appenders could be found for category (Actions). log4j:ERROR Please
>>initialize the log4j system properly." I don't know if this means that
>>Tomcat, Struts, or the Application (Chiki) is not initialized
>>properly. The manual has the following for Tomcat, and I don't know if I
>>should immediately get into that. Could someone kick me to the right
>>place to start? I did not have this problem with Tomcat 4.1.12, so I
>>assume this is some difference in Tomcat 4.1.18. However, the error
>>seems to be related to Struts actions, so I am not sure where to
>>start. Thanks for any assistance, which I don't really expect, since I
>>have asked this question before and have not gotten an answer.
>>
>>
>>
>>package com.foo;
>>
>>import org.apache.log4j.PropertyConfigurator;
>>import javax.servlet.http.HttpServlet;
>>import javax.servlet.http.HttpServletRequest;
>>import javax.servlet.http.HttpServletResponse;
>>import java.io.PrintWriter;
>>import java.io.IOException;
>>
>>public class Log4jInit extends HttpServlet {
>>
>> public
>> void init() {
>> String prefix = getServletContext().getRealPath("/");
>> String file = getInitParameter("log4j-init-file");
>> // if the log4j-init-file is not set, then no point in trying
>> if(file != null) {
>> PropertyConfigurator.configure(prefix+file);
>> }
>> }
>>
>> public
>> void doGet(HttpServletRequest req, HttpServletResponse res) {
>> }
>>}
>>
>>Define the following servlet in the web.xml file for your web-application.
>>
>>
>> <servlet>
>> <servlet-name>log4j-init</servlet-name>
>> <servlet-class>com.foo.Log4jInit</servlet-class>
>>
>> <init-param>
>> <param-name>log4j-init-file</param-name>
>> <param-value>WEB-INF/classes/log4j.lcf</param-value>
>> </init-param>
>>
>> <load-on-startup>1</load-on-startup>
>> </servlet>
>>
>>
>>
>>
>>LEGAL NOTICE
>>
>>This electronic mail transmission and any accompanying documents contain
>>information belonging to the sender which may be confidential and legally
>>privileged. This information is intended only for the use of the
>>individual or entity to whom this electronic mail transmission was sent
>>as indicated above. If you are not the intended recipient, any
>>disclosure, copying, distribution, or action taken in reliance on the
>>contents of the information contained in this transmission is strictly
>>prohibited. If you have received this transmission in error, please
>>delete the message. Thank you
>>
>>
>>
>>--
>>To unsubscribe, e-mail: <ma...@jakarta.apache.org>
>>For additional commands, e-mail: <ma...@jakarta.apache.org>
Re: log4j initialization: Tomcat, Struts Framework, or
Application?
Posted by Jacob Kjome <ho...@visi.com>.
Why put the log4j configuration file in the classpath if you are
initializing it in an init servlet? Just put it alongside your web.xml
file in
WEB-INF. Only put it in WEB-INF/classes if you want log4j to perform
default initialization....and call it log4j.xml or log4j.properties
otherwise default initialization won't find it (default initialization of
log4j.xml was only added in log4j-.2.7, I believe).
Also, you are expecting to find the config file via an absolute file
path. This is very bad practice unless you check for getRealPath("/")
returning null and provide alternate loading of the config file via an
InputStream. The servlet API provides this capability. Use
it. Actulally, there is no reason whatsoever to use getRealPath("/")
unless you are going to use configureAndWatch() because that doesn't take
an InputStream.
In addition, you'd be wise to use a servlet context listener and do your
log4j config in contextInitialized() and then clean up log4j in
contextDestroyed() with LogManager.shutdown(). This guarantees only one
call to either method during the entire lifecycle of the app. It is not
guaranteed that an init() method of a servlet won't be called more than
once. The container can destroy and re-initialize a servlet any time it wants.
Sorry, I'm not sure why this config wouldn't be working for you in 4.1.18
when it did in 4.1.12. But at least you can take some of the hints above
and improve on your current log4j usage.
Jake
At 11:45 AM 12/25/2002 -0800, you wrote:
>I am new to using log4j and am getting the standard error "log4j:ERROR No
>appenders could be found for category (Actions). log4j:ERROR Please
>initialize the log4j system properly." I don't know if this means that
>Tomcat, Struts, or the Application (Chiki) is not initialized
>properly. The manual has the following for Tomcat, and I don't know if I
>should immediately get into that. Could someone kick me to the right
>place to start? I did not have this problem with Tomcat 4.1.12, so I
>assume this is some difference in Tomcat 4.1.18. However, the error seems
>to be related to Struts actions, so I am not sure where to start. Thanks
>for any assistance, which I don't really expect, since I have asked this
>question before and have not gotten an answer.
>
>
>
>package com.foo;
>
>import org.apache.log4j.PropertyConfigurator;
>import javax.servlet.http.HttpServlet;
>import javax.servlet.http.HttpServletRequest;
>import javax.servlet.http.HttpServletResponse;
>import java.io.PrintWriter;
>import java.io.IOException;
>
>public class Log4jInit extends HttpServlet {
>
> public
> void init() {
> String prefix = getServletContext().getRealPath("/");
> String file = getInitParameter("log4j-init-file");
> // if the log4j-init-file is not set, then no point in trying
> if(file != null) {
> PropertyConfigurator.configure(prefix+file);
> }
> }
>
> public
> void doGet(HttpServletRequest req, HttpServletResponse res) {
> }
>}
>
>Define the following servlet in the web.xml file for your web-application.
>
>
> <servlet>
> <servlet-name>log4j-init</servlet-name>
> <servlet-class>com.foo.Log4jInit</servlet-class>
>
> <init-param>
> <param-name>log4j-init-file</param-name>
> <param-value>WEB-INF/classes/log4j.lcf</param-value>
> </init-param>
>
> <load-on-startup>1</load-on-startup>
> </servlet>
>
>
>
>
>LEGAL NOTICE
>
>This electronic mail transmission and any accompanying documents contain
>information belonging to the sender which may be confidential and legally
>privileged. This information is intended only for the use of the
>individual or entity to whom this electronic mail transmission was sent as
>indicated above. If you are not the intended recipient, any disclosure,
>copying, distribution, or action taken in reliance on the contents of the
>information contained in this transmission is strictly prohibited. If you
>have received this transmission in error, please delete the
>message. Thank you
>
>
>
>--
>To unsubscribe, e-mail: <ma...@jakarta.apache.org>
>For additional commands, e-mail: <ma...@jakarta.apache.org>