Log4j Interview Questions and Answers
Ques 1. What is Log4j?
Ans. Log4j (Log for Java) is logging framework provided by apache foundation for java based applications.
In the applicaitons, if you want to log some information, like any event triggered, or any Database updated is happened, we have the need to log the specific information or error for the useful of the application.
To debug any issues in applications, we have to log the error/exceptions in the logs. For this we will use log4j mechanism .
Log4j logs the information and shows this information in different targets. The different targets are called appenders (console, file etc ).
Ques 2. How do you define logging for your application?Ans. To define logging for your application, you have to download the log4j framework (log4j.jar) from the apache site.
Once log4j jars are downloaded, make sure that these jars are in classpath of your application. let say you have web-application need to be added log4j. In this case, log4j jars are copied to WEB-INFO/lib folder of your webapplication
create new file either logging.properties or log4j.xml which will be copied to WEB-INF/classes folder
logging.properties/log4j.xml contains the all the configuration related to logging mechanism and logger level and package that you want to define logger level.
Ques 3. What are the different logging levels?Ans. There are several logging levels that you can configure in you applicaiton
Those are FATAL,ERROR,WARN,TRACE,DEBUG,INFO OR ALL in apache logging. Default logging level is INFO.
Ques 4. What are different types of logs?Ans. Usually in any application there two types of logs
1.Application server logs :- These are the logs configured at the application server level. for example in tomcat,
we have log files called localhost.log. tomcat.log,catalina.log, stdout.log, sterr.log. all these logs are showing with default settings defined in logging.properites located in your tomcat installation folder/conf folder.
if you want custom settings, we have to change the different parameters in logging.properties in conf folder of tomcat directory.
2.Application logs:- We can define logging at each applicaiton level, For this we have to create log4j.xml or logging.properties in WEB-INF/classes folder.
Ques 5. What are different appenders that can configure in log4j?Ans. There are different appenders that we can configure in log4j are: CONSOLE, FILES, DATABASE, JMS, EVENT LOGGING
Mainly used appenders:
Files in log4j:- Files Appender is used to log the information into our custom name files. when we are configuring this appender, we can specify the file name.
Most helpful rated by users: