10 Interview Questions answers for Log4j in java

This post talks about Frequently asked questions in Java interviews with answers. It includes detailed answers to real-time questions for Developers and backend developers.

What is Log4j and why it is used?

Log4j is a logging framework written in java language, It is provided by the apache foundation. In the applications, if you want to log the same information, like any event, triggered, or any Database update happens, We need to log the specific information or error for the usefulness of the application. To debug any issues in applications, we have to log the error/exceptions in the logs. For this, we will use the log4j mechanism. Log4js log the information and show this information in different targets. The different targets are called appenders (console).

What is log4j used for?

Log4j logs the information to files or consoles or emails. Application status and errors are logged in files and console to know the application transactions.

This helps developers to debug issues during production issues. Log4j is thread-safe opensource that can be used for all types of projects

How do you define logging for your application

if you want to add logging for any application, you have to download the log4j framework from the apache site. Once log4j jars are downloaded, make sure that these jars are in the classpath of your application. let say you have a web application that needs to be added to log4j. In this case, log4j jars are copied to the WEB-INFO/lib folder of your web application to create a new file either logging.properties or log4j.xml which will be copied to WEB-INF/classes folder logging.properties contain all the configurations related to the logging mechanism and logger level and package that you want to define logger level.

What are the different logging levels?

There are several logging levels that you can configure in your application Those are FATAL, ERROR, WARN, TRACE, DEBUG, INFO, OR ALL in apache logging. The default logging level is INFO.

what are different types of logs?

usually in any application, there are 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. properties located in your tomcat installation folder/conf folder. if you want custom settings, we have to change the different parameters in logging.properties in the 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

What are different appenders that can configure in log4j?

There are different appenders that we can configure in log4j are CONSOLE, FILES, DATABASE, JMS, EVENT LOGGING

CONSOLE in log4j: if we use this as appenders in your application, log4j logs the information in the console or command prompt window that is started with a startup script. 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

What is the difference between INFO and DEBUG levels?

INFO and DEBUG are logger levels to categorize the type of messages. INFO message used to information messages, like successful transaction, Result of CRUD operation. DEBUG level used to log the debugger messages for finding the root causes for an issues

How to send stack trace error to log4j?

In Java, Stacktrace gives clues about the line of code during an exception is thrown in program execution.

try and catch blocks are used to handle exceptions in Java.

Here is the code to log stacktrace in log4j 1 version

try {
// Code
 } catch (Exception e) {
    logger.error("error occurred ", e);

logger.error or logger.debug levels are used to log the stack trace.

 try {

    } catch (Exception e) {

logger.catching() is used to catch the exception that extends the Throwable class.

How to disable log4j output in java?

Sometimes, We don’t want to produce logging for an application already configured with log4j.

This can be done in two ways One way is using log4j.properties to change rootLogger value to OFF log4j.properties


Another way, using log4j.xml

In this log4j.xml, Change Root element with level=off

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
        <Root level="off">
            <AppenderRef ref="Console"/>

Third way is using programmatically in application startup