Include Log4j to my java project in eclipse


Well, like any programmer, I started with System.out.println’s for my logging and debugging needs. But only after I wrote myself a set of test cases. As you assumed right I ran out of thought process to organize my log’s very soon. As I was debugging stuff, I realized that my logs not only need to be categorized they need to be “intended”. When I “intended” I meant that sometime in future I want to be able to say something like “show me logs of all threading happening on my server”. So I know who is using my threads, when and is some client eating up my threadpool ? I also want to be able to drive down to the client level to figure out who exactly is eating it up.

The that is “important” for me, not urgent as yet. As any good time management advice will tell you, to focus on your urgent and important. So currently I need to be able to just organize my logs into the regular 4 levels, fatal, error, info and debug. So here goes. I choose Log4j for many reasons
– Tried and tested
– A lot of help on the net for log4j
– Best Practices are already identified, documented and followed (http://juliusdavies.ca/logging.html)
/**
* Logger Design
* Should be greppable
* Separate files for separate areas of interest
* Device/Connection/StreamManager threads
* Stream Management
* Request Issues
* Database Issues
* Memcached Issues
* TimeStamps with timezones from server and client and down to nanoseconds (yyyy-MM-dd/HH:mm:ss.SSS/zzz) Java does not support nanoseconds
* Identity (who tried to do what)
* Single Line (no multiple line’s. Except, exceptions
* When did user last login ?
* Error reported to user “must” be logged (exact Error)
* Every Class to have
* public static final Logger log = Logger.getLogger(MyClass.class) ;
*/

I did encounter some trouble starting up. So this blog will help those who want to startup too. The the above advice, I also did the following
– In my main method I did
PropertyConfigurator.configure(“log4j.properties”);
– Added log.properties under the same level as src directory on my eclipse project
Project
src
….
log4j.properties
– My log4j.properties contents are
log4j.rootLogger=DEBUG, CA
log4j.appender.CA=org.apache.log4j.ConsoleAppender
log4j.appender.CA.layout=org.apache.log4j.PatternLayout
log4j.appender.CA.layout.ConversionPattern=%-4r [%t] %-5p %c %x – %m%n

#log4j.appender.A1.all=\u001B[1;37m
#log4j.appender.A1.fatal=\u001B[1;31m
#log4j.appender.A1.error=\u001B[0;31m
#log4j.appender.A1.warn=\u001B[1;33m
#log4j.appender.A1.info=\u001B[0;37m
#log4j.appender.A1.debug=\u001B[0;36m
#log4j.appender.A1.stacktrace=\u001B[0;31m
#log4j.appender.A1.defaultcolor=\u001B[1;37m

#AnsiColorLogger.ERROR_COLOR=2;31
#AnsiColorLogger.WARNING_COLOR=2;35
#AnsiColorLogger.INFO_COLOR=2;36
#AnsiColorLogger.VERBOSE_COLOR=2;32
#AnsiColorLogger.DEBUG_COLOR=2;34
#
#AnsiColorLogger.*=Attribute;Foreground;Background
#
#Attribute is one of the following:
#0 -> Reset All Attributes (return to normal mode)
#1 -> Bright (Usually turns on BOLD)
#2 -> Dim
#3 -> Underline
#5 -> link
#7 -> Reverse
#8 -> Hidden
#
#Foreground is one of the following:
#30 -> Black
#31 -> Red
#32 -> Green
#33 -> Yellow
#34 -> Blue
#35 -> Magenta
#36 -> Cyan
#37 -> White
#
#Background is one of the following:
#40 -> Black
#41 -> Red
#42 -> Green
#43 -> Yellow
#44 -> Blue
#45 -> Magenta
#46 -> Cyan
#47 -> White
#org.apache.tools.ant.listener.AnsiColorLogger=.ERROR_COLOR=2;31
#org.apache.tools.ant.listener.AnsiColorLogger=.WARNING_COLOR=2;35
#org.apache.tools.ant.listener.AnsiColorLogger=.INFO_COLOR=2;36
#org.apache.tools.ant.listener.AnsiColorLogger=.VERBOSE_COLOR=2;32
#org.apache.tools.ant.listener.AnsiColorLogger=.DEBUG_COLOR=2;34

I am still trying to get colors in my log, but that does not work. When I get it up and running, I’ll edit this post with more. If you have a solution on this, please comment.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s