Tuesday, May 1, 2012

logging in Java

 
         

logging in Java


basic java logging level e.g. DEBUG, INFO, WARN and ERROR.

DEBUG is the lowest restricted java logging level and we should write everything we need to debug an application, this java logging mode should only be used on Development and Testing environment and must not be used in production environment.

INFO is more restricted than DEBUG java logging level and we should log messages which are informative purpose like Server has been started, Incoming messages, outgoing messages etc in INFO level logging in java.

WARN is more restricted than INFO java logging level and used to log warning sort of messages e.g. Connection lost between client and server. Database connection lost, Socket reaching to its limit. These messages and java logging level are almost important because you can setup alert on these logging messages in java

ERROR is the more restricted java logging level than WARN and used to log Errors and Exception, you can also setup alert on this java logging level and alert monitoring team to react on this messages. ERROR is serious for logging in Java and you should always print it.

FATAL java logging level designates very severe error events that will presumably lead the application to abort. After this mostly your application crashes and stopped.

OFF java logging level has the highest possible rank and is intended to turn off logging in Java.

These java logging levels are based on log4j logging level and little bit different than java.util.logging API

I agree java.util.logging API is also very powerful but I find log4j more intuitive and easy to use than java.util.

Another flexibility of log4j is that you can change logging level of your java application without restarting your java application
Log4j also provides flexibility to set logging level based on per class in its configuration file log4j.xml.

logging in Java affects performance
Java logging severely affects performance of your application. Its quite common sense that more you log, more you perform file IO which slows down your application.

So always log DEBUG messages inside isDebugEnabled () block as shown in below example of debug mode in java.

if(logger.isDebugEnabled()){
logger.debug("java logging level is DEBUG Enabled");


Uses either WARN ERROR or FINER, FINEST java logging level in production environment.

1) Use isDebugEnabled() for putting debug log in Java , it will save lot of string concatenation

2) Carefully choose which kind of message should go to which level for logging in Java

If you log too much information your performance will be affected

Use either log4j or java.util.logging for logging in Java, I would recommend log4j because I have used it a lot and found it very flexible.

By using log4j.xml you can have different logger configuration for different java classes as well. You can have some classes in INFO mode, some in WARN mode or ERROR mode.

Another important point to remember is format of java logging, this you specify in logger. Properties file in case of java.util.logging API for logging to use which java logging Formatter. Don’t forget to include Thread Name and fully qualified java class Name while printing logs because it would be impossible to find sequence of events if your code is executed by multiple threads without having thread name on it. In my opinion this is the most important tips you consider for logging in Java.

By carefully choosing format of  java logging at logger level and format of writing log you can have generate reports from your java log files.

while writing message for logging in Java try to use some kind of prefix to indicate which part of your code is printing log e.g. client side , Database site or session side, later you can use this prefix to do a "grep" or "find" in Unix and have related logs at once place.

For example you can put all Database level log with a prefix "DB_LOG:" and put all session level log with prefix "SESSION_LOG:”

If a given logger is not assigned a level, then it inherits one from its closest ancestor. That’s why we always assign log level to root logger in configuration file log4j.rootLogger=DEBUG.

Both no logging and excessive logging is bad so carefully planned what to log and on which level you log that messages so that you can run fast in production environment and at same time able to identify any issue in QA and TEST environment.

I found that for improving logging its important you look through your log and monitor your log by yourself and tune it wherever necessary.

if you are using SLFJ for logging in java use parametrized version of various log methods they are faster as compared to normal method.


No comments:

Post a Comment