You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4j-user@logging.apache.org by Wyss Patrick <Pa...@mobilesolutions.ch> on 2007/01/08 14:02:20 UTC

AW: Loading sequence of log4j

jacob,

thanks for your advice. as you seem to be expert on servlets maybe you can tell me what you think of the way i used to handle my logs when i was doing servlets under jboss (it's been more than a year ago, so this all may be a little inaccurate):

1. i have 2 system properties: log-path and conf-path both pointing to subdirectories in the jboss directory
2. in contextInitialized() i load my log4j properties with PropertyConfigurator.configure(confPath + projectName + "/log4j.properties")
3. in the log4j.properties files i set the files to ${log-path}/projectName/servlet.log
4. from my servlets i can reload the whole thing if some special URL is called (..&reloadLog4j=mySecretPWD&...)

the advantages of this system is that
- i can reconfigure the logging "on-the-fly" without redeploying or restarting the servlet and/or app server
- it works on all jboss versions (e.g. the different classloading strategies) and also if log4j.jar is in a central possition
- i get all my logs into a directory which can be "served" by apache (to authorized users)

any comments?

cheers
patrick

> -----Ursprüngliche Nachricht-----
> Von: Jacob Kjome [mailto:hoju@visi.com]
> Gesendet: Dienstag, 26. Dezember 2006 18:28
> An: Log4J Users List
> Betreff: Re: Loading sequence of log4j
> 
> 
> 
> 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().getIni
> tParamete
>  >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: log4j-user-unsubscribe@logging.apache.org
> For additional commands, e-mail: log4j-user-help@logging.apache.org
> 
> 
> 
> 



---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-user-help@logging.apache.org


Re: AW: Loading sequence of log4j

Posted by Jacob Kjome <ho...@visi.com>.
At 07:02 AM 1/8/2007, you wrote:
 >jacob,
 >
 >thanks for your advice. as you seem to be expert on servlets maybe you
 >can tell me what you think of the way i used to handle my logs when i
 >was doing servlets under jboss (it's been more than a year ago, so
 >this all may be a little inaccurate):
 >
 >1. i have 2 system properties: log-path and conf-path both pointing to
 >subdirectories in the jboss directory
 >2. in contextInitialized() i load my log4j properties with
 >PropertyConfigurator.configure(confPath + projectName + "/log4j.properties")
 >3. in the log4j.properties files i set the files to
 >${log-path}/projectName/servlet.log
 >4. from my servlets i can reload the whole thing if some special URL
 >is called (..&reloadLog4j=mySecretPWD&...)
 >
 >the advantages of this system is that
 >- i can reconfigure the logging "on-the-fly" without redeploying or
 >restarting the servlet and/or app server
 >- it works on all jboss versions (e.g. the different classloading
 >strategies) and also if log4j.jar is in a central possition
 >- i get all my logs into a directory which can be "served" by apache
 >(to authorized users)
 >
 >any comments?
 >

Sounds fine if it meets your needs.  Of course 
loading the properties file via File IO means 
extra documentation about where your confPath is, 
when you could just deploy the config file with 
the app itself and use something like LogWeb [1] 
for runtime configuration changes.  It just 
reduces the moving parts.  That said, what you 
are doing is close to the same approach used on 
the projects I am working on, though the config 
file is actually in the appservers classpath, so 
Log4j's default logger repository gets configured 
automatically.  And we can use a standalone 
version of LogWeb to configure the default logger 
repository, which is shared by the entire 
server.  It's suits us fine because the entire 
server is dedicated to a couple related apps that 
we control.  No real need for a repository selector in this case.

It sounds like you have a decent system in 
place.  If it works for you, I wouldn't change it.


[1] http://www.codeczar.com/products/logweb/index.html

 >cheers
 >patrick
 >
 >> -----Ursprüngliche Nachricht-----
 >> Von: Jacob Kjome [mailto:hoju@visi.com]
 >> Gesendet: Dienstag, 26. Dezember 2006 18:28
 >> An: Log4J Users List
 >> Betreff: Re: Loading sequence of log4j
 >>
 >>
 >>
 >> 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().getIni
 >> tParamete
 >>  >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: log4j-user-unsubscribe@logging.apache.org
 >> For additional commands, e-mail: log4j-user-help@logging.apache.org
 >>
 >>
 >>
 >>
 >
 >
 >
 >---------------------------------------------------------------------
 >To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
 >For additional commands, e-mail: log4j-user-help@logging.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-user-help@logging.apache.org