logging-log4j-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jacob Kjome <h...@visi.com>
Subject Re: Initializing log4j using a servlet question ..
Date Mon, 01 Nov 2004 04:56:23 GMT
At 09:27 PM 10/31/2004 -0700, you wrote:
 >Hi,
 >
 >Thanks. Yes I found the same out yesterday.
 >log4j.appender.A1.file=${log.home}/cw.log was what was missing. I have
 >log.home pointing to /Tomcat/webapps/mywebapp/WEB-INF/log/
 >
 >Now the other problem is about redirecting the exceptions like
 >NullPointerExceptions etc that get written to Tomcat/logs/catalina.out to
 >the ${log.home}/cw.log file. Is there a way to make Tomcat log all
 >exceptions resulting due to mywebapp to ${log.home}/cw.log??
 >
 >Do I have to do something regarding this in the Tomcat server.xml file?
 >
 >Following is an extract from the server.xml file. I have set the
 >swallowOutput = true for the Context of my webapp, but I do not know how
 >to redirect output to the ${log.home}/cw.log file (where log.home =
 >/Tomcat/webapps/mywebapp/WEB-INF/log/)
 >

Well, with the swallowOutput="true", you could use the 
ServletContextLogAppender in log4j-sandbox.  Check out the 
<http://cvs.apache.org/viewcvs.cgi/logging-log4j-sandbox/src/java/org/apache/log4j/servlet/ServletContextLogAppender.java?only_with_tag=LOG4J_SANDBOX_ALPHA3>LOG4J_SANDBOX_ALPHA3

tag if you use it.
http://cvs.apache.org/viewcvs.cgi/logging-log4j-sandbox/src/java/org/apache/log4j/servlet/ServletContextLogAppender.java

With this, all logging will go to the context log, such as the 
cw2004-10-30.log file you mention below.  You won't even need to set up a 
FileAppender.  Just set the ServletContextLogAppender's 
"servletContextPath" param to the path of your context, such as 
"/MyContext".  It's not a solution that puts all logging in your cw.log 
file of your FileAppender, but it will put all logging in the servlet 
context log file and you won't have to deal with worrying about whether you 
have access to the file system within your application (which isn't 
guaranteed by the servlet spec).  Make sure to read the javadoc for useage.

Jake

 >....
 >....
 ><Context path="/mywebapp" docBase="mywebapp" debug="0"
 >          swallowOutput="true"
 >          reloadable="true" crossContext="false">
 >    <Logger className="org.apache.catalina.logger.FileLogger"
 >     directory="webapps/mywebapp/WEB-INF/log"  prefix="cw" suffix=".log"
 >     timestamp="false"/>
 ></Context>
 >....
 >....
 >
 >This logs in a file cw2004-10-30.log. But I want to log in the cw.log
 >file. Any tips as to how I could accomplish this?
 >
 >Thanks.
 >
 >> The appender is writing to a file that is located in the same directory
 >> that the VM started.  How did you start Tomcat?  If from one of the
 >> scripts  in the CATALINA_HOME/bin directory, then that's where you log
 >> file will  exist.  If you started it as a service (and didn't do any
 >> special  configuration to reset the location where the JVM starts), then
 >> (and I'm  assuming Windows NT, 2k, or XP here) it will likely exist in
 >> c:\winnt\system32.
 >>
 >> If you want the appender to write the file to your configured location,
 >> then prefix the filename with the name of the system property you are
 >> setting, such as...
 >>
 >> log4j.appender.A1.file=${log.home}/cw.log
 >>
 >> I know this works in the XML config file.  I'm not 100% sure properly
 >> substitution works in properties files, mostly because I never use
 >> them.  But I would think this should work.
 >>
 >> Jake
 >>
 >> At 04:45 PM 10/29/2004 -0700, you wrote:
 >>  >Hi,
 >>  >
 >>  >I am trying to use this under Tomcat 5.0.27.
 >>  >
 >>  >I have the following log4j.properties file under WEB-INF/conf/
 >>  ># initialise root logger with level DEBUG and call it A1
 >>  >log4j.rootLogger=DEBUG, A1
 >>  ># set the appender to be FileAppender
 >>  >log4j.appender.A1=org.apache.log4j.FileAppender
 >>  >log4j.appender.A1.file=cw.log
 >>  ># set set that layout to be SimpleLayout
 >>  >log4j.appender.A1.layout=org.apache.log4j.SimpleLayout
 >>  >
 >>  >I have the following in my web.xml
 >>  >     <servlet>
 >>  >               <servlet-name>Log4jInitServlet</servlet-name>
 >>  >
 >> <servlet-class>com.somepackage.Log4jInitServlet</servlet-class>
 >>  >               <load-on-startup>1</load-on-startup>
 >>  >               <init-param>
 >>  >               <param-name>log4j-configuration</param-name>
 >>  >                       <param-value>conf/log4j.properties</param-value>
 >>  >           </init-param>
 >>  >           <init-param>
 >>  >                <param-name>log4j-log-path</param-name>
 >>  >                <param-value>WEB-INF/log</param-value>
 >>  >               </init-param>
 >>  >       </servlet>
 >>  >
 >>  >
 >>  >I do the following in my Log4jInitServlet:
 >>  >
 >>  >...
 >>  >String baseDir = getServletContext().getRealPath( "/" );
 >>  >String logPath = config.getInitParameter(log4j-log-path);
 >>  >logPath = baseDir + logPath;
 >>  >File logPathDir = new File( logPath );
 >>  >System.setProperty( "log.home", logPath );
 >>  >PropertyConfigurator.configureAndWatch( confFile );
 >>  >...
 >>  >
 >>  >When I start my tomcat engine, I do not get any exceptions but my log
 >> file does not get created? Am I missing something in my
 >>  >log4j.properties file while specifying log4j.appender.A1.file ? or
 >> something else? I do not get any exceptions in catalina.out.
 >>  >
 >>  >I am trying to understand if this problem is due to my configuration
 >> of Tomcat or the configuration of my logger?
 >>  >
 >>  >Mufaddal Khumri
 >>  >
 >>  >
 >>  >---------------------------------------------------------------------
 >> 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


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


Mime
View raw message