This week's book giveaway is in the Java 9 forum.
We're giving away four copies of Java 9 Modularity: Patterns and Practices for Developing Maintainable Applications and have Sander Mak & Paul Bakker on-line!
See this thread for details.
Win a copy of Java 9 Modularity: Patterns and Practices for Developing Maintainable Applications this week in the Java 9 forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Log4j crap instructions  RSS feed

 
Greg Belyea
Greenhorn
Posts: 27
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looking at the instructions for Log4j, never having had to set up Logging before and constantly ridling my code with System.out's that need to be commented out all the time, i figure, what the hey. I'll implement log4j...

I am thinking there should be a simple example out there on how to set it up with my struts application, after sifting through the non applicable examples and the language of doublespeak, i am left 2 hours later with no logging set up.

I have dropped log4j jar into my lib directory under webapps, and i intended to have a class that extended action that i would set up a logger in. this way each action i had would inherently have a logger available to it without having to set up it's own. My system would not deal with the PropertyConfigurator at all, even though i could successfully import it, it would'nt compile any of the examples.

With Log4j ready and the following class skeleton, what would be my best way to set up a simple, very simple logger that my action classes could inherit? Once i get it working i guess i could conquer the more detailed aspects...



package com.cgi.excelsior.actions;

import javax.servlet.http.HttpServletRequest;
import javax.sql.DataSource;

import org.apache.struts.action.Action;
import org.apache.log4j.*;


public class ExcelsiorAction extends Action{


public DataSource getDatasource(HttpServletRequest request, String key)
{
return this.getDataSource(request, key);
}

}
 
Carol Enderlin
drifter
Ranch Hand
Posts: 1364
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good idea.

You were a little vague in your assault of log4j instructions. Have you checked the log4j documentation and in particular short log4j manual? Not sure there is anything specifically like "the best way to set up to use log4j with struts".

I responded yesterday to a log4j newbie in a post in this same forum, New to log4j. Pretty much the same info applies to your situation.

For a webapp, putting the log4 jar in web-inf/lib directory is a good bet. If you use a properties (or xml) file, it needs to be on the classpath.

There are different ways to use the Logger naming/hierarchy, so your mileage may vary with the following, it is my opinion: I'm not sure that you will get the information you need if you put a Logger in a base class to be used by all the classes that extend it. I've always put a Logger in each class (it's one line of code, OK 2 with the import). In the code below, when you get a logger you can pass in a String or a class. That information can be included in the log messages that are output. So, if all your classes use the same logger that information could not be used to distinguish them.





Basically from short log4j manual. Loggers created in two different classes, Bar in package com.foo and MyApp in default pacakge:

final static Logger logger = Logger.getLogger(MyApp.class);
final static Logger logger = Logger.getLogger(Bar.class);

Output shows "MyApp" and "com.foo.Bar":
0 [main] INFO MyApp - Entering application.
36 [main] DEBUG com.foo.Bar - Did it again!
51 [main] INFO MyApp - Exiting application.



One word of warning when setting up log4j with a struts application: Struts code may log lots of messages, depending on how it's configured. It can be helpful to direct that output to a separate log file, something like this added to properties file (if you have any problems getting struts going it can be helpful to look at that log):


copied bits and pieces from a fairly large working log4j.properties file, I may have missed something.
log4j.logger.org.apache=INFO,A3
log4j.additivity.org.apache.struts=false
log4j.appender.A3=org.apache.log4j.RollingFileAppender
log4j.appender.A3.File=struts_log4j.log
log4j.appender.A3.MaxFileSize=10000KB
log4j.appender.A3.MaxBackupIndex=5
log4j.appender.A3.Threshold=INFO
log4j.appender.A3.layout=org.apache.log4j.PatternLayout
log4j.appender.A3.layout.ConversionPattern=[%d] %c %p %m%n


Hope that helps. I've covered a lot of territory quickly, so shoot back if that's not clear.
[ March 17, 2005: Message edited by: Carol Enderlin ]
 
Greg Belyea
Greenhorn
Posts: 27
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Carol

Yeah my assault was probably a bit unfounded because i just came off a wasted day of development, due to some problems before i hit the log4j!!! So i was a bit heated..

I appreciate you taking the time to shoot back a thought out answer, i am in a meeting now but i am going to take a closer look when i get out and i will try to implement.

Thanks alot

Greg B
 
Carol Enderlin
drifter
Ranch Hand
Posts: 1364
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We've all had days like that. Hopefully today will be a better day.

 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!