• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

log4j in tomcat - changing properties file

 
Bartender
Posts: 2911
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
is there a way that i can change the log4j properties files without directly editing it ?

I need to find a way to adjust the log levels using apache console (similar to jboss and weblogic)
 
Saloon Keeper
Posts: 27762
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tomcat itself doesn't use Log4j these days normally, and the docs on the tomcat website are just confusing on that part, since they fail to make it clear that the "logging" topic in their documentation is to customize logging of the Tomcat SERVER and not the applications running IN that server. The apps have to setup their own logging on a per-application basis.

Normally, you'd configure a webapp's log4j by placing the log4.properties or log4j.xml file in the webapp's WEB-INF/classes directory (that is, in the application's classpath).

But that's a static file and you'd have to edit the WAR to change it.

If you want something more dynamic, you can customize the Log4j startup by writing Log4J java code and placing it in the init() method of the first servlet that the app starts (write a dummy servlet if you need to). You can pull custom config information via JNDI from the web.xml file, which can be overridden by the application server. Or from a custom properties/xml file external to the webapp. Or by splitting the difference and using JNDI to find the external log4j properties file you want to use. For example, one for normal production and one for panic mode.

If you're really serious, you can also dynamically modify the Log4 settings on the fly by coding log control functions into your webapp and invoking the Log4 control API.
 
salvin francis
Bartender
Posts: 2911
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Holloway wrote:... and invoking the Log4 control API.


i think i should research on that. maybe its the solution for my problem.
thanks

btw, i have no idea abt jndi.
 
salvin francis
Bartender
Posts: 2911
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I searched and searched and searched in vain,
but i really could not at all gain,
I searched the ground and the sky,
could not find Log4 control API

 
Tim Holloway
Saloon Keeper
Posts: 27762
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


http://logging.apache.org/log4j/1.2/apidocs/index.html

And, to help figure out how to use it:

http://logging.apache.org/log4j/1.2/manual.html
 
salvin francis
Bartender
Posts: 2911
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Holloway wrote:

http://logging.apache.org/log4j/1.2/apidocs/index.html

And, to help figure out how to use it:

http://logging.apache.org/log4j/1.2/manual.html



still it does not solve my problem.
 
salvin francis
Bartender
Posts: 2911
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let me give a simple example,
in jboss for example, we can goto the server web console and then open a Mbean (org.jboss.mx.modelmbean.XMBean)
then we can change the value of its setLoggerLevel() on the fly,
we can then give it any value we want and jboss will change the value of the logger with the new specified level,
this does not modify the property files, the value is retained until the server is on,

effectively, a client can remotely change the logging level without changing any file or using ftp and things like that
jboss exposes some objects that can be manipulated on the fly !!

i think that tomcat too may provide a similar functionality. Does any one have any ideas ???
 
Tim Holloway
Saloon Keeper
Posts: 27762
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tomcat itself can be monitored and controlled using MBeans. Applications can also implement MBeans for monitoring and control. What drove me crazy was trying to figure out how they plugged themselves into the Tomcat monitoring framework. Nobody seems to write about this.

What actually happens is that both your own app's MBeans and Tomcat's MBeans are registered in the JVM that runs Tomcat (and by extension, all its contained processes, including webapps). They don't really know about each other, and there's no special Tomcat-specific logic required.

So yes, you can create an MBean that displays and controls the log4j logging subsystem if you like. Or create a control servlet/JSP to do so. Whichever you prefer. Or both!
 
salvin francis
Bartender
Posts: 2911
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So,
Is there any already existent Mbean in tomcat that can help me to configure tomcat in a similar fashion ?
 
Tim Holloway
Saloon Keeper
Posts: 27762
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

salvin francis wrote:So,
Is there any already existent Mbean in tomcat that can help me to configure tomcat in a similar fashion ?



No, because it's not Tomcat you need to configure, it's the application. Like I mentioned before, the current version of Tomcat doesn't even use Log4j itself, and applications are free to use whatever logging framework they want on a per-application basis. So you'd have to create an MBean component as part of the app you want to configure and make the calls to the Log4j API as part of the implementation of that MBean.
 
salvin francis
Bartender
Posts: 2911
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
so, you mean that i should develop a custom MBean that does so ???
 
reply
    Bookmark Topic Watch Topic
  • New Topic