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 "Horry, Adam J" <ad...@lmco.com> on 2004/06/11 19:06:01 UTC

Separate log file per servlet

The web server is configured to be one JVM for all the servlets. 
Numerous people are developing different classes. We want each 
Servlet to write to its own log. 

Class X 
{ 
 ... 
 xLog = getLogger( "DEBUG" ); 
 ... 
 xLog.info( "message" ); 
 ... 
} 

Class ServletY extends HttpServlet 
{ 
 ... 
 xLog = getLogger( "DEBUG" ); 
 ... 
 configureAndWatch( ); 
 ... 
 xLog.info( "message" ); 
 ... 
 someMethod() 
 { 
  new X(); 
 } 
} 

Class ServletZ extends HttpServlet 
{ 
 ... 
 xLog = getLogger( "DEBUG" ); 
 ... 
 configureAndWatch( ); 
 ... 
 xLog.info( "message" ); 
 ... 
 someMethod() 
 { 
  new X(); 
 } 
 ... 
} 

log4j.properties: 
 ... 
 Logger name="DEBUG" 
 Appender A 
 ... 
 Appender A 
 ... 

Class A extends DailyRollingFileAppender 
{ 
 String _file = null; 

 a( ) 
 { 
  super.init(); 
  setFile( "" ); 
 } 

 setFile( aFile ) 
 { 
  // Throw stack trace to figure out which servlet called you 
  ... 
  // add path and .log to get filename like /tmp/ServletY.log or 
  // /tmp/ServletZ.log 
  _file = "newFile"; 
 } 
} 

Now the problem: When the first servlet comes up, say ServletY, it 
configures and creates /tmp/ServletY.log. ServletY and X write to 
the file and everyone is happy. Now the next servlet comes up, say 
ServletZ. It configures and creates /tmp/ServletX.log. Now 
everything writes to this log but not the other. 

So I guess what I am looking for is how I can I do logging on a 
servlet basis that writes to a particular log. The various support 
classes need to call something generic because they do not know 
which specific servlet called them. 

Thanks, 
Adam

adam.j.horry@lmco.com



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


Re: Separate log file per servlet

Posted by Ceki Gülcü <ce...@qos.ch>.
Do you have control over class X? If you do, then you use a ThreadLocal 
variable to set the servlet name from inside the servlet and retreive the 
name from class X.

Assume the threadlocal variable is called TL.

This is what will happen:

   servlet1 sets the TL to "servlet1"
   servlet 1 creates an instance of X, say x1
   while constructing x1, x1 reads TL, the read value is "servlet1"
   the constructor retrieves a logger whose name depends on the value of TL
   servlet 1 calls a method on x1, say foo()
   x1.foo() logs with that logger specific to the x1
   x1.foo() eventually returns
   servlet1 eventually returns, before returning it sets TL to null

Make sense?


At 07:06 PM 6/11/2004, you wrote:
>The web server is configured to be one JVM for all the servlets.
>Numerous people are developing different classes. We want each
>Servlet to write to its own log.
>
>Class X
>{
>  ...
>  xLog = getLogger( "DEBUG" );
>  ...
>  xLog.info( "message" );
>  ...
>}
>
>Class ServletY extends HttpServlet
>{
>  ...
>  xLog = getLogger( "DEBUG" );
>  ...
>  configureAndWatch( );
>  ...
>  xLog.info( "message" );
>  ...
>  someMethod()
>  {
>   new X();
>  }
>}
>
>Class ServletZ extends HttpServlet
>{
>  ...
>  xLog = getLogger( "DEBUG" );
>  ...
>  configureAndWatch( );
>  ...
>  xLog.info( "message" );
>  ...
>  someMethod()
>  {
>   new X();
>  }
>  ...
>}
>
>log4j.properties:
>  ...
>  Logger name="DEBUG"
>  Appender A
>  ...
>  Appender A
>  ...
>
>Class A extends DailyRollingFileAppender
>{
>  String _file = null;
>
>  a( )
>  {
>   super.init();
>   setFile( "" );
>  }
>
>  setFile( aFile )
>  {
>   // Throw stack trace to figure out which servlet called you
>   ...
>   // add path and .log to get filename like /tmp/ServletY.log or
>   // /tmp/ServletZ.log
>   _file = "newFile";
>  }
>}
>
>Now the problem: When the first servlet comes up, say ServletY, it
>configures and creates /tmp/ServletY.log. ServletY and X write to
>the file and everyone is happy. Now the next servlet comes up, say
>ServletZ. It configures and creates /tmp/ServletX.log. Now
>everything writes to this log but not the other.
>
>So I guess what I am looking for is how I can I do logging on a
>servlet basis that writes to a particular log. The various support
>classes need to call something generic because they do not know
>which specific servlet called them.
>
>Thanks,
>Adam
>
>adam.j.horry@lmco.com
>
>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
>For additional commands, e-mail: log4j-user-help@logging.apache.org

-- 
Ceki Gülcü

      For log4j documentation consider "The complete log4j manual"
      ISBN: 2970036908 http://www.qos.ch/shop/products/clm_t.jsp  



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