Log4j Interview Questions & Answers

  1. Question 1. What Is Log4j?

    Answer :

    log4j is a reliable, fast and flexible logging framework (APIs) written in Java, which is distributed under the Apache Software License.

    log4j has been ported to the C, C++, C#, Perl, Python, Ruby, and Eiffel languages.

    log4j is highly configurable through external configuration files at runtime. It views the logging process in terms of levels of priorities and offers mechanisms to direct logging information to a great variety of destinations, such as a database, file, console, UNIX Syslog, etc.

  2. Question 2. What Are The Components Of Log4j?

    Answer :

    log4j has three main components −

    • loggers: Responsible for capturing logging information.
    • appenders: Responsible for publishing logging information to various preferred destinations.
    • layouts: Responsible for formatting logging information in different styles.
  3. HTML Interview Questions

  4. Question 3. What Are The Features Of Log4j?

    Answer :

    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 fail-stop. However, although it certainly strives to ensure delivery, log4j does not guarantee that each log statement will be delivered to its destination.
  5. Question 4. What Are Pros And Cons Of Logging?

    Answer :

    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.
  6. HTML Tutorial

  7. Question 5. What Is The Purpose Of Logger Object?

    Answer :

    Logger Object − The top-level layer of log4j architecture is the Logger which provides the Logger object. The Logger object is responsible for capturing logging information and they are stored in a namespace hierarchy.

  8. HTML 5 Interview Questions

  9. Question 6. What Is The Purpose Of Layout Object?

    Answer :

    Layout Object − The layout layer of log4j architecture provides objects which are used to format logging information in different styles. It provides support to appender objects before publishing logging information.

    Layout objects play an important role in publishing logging information in a way that is human-readable and reusable.

  10. Question 7. What Is The Purpose Of Appender Object?

    Answer :

    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.

  11. HTML 5 Tutorial
    AJAX Interview Questions

  12. Question 8. What Is The Purpose Of Level Object?

    Answer :

    Level Object – The Level object defines the granularity and priority of any logging information. There are seven levels of logging defined within the API: OFF, DEBUG, INFO, ERROR, WARN, FATAL, and ALL.

  13. Question 9. What Is The Purpose Of Filter Object?

    Answer :

    Filter Object − The Filter object is used to analyze logging information and make further decisions on whether that information should be logged or not. An Appender objects can have several Filter objects associated with them. If logging information is passed to a particular Appender object, all the Filter objects associated with that Appender need to approve the logging information before it can be published to the attached destination.

  14. JSP Interview Questions

  15. Question 10. What Is The Purpose Of Objectrenderer Object?

    Answer :

    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.

  16. AJAX Tutorial

  17. Question 11. What Is The Purpose Of Logmanager Object?

    Answer :

    LogManager − The LogManager object manages the logging framework. It is responsible for reading the initial configuration parameters from a system-wide configuration file or a configuration class.

  18. Quark Xpress Interview Questions

  19. Question 12. What Is The Use Of Log4j.properties?

    Answer :

    The log4j.properties file is a log4j configuration file which keeps properties in key-value pairs. By default, the LogManager looks for a file named log4j.properties in the CLASSPATH.

  20. HTML Interview Questions

  21. Question 13. What Is The Purpose Of Layout Object In Appender?

    Answer :

    layout − Appender uses the Layout objects and the conversion pattern associated with them to format the logging information.

  22. JSP Tutorial

  23. Question 14. What Is The Purpose Of Target In Appender?

    Answer :

    target − The target may be a console, a file, or another item depending on the appender.

  24. Question 15. What Is The Purpose Of Level In Appender?

    Answer :

    level − The level is required to control the filtration of the log messages.

  25. JavaMail API Interview Questions

  26. Question 16. What Is The Purpose Of Threshold In Appender?

    Answer :

    threshold − Appender can have a threshold level associated with it independent of the logger level. The Appender ignores any logging messages that have a level lower than the threshold level.

  27. JavaMail API Tutorial

  28. Question 17. What Is The Purpose Of Filter In Appender?

    Answer :

    filter − The Filter objects can analyze logging information beyond level matching and decide whether logging requests should be handled by a particular Appender or ignored.

  29. JUnit Interview Questions

  30. Question 18. How Will You Define A Root Logger With Appender File Using Log4j.properties?

    Answer :

    Following syntax defines the root logger with appender file:

    # Define the root logger with appender file
    log = /usr/home/log4j
    log4j.rootLogger = DEBUG, FILE

  31. HTML 5 Interview Questions

  32. Question 19. How Will You Define A File Appender Using Log4j.properties?

    Answer :

    Following syntax defines a file appender −

    # Define the file appender
    log4j.appender.FILE=org.apache.log4j.FileAppender
    log4j.appender.FILE.File=${log}/log.out

  33. JUnit Tutorial

  34. Question 20. How Will You Define The Layout Of File Appender Using Log4j.properties?

    Answer :

    Following syntax defines the layout of file appender −

    # Define the layout for file appender
    log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
    log4j.appender.FILE.layout.conversionPattern=%m%n

  35. Eclipse (software) Interview Questions

  36. Question 21. How Will You Create A Logger In Any Class?

    Answer :

    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());

  37. Question 22. How Will You Print A Log Message In Debug Mode?

    Answer :

    public void debug(Object message) of Logger class prints messages with the level Level.DEBUG.

  38. Apache Ant Tutorial

  39. Question 23. How Will You Print A Log Message In Error Mode?

    Answer :

    public void error(Object message) of Logger class prints messages with the level Level.ERROR.

  40. Apache Ant Interview Questions

  41. Question 24. How Will You Print A Log Message In Fatal Mode?

    Answer :

    public void fatal(Object message) of Logger class prints messages with the level Level.FATAL.

  42. AJAX Interview Questions

  43. Question 25. How Will You Print A Log Message In Info Mode?

    Answer :

    public void info(Object message) of Logger class prints messages with the level Level.INFO.

  44. Spring MVC Framework Tutorial

  45. Question 26. How Will You Print A Log Message In Warn Mode?

    Answer :

    public void warn(Object message) of Logger class prints messages with the level Level.WARN.

  46. Spring MVC Framework Interview Questions

  47. Question 27. How Will You Print A Log Message In Trace Mode?

    Answer :

    public void trace(Object message) of Logger class prints messages with the level Level.TRACE.

  48. JSP Interview Questions

  49. Question 28. What Is Purpose Of All Log Level?

    Answer :

    ALL − All levels including custom levels.

  50. JAVA Persistence API (JPA) Tutorial

  51. Question 29. What Is Purpose Of Debug Log Level?

    Answer :

    DEBUG − Designates fine-grained informational events that are most useful to debug an application.

  52. Structural Analysis Interview Questions

  53. Question 30. What Is Purpose Of Error Log Level?

    Answer :

    ERROR − Designates error events that might still allow the application to continue running.

  54. Question 31. What Is Purpose Of Fatal Log Level?

    Answer :

    FATAL − Designates very severe error events that will presumably lead the application to abort.

  55. Question 32. What Is Purpose Of Info Log Level?

    Answer :

    INFO − Designates informational messages that highlight the progress of the application at coarse-grained level.

  56. Question 33. What Is Purpose Of Off Log Level?

    Answer :

    OFF − The highest possible rank and is intended to turn off logging.

  57. Quark Xpress Interview Questions

  58. Question 34. What Is Purpose Of Trace Log Level?

    Answer :

    TRACE − Designates finer-grained informational events than the DEBUG.

  59. Question 35. What Is Purpose Of Warn Log Level?

    Answer :

    WARN − Designates potentially harmful situations.

  60. Question 36. How Do Levels Works?

    Answer :

    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.

  61. JavaMail API Interview Questions

  62. Question 37. How Will You Define A Root Logger Turning Debug Mode Off?

    Answer :

    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

  63. Question 38. What Is The Purpose Of Patternlayout Object?

    Answer :

    If you want to generate your logging information in a particular format based on a pattern, then you can use org.apache.log4j.PatternLayout to format your logging information.

    The PatternLayout class extends the abstract org.apache.log4j.Layout class and overrides the format() method to structure the logging information according to a supplied pattern.

  64. Question 39. What Is The Purpose Of C Character Used In The Conversionpattern Of Patternlayout Object?

    Answer :

    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”.

  65. Question 40. What Is The Purpose Of C Character Used In The Conversionpattern Of Patternlayout Object ?

    Answer :

    C − Used to output the fully qualified class name of the caller issuing the logging request. For example, for the class name. “org.apache.xyz.SomeClass”, the pattern %C{1} will output “SomeClass”.

  66. JUnit Interview Questions

  67. Question 41. What Is The Purpose Of D Character Used In The Conversionpattern Of Patternlayout Object?

    Answer :

    d − Used to output the date of the logging event. For example, %d{HH:mm:ss,SSS} or %d{dd MMM yyyy HH:mm:ss,SSS}.

  68. Question 42. What Is The Purpose Of F Character Used In The Conversionpattern Of Patternlayout Object?

    Answer :

    F − Used to output the file name where the logging request was issued.

  69. Eclipse (software) Interview Questions

  70. Question 43. What Is The Purpose Of L Character Used In The Conversionpattern Of Patternlayout Object?

    Answer :

    l − Used to output location information of the caller which generated the logging event.

  71. Question 44. What Is The Purpose Of L Character Used In The Conversionpattern Of Patternlayout Object ?

    Answer :

    L − Used to output the line number from where the logging request was issued.

  72. Question 45. What Is The Purpose Of M Character Used In The Conversionpattern Of Patternlayout Object?

    Answer :

    m − Used to output the application supplied message associated with the logging event.

  73. Question 46. What Is The Purpose Of M Character Used In The Conversionpattern Of Patternlayout Object ?

    Answer :

    M − Used to output the method name where the logging request was issued.

  74. Question 47. What Is The Purpose Of N Character Used In The Conversionpattern Of Patternlayout Object?

    Answer :

    n − Outputs the platform dependent line separator character or characters.

  75. Question 48. What Is The Purpose Of P Character Used In The Conversionpattern Of Patternlayout Object?

    Answer :

    p − Used to output the priority of the logging event.

  76. Question 49. What Is The Purpose Of R Character Used In The Conversionpattern Of Patternlayout Object?

    Answer :

    r − Used to output the number of milliseconds elapsed from the construction of the layout until the creation of the logging event.

  77. Question 50. What Is The Purpose Of T Character Used In The Conversionpattern Of Patternlayout Object?

    Answer :

    t − Used to output the name of the thread that generated the logging event.

  78. Question 51. What Is The Purpose Of X Character Used In The Conversionpattern Of Patternlayout Object?

    Answer :

    x − Used to output the NDC (nested diagnostic context) associated with the thread that generated the logging event.

  79. Question 52. What Is The Purpose Of X Character Used In The Conversionpattern Of Patternlayout Object ?

    Answer :

    X − The X conversion character is followed by the key for the MDC. For example, X{clientIP} will print the information stored in the MDC against the key clientIP.

  80. Question 53. What Is The Purpose Of % Character Used In The Conversionpattern Of Patternlayout Object?

    Answer :

    % − The literal percent sign. %% will print a % sign.

  81. Question 54. What Are Format Modifiers?

    Answer :

    By default, the relevant information is displayed as output as is. However, with the aid of format modifiers, it is possible to change the minimum field width, the maximum field width, and justification.

  82. Question 55. What Is The Intent Of %20c Format Modifier?

    Answer :

    %20c − Left pad with spaces if the category name is less than 20 characters long.

  83. Question 56. What Is The Intent Of %-20c Format Modifier?

    Answer :

    %-20c − Right pad with spaces if the category name is less than 20 characters long.

  84. Question 57. What Is The Intent Of %.30c Format Modifier?

    Answer :

    %.30c − Truncate from the beginning if the category name is longer than 30 characters.

  85. Question 58. What Is The Intent Of %20.30c Format Modifier?

    Answer :

    %20.30c − Left pad with spaces if the category name is shorter than 20 characters. However, if the category name is longer than 30 characters, then truncate from the beginning.

  86. Question 59. What Is The Intent Of %-20.30c Format Modifier?

    Answer :

    %-20.30c − Right pad with spaces if the category name is shorter than 20 characters. However, if category name is longer than 30 characters, then truncate from the beginning.

  87. Question 60. If You Want To Generate Your Logging Information In An Html-formatted File, How Will You Proceed?

    Answer :

    If you want to generate your logging information in an HTML-formatted file, then you can use org.apache.log4j.HTMLLayout to format your logging information.

    The HTMLLayout class extends the abstract org.apache.log4j.Layout class and overrides the format() method from its base class to provide HTML-style formatting.

  88. Question 61. What Kind Of Information Htmllayout Class Provides?

    Answer :

    It provides the following information to be displayed −

    • The time elapsed from the start of the application before a particular logging event was generated.
    • The name of the thread that invoked the logging request.
    • The level associated with this logging request.
    • The name of the logger and logging message.
    • The optional location information for the program file and the line number from which this logging was invoked.
  89. Question 62. How Will You Set The Content Type Of Html Generated Using Htmllayout?

    Answer :

    HTMLLayout.setContentType(String) − Sets the content type of the HTML content. Default is text/html.

  90. Question 63. How Will You Set The Location Information For The Logging Event Using Htmllayout?

    Answer :

    HTMLLayout.setLocationInfo(String) − Sets the location information for the logging event. Default is false.

  91. Question 64. How Will You Set The Title Of Html Page Generated Using Htmllayout?

    Answer :

    HTMLLayout.setTitle(String) − Sets the title for the HTML file. Default is log4j Log Messages.

  92. Question 65. What Is The Purpose Of Immediateflush Configuration Of Fileappender?

    Answer :

    immediateFlush − This flag is by default set to true, which means the output stream to the file being flushed with each append operation.

  93. Question 66. What Is The Purpose Of Encoding Configuration Of Fileappender?

    Answer :

    encoding − It is possible to use any character-encoding. By default, it is the platform-specific encoding scheme.

  94. Question 67. What Is The Purpose Of Threshold Configuration Of Fileappender?

    Answer :

    threshold − The threshold level for this appender.

  95. Question 68. What Is The Purpose Of Filename Configuration Of Fileappender?

    Answer :

    Filename − The name of the log file.

  96. Question 69. What Is The Purpose Of Fileappend Configuration Of Fileappender?

    Answer :

    fileAppend − This is by default set to true, which means the logging information being appended to the end of the same file.

  97. Question 70. What Is The Purpose Of Bufferedio Configuration Of Fileappender?

    Answer :

    bufferedIO − This flag indicates whether we need buffered writing enabled. By default, it is set to false.

  98. Question 71. What Is The Purpose Of Buffersize Configuration Of Fileappender?

    Answer :

    bufferSize − If buffered I/O is enabled, it indicates the buffer size. By default, it is set to 8kb.

  99. Question 72. How Will You Configure Immediate Flush To True Using Log4j.properties?

    Answer :

    Following code configures immediate flush to true −

    # Set the immediate flush to true (default)
    log4j.appender.FILE.ImmediateFlush=true

  100. Question 73. How Will You Set The Threshold To Debug Mode Using Log4j.properties?

    Answer :

    Following code sets the threshold to debug mode −

    # Set the threshold to debug mode
    log4j.appender.FILE.Threshold=debug

  101. Question 74. How Will You Set The Append To False, Overwrite Using Log4j.properties?

    Answer :

    Following code sets the append to false, overwrite −

    # Set the append to false, overwrite
    log4j.appender.FILE.Append=false

  102. Question 75. If You Want To Write Your Logging Information Into Multiple Files Then How Will You Proceed?

    Answer :

    To write your logging information into multiple files, you would have to use org.apache.log4j.RollingFileAppender class which extends the FileAppender class and inherits all its properties.

  103. Question 76. What Is The Purpose Of Maxfilesize Property Of Rollingfileappender Class?

    Answer :

    This is the critical size of the file above which the file will be rolled.

  104. Question 77. What Is Default Value Of Maxfilesize Property Of Rollingfileappender Class?

    Answer :

    Default value is 10 MB.

  105. Question 78. What Is The Purpose Of Maxbackupindex Property Of Rollingfileappender Class?

    Answer :

    This property denotes the number of backup files to be created.

  106. Question 79. What Is Default Value Of Maxbackupindex Property Of Rollingfileappender Class?

    Answer :

    Default value is 1.

  107. Question 80. How Will You Configure A Rollingfileappender Using Log4j.properties?

    Answer :

    Following code configures a RollingFileAppender −

    # Define the root logger with appender file
    log4j.rootLogger = DEBUG, FILE
    # Define the file appender
    log4j.appender.FILE=org.apache.log4j.RollingFileAppender

  108. Question 81. How Will You Configure Maximum File Size Before Rollover Using Log4j.properties?

    Answer :

    Following code configures maximum file size before rollover −

    # Set the maximum file size before rollover
    log4j.appender.FILE.MaxFileSize=5KB

  109. Question 82. How Will You Configure Maximum Files To Be Used To Log Data Using Log4j.properties?

    Answer :

    Following code configures maximum files to be used −

    # Set the the backup index
    log4j.appender.FILE.MaxBackupIndex=2

  110. Question 83. What Happens If Logs Exceeding The Maximum Size While Using Rollingfileappender?

    Answer :

    A new log file will be created.

  111. Question 84. What Happens If Last Log File Reaches The Maximum Size While Using Rollingfileappender?

    Answer :

    Once the last log file reaches the maximum size, the first log file will be erased and thereafter, all the logging information will be rolled back to the first log file.

  112. Question 85. How Will You Generate Your Log Files On A Daily Basis?

    Answer :

    To write your logging information into files on a daily basis, you would have to use org.apache.log4j.DailyRollingFileAppender class which extends the FileAppender class and inherits all its properties.

  113. Question 86. What Is The Purpose Of Datepattern Property Of Dailyrollingfileappender Class?

    Answer :

    This indicates when to roll over the file and the naming convention to be followed. By default, roll over is performed at midnight each day.

  114. Question 87. How Will You Configure Your Log To Roll Over At The End Of Each Month And At The Beginning Of The Next Month?

    Answer :

    ‘.’ yyyy-MM − Roll over at the end of each month and at the beginning of the next month.

  115. Question 88. How Will You Configure Your Log To Roll Over At Midnight Each Day?

    Answer :

    ‘.’ yyyy-MM-dd − Roll over at midnight each day. This is the default value.

  116. Question 89. How Will You Configure Your Log To Roll Over At Midday And Midnight Of Each Day?

    Answer :

    ‘.’ yyyy-MM-dd-a − Roll over at midday and midnight of each day.

  117. Question 90. How Will You Configure Your Log To Roll Over At The Top Of Every Hour?

    Answer :

    ‘.’ yyyy-MM-dd-HH − Roll over at the top of every hour.

  118. Question 91. How Will You Configure Your Log To Roll Over Every Minute?

    Answer :

    ‘.’ yyyy-MM-dd-HH-mm − Roll over every minute.

  119. Question 92. How Will You Configure Your Log To Roll Over On The First Day Of Each Week Depending Upon The Locale?

    Answer :

    ‘.’ yyyy-ww − Roll over on the first day of each week depending upon the locale.

  120. Question 93. How Will You Configure A Dailyrollingfileappender Using Log4j.properties?

    Answer :

    Following code configures a DailyRollingFileAppender −

    # Define the root logger with appender file
    log4j.rootLogger = DEBUG, FILE 
    # Define the file appender
    log4j.appender.FILE = org.apache.log4j.DailyRollingFileAppender

  121. Question 94. How Will You Set The Datepattern Using Log4j.properties?

    Answer :

    Following code configures a DatePattern −

    # Set the DatePattern
    log4j.appender.FILE.DatePattern = ‘.’ yyyy-MM-dd-a

  122. Question 95. How Will You Put The Logs In Database Using Log4j?

    Answer :

    The log4j API provides the org.apache.log4j.jdbc.JDBCAppender object, which can put logging information in a specified database.

  123. Question 96. What Is The Purpose Of Driver Configuration Of Jdbcappender?

    Answer :

    driver − Sets the driver class to the specified string. If no driver class is specified, it defaults to sun.jdbc.odbc.JdbcOdbcDriver.

  124. Question 97. What Is The Purpose Of Password Configuration Of Jdbcappender?

    Answer :

    password − Sets the database password.

  125. Question 98. What Is The Purpose Of Sql Configuration Of Jdbcappender?

    Answer :

    sql − Specifies the SQL statement to be executed every time a logging event occurs. This could be INSERT, UPDATE, or DELETE.

  126. Question 99. What Is The Purpose Of Url Configuration Of Jdbcappender?

    Answer :

    URL − Sets the JDBC URL.

  127. Question 100. What Is The Purpose Of User Configuration Of Jdbcappender?

    Answer :

    user − Sets the database user name.