Friday, September 3, 2010

log4j

When I first tried to implement this, my mind was about to explode.  The tutorials for these seem rather complex for first time users.  Anywho, the main reason I wanted to implement this API was to have a log of what the user did to the database in the application.  It's kind of embarrassing to say how long it took me to set that up.  I would say it took at least 2-3 hours.  First the installation was confusing, according to a website, you needed to download the whole folder into the src directory and link that folder to the application.  In the end, all I had to do is add the .jar file to the build path! Then the properties file..... ooooh the properties file..  by taking one look at it, I feel like most people would think its jibberish.  Just like any programming language, I pushed through and figured out how to make a log file for the program.  It came out to be something in the lines of :


### direct log messages to stdout ###
log4j.appender.stdout=org.apache.log4j.FileAppender
log4j.appender.stdout.File=log.log
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1} : %L - %m%n
log4j.rootLogger=debug, stdout

I am very happy with the results.  The part that I love about the log4j aside from the setting up and writing the properties is the ease of usage in the program.  It takes one line to set up the logger which is:

private static org.apache.log4j.Logger log = Logger.getLogger("classname");

and bam! now log.info("blah");
The information is logged into the log!
I should probably look more into how the layout can be changed....
But for now, I am done.

Peace out!
Taka

No comments:

Post a Comment