Thursday, September 9, 2010

Bored Of Logging : Here's An Easy Way


Most of the developers ignore logging mainly because they have to type the class name and method name whenever they log. This really irritates the developer while they coding.
For Example:
Private String CLASS_NAME =”JunkClass”;
Private String METHOD_NAME=”junkMethod()”;
Logger.info(CLASS_NAME,METHOD_NAME,”Simple Logging”);
Consider the above line, we have to type every time when we do a logging, this may seem simple but while you most concentration goes towards the core logic, this is a real overhead and makes developer to just ignore the logs.
When a thing goes wrongs in production, we may think that we should have done more logging. This is a cycle process which always happen in the developers world ;)
I was trying to look some options in Eclipse, which may eliminate this REAL overhead which we never may notice.
Using Eclipse Code Template, you can create a template as below which automatically populates the Class Name & Method name for you.
Creating code Template:

Windows àPreference à Java àEditor àTemplate



Logger.info(${enclosing_type}.class, "${enclosing_method}", "");

Name: _Logs_info : type this word in your eclipse and eclipse will automatically populates the method and class name automatically.
For Example if you are inside class called ServeMe and inside method getServiceName().You template will generate the code as below

Logger.info(ServeMe.class,”getServiceName()”,””);

What you need to do is just type text which you need to log , that’s it.

If you thought this may help in your daily life, just leave your comments.

4 comments:

Anonymous said...

Excellent tip! Even even in shops that have coding standards where the logging extensions require inclusion of more arguments, like errorID, templates can be created to at least automatically put in the correct 'boilerplate'. Just like the sysout macro in Eclipse.

-- Josef

Florian Laplantif said...

Nice, but how about using a Framework like Spring instead and an AOP approach to logging instead of having redundant boilerplate logging code? That's exactly what frameworks like Spring were developed for...

Mohammed Yousuff said...

You are right Florian, however all the projects does't require spring to be implemented. in reality most of the projects does't uses any frameworks ...only JSP,Servlets....
Going forward Yes AOP is best way to do the logging and we don't require any log statements in our business logics

John McClean said...

Florian - you nearly always still need logging in the code. If you can produce nice generic logging you can certainly abstract a large portion of your logging out into Aspects, but in most projects there'll still be a lot of cases where you want specific / differentiated logging. Trying to accommodate that in generic abstraction is nearly always more trouble than it's worth..

Post a Comment

 

This content comes from a hidden element on this page.

The inline option preserves bound JavaScript events and changes, and it puts the content back where it came from when it is closed.
Click me, it will be preserved!

If you try to open a new ColorBox while it is already open, it will update itself with the new content.

Updating Content Example:
Click here to load new content