Log4j Interview Questions and Answers
Ques 6. What is the importance of logging applications?
Ans. Logging helps in debugging as well. Although debuggers are available but frankly it takes time to debug an application using a debugger. An application can be debugged more easily with few well-placed logging messages. So we can safely say that logging is a very good debugging tool. If logging is done sensibly and wisely, it can provide detailed context for application failures.
In distributed applications (e.g. web/remote applications), the logging is very important. The administrator can read logs to learn about the problems that occurred during some interval.
Java's built-in APIs provide logging options but they are not that flexible. Another option is to use Apache’s open source logging framework called log4j.
Drawbacks of logging applications:
There are some drawbacks of use logging in your application. For example: logging will
- pollute the code
- increase the size of the code
- reduce the speed
These are important points because, we ultimately want efficient applications. Log4J has the solution to this. You may turn on or turn off the logging at runtime by changing the configuration file. This means no change in the Java source code (binary).
Ques 7. Describe about logger levels in log4j.Ans. Before using log4j framework, one should be aware of different categories of log messages. Following are 5 categories:
The DEBUG Level is used to indicate events that are useful to debug an application. Handling method for DEBUG level is: debug().
INFO level is used to highlight the progress of the application. Handling method for INFO level is: info().
The WARN level is used to indicate potentially harmful situations. Handling method for WARN level is: warn().
The ERROR level shows errors messages that might not be serious enough and allow the application to continue. Handling method for ERROR level is: error().
The Fatal level is used to indicate severe events that will may cause abortion of the application. Handling method for FATAL level is: fatal().
If you declare log level as debug in the configuration file, then all the other log messages will also be recorded.
If you declare log level as info in the configuration file, then info, warn, error and fatal log messages will be recorded.
If you declare log level as warn in the configuration file, then warn, error and fatal log messages will be recorded.
If you declare log level as error in the configuration file, then error and fatal log messages will be recorded.
If you declare log level as fatal in the configuration file, then only fatal log messages will be recorded.
Ques 8. What are the three main components in log4j?Ans. There are 3 main components that are used to log messages based upon type and level. These components also control the formatting and report place at runtime. These components are:
Most helpful rated by users: