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

Help with Log4j

 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
I have the following code to initialize log4j in the init() method of a servlet.
public void init(ServletConfig config) throws ServletException{
String fileName = config.getInitParameter("LogService");
String configFile = getServletContext().getRealPath("WEB-INF/classes/"+fileName);
LogService logServ = new LogService(configFile);
}
How can I get an instance of the "logServ" in my .java class file.
Thanks
Praveen
 
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
The very cool thing about log4j, is that it configures itself if you name the config file: log4j.properties

Then all you need to do to use it isThis may not help you in your particular case, but it's good to know if you want 'quick and simple' logging.
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
the other cool thing about log4J is that if it isn't already configured, it will first check the classpath for the log4j.properties file.
Since the directory WEB-INF/classes is on the web application classpath, log4J should find it automatically. This means you can do away with your initialisation code and go straight to the code Mike gave you - you can ignore the 'logServ'
 
Mike Curwen
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Actually David, that's the same cool thing.


Although... you did provide more details.

And one final detail...

the 'logger' object mentioned in my code above, is the one that you use to log messages...

[ November 06, 2003: Message edited by: Mike Curwen ]
 
Praveen Dharmavaram
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
I am trying to acheive the same thing what you guys said. I initialize the log4j by passing in the properties file and everything is fine till then. In my properties file I do have two categories. I found that It wasnt functioning well. However it works fine, when i reinitialize it with the properties file in the .java classes. I am totally lost in what I should be doing. Please help me out.
Thanks
 
Mike Curwen
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
is your log4j.jar file in the WEB-INF/lib directory?

what do you mean by 'not working well'?

what does your log4j.properties file look like?
 
Praveen Dharmavaram
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Well..I finally got it working.The problem was with my log4j.properties file. I had a typo in it and dint realise it until you asked for it. Thanks a lot guys. You have been of real help to me . I was going nuts about it.
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Guys, cool stuff about Log4J, however, this isn't really the best forum for this discussion. I am going to move this, but I just need to figure out which forum would be best.
Mark
 
Mark Spritzler
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

Originally posted by Praveen Dharmavaram:
Well..I finally got it working.The problem was with my log4j.properties file. I had a typo in it and dint realise it until you asked for it. Thanks a lot guys. You have been of real help to me . I was going nuts about it.


Isn't that always the case.
So I will just close this thread, instead of moving it.
Mark
 
To get a wish, you need a genie. To get a genie, you need a lamp. To get a lamp, you need a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
    Bookmark Topic Watch Topic
  • New Topic