Log4j is a fast, flexible and reliable logging framework written in Java and developed in early 1996. It is distributed under the Apache software license and can be used for small to large-scale projects. It has been ported to the languages like C, C++, C#, Python, etc.
Q2. What are the components of log4j?
1. Logger: It is used to log the messages.
2. Appender: It is used to publish the logging information to the destination like file, database, console etc.
3. Layout: It is used to format logging information in different styles.
Q3. What are the different logging levels?
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.
Q4. What is purpose of DEBUG log level?
DEBUG − Designates fine-grained informational events that are most useful to debug an application.
Q5. Why to use Log4j?
Q6. What are the features of log4j?
Following are features of log4j −
Q7. What are Pros and Cons of Logging?
Following are the Pros and Cons of Logging −
Logging is an important component of the software development. A well-written logging code offers quick debugging, easy maintenance, and structured storage of an application’s runtime information.
Logging does have 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.
Q8. What are different types of logs?
Usually in any application there two types of logs
Q9. How do Levels Works?
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.
Q10. What is purpose of ERROR log level?
ERROR − Designates error events that might still allow the application to continue running.
Q11. What is purpose of FATAL log level?
FATAL − Designates very severe error events that will presumably lead the application to abort.
Q12. What is purpose of INFO log level?
INFO − Designates informational messages that highlight the progress of the application at coarse-grained level.
Q13. What is purpose of OFF log level?
OFF − The highest possible rank and is intended to turn off logging.
Q14. What is purpose of TRACE log level?
TRACE − Designates finer-grained informational events than the DEBUG.
Q15. What is purpose of WARN log level?
WARN − Designates potentially harmful situations.
Q16. Mention what is the best way to migrate from java.util logging to log4j?
The best way to migrate from java.util logged to log4j is to use global file search/replace method. It will replace with “org.apache.log4j.Logger”
Q17. Mention what are the two static methods for obtaining a logger object?
The two static methods for obtaining a logger object are
Q18. What is the purpose of Appender object?
Appender Object − This is a lower-level layer of log4j architecture which 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.
Q19. What is the purpose of ObjectRenderer object?
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.
Q20. How will you create a logger in any class?
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());
Q21. How will you define a root logger turning DEBUG mode off?
Following syntax defines the root logger with WARN mode turning DEBUG mode off.
# Define the root logger with appender file log = /usr/home/log4j log4j.rootLogger = WARN, FILE
Q22. What is the purpose of c character used in the conversionPattern of PatternLayout object?
c − Used to output the category of the logging event. For example, for the category name “a.b.c” the pattern %c{2} will output “b.c”.
Q23. Explain what are the format characters used in log4j?
The format characters used in log4j are
Q24. How log4j file is defined?
Log4j file is defined by the name log4j.properties, it keeps properties in key-value pairs. By default, the log manager looks for a file name log4j.properties in the CLASSPATH.
Q25. Mention what is the difference between Threshold and LevelRangeFilter in log4j?
Both Threshold and Level Range Filter does the same thing. However threshold should be faster. Filters enable you to implement your own logic, and you can also link them together if required. If you need a basic threshold functionality, then “threshold” function will be enough.