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 (
* 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
– Added under the same level as src directory on my eclipse project
– My contents are
log4j.rootLogger=DEBUG, CA
log4j.appender.CA.layout.ConversionPattern=%-4r [%t] %-5p %c %x – %m%n


#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;31;35;36;32;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.

Leave a Reply

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

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

Google photo

You are commenting using your Google 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 )

Connecting to %s