Be aware that system properties are shared by all webapps running inside one
tomcat instance.
That means, if you call System.setProperty("foo","bar") in a listener of
webapp1
all other webapps will get "bar" when they call System.getProperty("foo")
Not all servlet containers behave this way.
Maarten
On 12/26/06, Jacob Kjome <[EMAIL PROTECTED]> wrote:
Don't use a init servlet. The servlet spec makes no guarantee as to
when the servlet initialization will happen. It may or may not be at
container startup. Use a serlvet context listener. It has methods
that are guaranteed to be called at application startup and
application shutdown. Here's what I suggest...
Assuming you are using Tomcat...
1. Put log4j.jar in WEB-INF/lib and a dummy Log4j config file (see
below) in WEB-INF/classes. The dummy config file is meant to cover
for autoconfiguration if it runs before your manual configuration
runs, avoiding various bogus logging, warning messages from Log4j, or
other errors.
2. Put your real config file under WEB-INF, but not in WEB-INF/classes
3. Implement a servlet context listener and implement the
contextInitialized() method (off the top of my head, I think that's
what it is called). Set you system property first and then manually
configure log4j, pointing it to your real config file somewhere under
WEB-INF.
BTW, I don't recommend logging to a directory directly under the
application directory. The servlet spec makes no guarantee that you
have write access to the file system other than the provided
System.getProperty("java.io.temp") directory. I suggest you set the
system property via the command line using a -D parameter when you
start up the appserver. With Tomcat, you can provide this in the
CATALINA_OPTS system property...
CATALINA_OPTS=-Dadmin-console-abs-home=/my/path/to/logs
If you do this, you can bypass the dummy Log4j config file and skip
manual initialization. Just put your real config file in
WEB-INF/classes and let Log4j auto-configure itself, since the system
property is now guaranteed to be there before Log4j initializes itself.
Here's an example of a dummy Log4j config file to suppress Log4j
errors until you perform your manual initialization......
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<!-- This file is meant to satisfy/supress auto-configuration of the
default logger repository
when log4j.jar is included in WEB-INF/lib. This assumes that
manual configuration will be
performed later, usually on a non-default logger repository as
specified by a repository selector.
The purpose of supression is to avoid Log4j auto-configuration
finding a Log4j config file in a
parent classloader when it can't find one locally, which can
lead to either bogus
(but generally harmless) configuration of the default
repository or even a nasty stacktrace
if an incompatible Log4j config file is found (such as when
using Log4j-1.2.x in WEB-INF/lib
and Log4j-1.3 in the server's classpath. The XML formats are
generally incompatible!).
This file should be copied to WEB-INF/classes as "log4j.xml"
when building the .war file. -->
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/"
debug="false" threshold="debug">
<root>
<level value="off"/>
</root>
</log4j:configuration>
Jake
At 03:02 AM 12/26/2006, you wrote:
>Hi all
>
>I have this start up servlet that does the initializing some system
>parameters.
>
> <load-on-startup>1</load-on-startup>
>And i place this servlet on the first priority
>
>In the servlet i have something..
>System.setProperty("admin-console-abs-home",
>getServletContext().getRealPath("")+getServletConfig().getInitParamete
>r("adminConsoleLog"));
>
>So the log4j.xml uses something like
><param name="File" value="${admin-console-abs-home}"/>
>
>Sometimes the Start up servlet runs first and initalize the property. But
at
>times log4j starts first and giving an error
>log4j:ERROR setFile(null,true) call failed.
>
>So how can i ensure that log4j initalizes only after my start up servlet
>finishes initalizing?
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]