Log4j Interview Questions with Answers
Question: 1. What is Log4j?
Answer: Log4j is a fast, bendy and reliable logging framework written in Java developed in early 1996. It is shipped under the Apache software program license and can be used for small to massive scale initiatives. It has been ported to the languages like C, C++, C#, Python, etc.
Question: 2. What are the additives of log4j?
Answer:
Logger: It is used to log the messages.
Appender: It is used to post the logging records to the vacation spot like file, database, console and so on.
Layout: It is used to format logging data in different styles.
Question: 3. What are the unique logging ranges?
Answer: There are several logging ranges 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.
Question: 4. What is purpose of DEBUG log level?
Answer: DEBUG: Designates great-grained informational occasions that are maximum beneficial to debug an utility.
HubSpot Video
Question: 5. Why to use Log4j?
Answer:
Being open-supply its absolutely unfastened to use.
You can without difficulty keep log information into either files or even databases.
Can be used for tasks of any sizes small or large.
Question: 6. What are the functions of log4j?
Answer: Following are capabilities of log4j:
It is thread-secure.
It is optimized for speed.
It is primarily based on a named logger hierarchy.
It helps more than one output appenders in keeping with logger.
It supports internationalization.
It isn’t always limited to a predefined set of centers.
Logging conduct can be set at runtime using a configuration file.
It is designed to address Java Exceptions from the start.
It makes use of multiple degrees, namely ALL, TRACE, DEBUG, INFO, WARN, ERROR and FATAL.
The format of the log output may be without problems modified by means of extending the Layout magnificence.
The goal of the log output as well as the writing approach can be altered by implementations of the Appender interface.
It is fail-prevent. However, although it actually strives to ensure shipping, log4j does now not guarantee that each log assertion will be brought to its vacation spot.
Question: 7. What are Pros and Cons of Logging?
Answer: Following are the Pros and Cons of Logging −
Logging is an essential factor of the software program improvement. A nicely-written logging code gives short debugging, clean protection, and structured garage of an utility’s runtime information.
Logging does have its drawbacks also. It can gradual down an utility. If too verbose, it may reason scrolling blindness. To alleviate these issues, log4j is designed to be reliable, fast and extensible.
Since logging is rarely the principle cognizance of an software, the log4j API strives to be simple to apprehend and to apply.
Question: 8. What are different styles of logs?
Answer: Usually in any software there forms of logs:
Application server logs: These are the logs configured at the utility server degree. As an instance in tomcat,
we have log documents referred to as localhost.Log. Tomcat.Log,catalina.Log, stdout.Log, sterr.Log. These types of logs are displaying with default settings described in logging.Properites placed on your tomcat installation folder/conf folder.
If you want custom settings, we ought to alternate the specific parameters in logging.Homes in conf folder of tomcat listing.
Application logs: We can define logging at each applicaiton stage, For this we need to create log4j.Xml or logging.Properties in WEB-INF/lessons folder.
Question: 9. How do Levels Works?
Answer: A log request of level p in a logger with stage q is enabled if p >= q. This rule is on the coronary heart of log4j. It assumes that stages are ordered. For the same old levels, we have ALL < DEBUG < INFO < WARN < ERROR < FATAL < OFF.
Question: 10. What is motive of ERROR log stage?
Answer: ERROR: Designates blunders activities that would nevertheless allow the application to maintain going for walks.
Question: 11. What is reason of FATAL log stage?
Answer: FATAL: Designates very excessive error activities a good way to possibly lead the software to abort.
Question: 12. What is purpose of INFO log degree?
Answer: INFO: Designates informational messages that spotlight the development of the software at coarse-grained degree.
Question: 13. What is cause of OFF log level?
Answer: OFF: The highest viable rank and is meant to turn off logging.
Question: 14. What is motive of TRACE log stage?
Answer: TRACE: Designates finer-grained informational events than the DEBUG.
Question: 15. What is cause of WARN log degree?
Answer: WARN: Designates doubtlessly harmful conditions.
Question: 16. Mention what’s the best way emigrate from java.Util logging to log4j?
Answer: The excellent manner emigrate from java.Util logged to log4j is to use worldwide file search/replace method. It will update with “org.Apache.Log4j.Logger”
Question: 17. Mention what are the two static techniques for acquiring a logger object?
Answer: The two static methods for obtaining a logger object are:
Public static Logger getRootLogger()
Public static Logger getLogger(String call)
Question: 18. What is the cause of Appender item?
Answer: Appender Object: This is a decrease-stage layer of log4j architecture which presents Appender items. The Appender item is liable for publishing logging records to various favored destinations which include a database, report, console, UNIX Syslog, and so forth.
Question: 19. What is the reason of ObjectRenderer item?
Answer: ObjectRenderer: The ObjectRenderer object is specialised in providing a String illustration of different items passed to the logging framework. This object is used by Layout items to prepare the very last logging facts.
Question: 20. How will you create a logger in any magnificence?
Answer: Any other named Logger item instance is obtained via the second one technique by way of passing the call of the logger. The call of the logger may be any string you can bypass, generally a class or a package call as we’ve got used inside the final bankruptcy and it is stated underneath −
static Logger log = Logger.GetLogger(log4jExample.Magnificence.GetName());
Question: 21. How will you outline a root logger turning DEBUG mode off?
Answer: Following syntax defines the foundation logger with WARN mode turning DEBUG mode off.
# Define the basis logger with appender file log = /usr/domestic/log4j log4j.RootLogger = WARN, FILE
Question: 22. What is the cause of c character used within the conversionPattern of PatternLayout object?
Answer: c: Used to output the class of the logging event. For example, for the category call “a.B.C” the pattern %c2 will output “b.C”.
Question: 23. Explain what are the layout characters used in log4j?
Answer: The format characters utilized in log4j are:
L: it is used to output the line wide variety from where the logging request became processed or issued
M: It is used to output the software supplied message associated with the logging occasion
P: It is used to output the priority of the logging occasion
C: It is used to output the class name of the caller issuing the logging request
Question: 24. How log4j file is described?
Answer: Log4j report is defined by means of the call log4j.Houses, it keeps houses in key-fee pairs. By default, the log supervisor looks for a file name log4j.Properties within the CLASSPATH.
Question: 25. Mention what’s the difference among Threshold and LevelRangeFilter in log4j?
Answer: Both Threshold and LevelRangeFilter does the equal aspect. However threshold have to be quicker. Filters allow you to enforce your very own good judgment, and you may additionally link them together if required. If you need a simple threshold functionality, then “threshold” function might be sufficient.