First Test Case using log4j

After installation and basic overview of log4j, we are implementing log4j in our first test case.Here we would be configuring log4j pro-grammatically but recommended approach is to use it manually by using configuration file.

Also we would be utilizing the main components of log4j i.e. Logger,Appender and Layout.Lets go step by step.

Main components of log4j
Log4j components

Step1:-Creating a new logger:
Logger logger = Logger.getLogger(“MyLogger”);
MyLogger:-logger name

Log4j logger object
getlogger method

More usually, one instantiates a static logger globally, based on the name of the class:-
static Logger logger = Logger.getLogger(test.class);

 /* Getting class name to be printed on */
static Logger log = Logger.getLogger(Log4j_FirstTestCase.class);

Creating the layout object:-
Layout sLayout = new SimpleLayout();
a.Creating the Appender object:-
Appender app = new ConsoleAppender(sLayout);

b.Adding Appender to Logger object
Calling the available logger levels:-

Priority level = debug < info < warn < error < fatal

Logger levels in log4j
Logger levels
error level in log4j
Adding level to Logger object

Note:- This priority level is important, as one’s it is applied, only the Log level of that logger and higher than this will be logged. We can set log level of a logger using the Logger.setLevel method.
Example:- If we set the, log.setLevel(Level.WARN), then even if we have code like :- log.Debug(“Debug message”) in our flow, it would not be shown in the output, because as per the priority Debug < Warn.


package Log4j_Learning;
import org.apache.log4j.Appender;
import org.apache.log4j.ConsoleAppender;
import org.apache.log4j.Layout;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.SimpleLayout;

public class Log4j_FirstTestCase {
//Creating the logger object
static Logger log = Logger.getLogger(Log4j_FirstTestCase.class);
public static void main(String[] args) {
//Setting the log level
//Creating the layout object
Layout sLayout = new SimpleLayout();
//Creating the Appender object
Appender app = new ConsoleAppender(sLayout);
//Adding appender to logger
log.debug("First debug Message");"First info Message");
log.warn("First Warning Message");
log.error("First Error Message");
log.fatal("First Fatal Message");

Note:- In our case as we used log level =WARN ,thus debug and info message won’t be shown in the output.Also the below output is just for illustration, if You run the above code we will have warn,error and fatal messages.

log4j output after run
Console output log4j

Why to avoid above approach:-

Suppose we have used logger in our code, and we wont to change the Appender class or layout design or log level, we need to dig that into the code and have to make the changes.Thus to avoid these code level changes we would recommend going with “configuration file” by which we can control the logger features.In our next tutorial we are implementing the “Configuration file” approach.

Leave a Reply

Your email address will not be published. Required fields are marked *