Log4j Interview Questions
1.What is
log4j?
Log4j is java based logging utility.it is used to log the
debug, info, warn, error, and fatal message.
2.Who does
logging?
Logging is done by developers.
3.What are logging message levels?
Debug, info, warn, error, fatal.
4. Which level has the lowest priority?
debug
5. Which level has the highest priority?
fatal
fatal
6. What are the components of log4j?
Logger: It is used to log the messages.
Appender: It is used to publish the logging information to the destination like file, database, console, etc.
Layout: It is used to format logging information in different styles.
7. What is the purpose of DEBUG log level?
Ans: DEBUG: Designates fine-grained informational events that are most useful to debug an application.
8. Why use Log4j?
Being open-source it's completely free to use.
You can easily save log information into either files or even databases.
Can be used for projects of any size small or large.
9. What are the features of log4j?
Following are features of log4j:
It is thread-safe.
It is optimized for speed.
It is based on a named logger hierarchy.
It supports multiple output appenders per logger.
It supports internationalization.
It is not restricted to a predefined set of facilities.
Logging behavior can be set at runtime using a configuration file.
It is designed to handle Java Exceptions from the start.
It uses multiple levels, namely ALL, TRACE, DEBUG, INFO, WARN, ERROR, and FATAL.
The format of the log output can be easily changed by extending the Layout class.
The target of the log output, as well as the writing strategy, can be altered by implementations of the Appender interface.
It is a fail-stop. However, although it certainly strives to ensure delivery, log4j does not guarantee that each log statement will be delivered to its destination.
10. What are the Pros and Cons of Logging?
Following are the Pros and Cons of Logging −
Logging is an important component of software development. A well-written logging code offers quick debugging, easy maintenance, and structured storage of an application's runtime information.
Logging does has its drawbacks also. It can slow down an application. If too verbose, it can cause scrolling blindness. To alleviate these concerns, log4j is designed to be reliable, fast and extensible.
Since logging is rarely the main focus of an application, the log4j API strives to be simple to understand and to use.
11. What are different types of logs?
Usually in any application there two types of logs:
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 the tomcat directory.
Application logs: We can define logging at each application level, For this, we have to create log4j.xml or logging.properties in WEB-INF/classes folder.
12. How do log levels Work?
Ans: A log request of level p in a logger with level q is enabled if p >= q. This rule is at the heart of log4j. It assumes that levels are ordered. For the standard levels, we have ALL < DEBUG < INFO < WARN < ERROR < FATAL < OFF.
13. What is the purpose of ERROR log level?
ERROR: Designates error events that might still allow the application to continue running.
14. What is the purpose of FATAL log level?
FATAL: Designates very severe error events that will presumably lead the application to abort.
15. What is the purpose of INFO log level?
INFO: Designates informational messages that highlight the progress of the application at a coarse-grained level.
16. What is the purpose of OFF log level?
Ans: OFF: The highest possible rank and is intended to turn off logging.
17. What is the purpose of WARN log level?
Ans: WARN: Designates potentially harmful situations.
18. Mention the best way to migrate from java.util logging to log4j?
Ans: The best way to migrate from java.util logged to log4j is to use the global file search/replace method. It will replace with “org.apache.log4j.Logger”
19. Mention what are the two static methods for obtaining a logger object?
The two static methods for obtaining a logger object are:
Public static Logger getRootLogger()
Public static Logger getLogger(String name)
20. What is the purpose of the Appender object?
Ans: Appender Object: This is a lower-level layer of log4j architecture that provides Appender objects. The Appender object is responsible for publishing logging information to various preferred destinations such as a database, file, console, UNIX Syslog, etc.
21. What is the purpose of the ObjectRenderer object?
Ans: ObjectRenderer: The ObjectRenderer object is specialized in providing a String representation of different objects passed to the logging framework. This object is used by Layout objects to prepare the final logging information.
22. How will you create a logger in any class?
Ans: Any other named Logger object instance is obtained through the second method by passing the name of the logger. The name of the logger can be any string you can pass, usually a class or a package name as we have used in the last chapter and it is mentioned below −
static Logger log = Logger.getLogger(log4jExample.class.getName());
No comments:
Post a Comment