This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Servlets and the fly likes Help with Log4j Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Reply locked New topic
Author

Help with Log4j

Praveen Dharmavaram
Ranch Hand

Joined: Nov 05, 2002
Posts: 59
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
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

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.
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

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

Joined: Feb 20, 2001
Posts: 3695

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

Joined: Nov 05, 2002
Posts: 59
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

Joined: Feb 20, 2001
Posts: 3695

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

Joined: Nov 05, 2002
Posts: 59
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.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

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


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help with Log4j
 
Similar Threads
Having problems reading a file from servlet in Windows XP with Tomcat
invalid console appender .....
Tomcat Server Running Status
Spring and Quartz integration
invalid console appender config detected, console stream is looping