First Test Case using log4j – User friendly Tech help

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.
n
nAlso we would be utilizing the main components of log4j i.e. Logger,Appender and Layout.Lets go step by step.

n

n

n

n

n

n

n

n

Log4j components

n

Implementation:-
nStep1:-Creating a new logger:
nLogger logger = Logger.getLogger(“MyLogger”);
nMyLogger:-logger name

n

n

n

n

n

n

n

n

getlogger method

n

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

n

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

n

nStep2:-
nCreating the layout object:-
nLayout sLayout = new SimpleLayout();
nStep3:-
na.Creating the Appender object:-
nAppender app = new ConsoleAppender(sLayout);
n
b.Adding Appender to Logger object
n log.addAppender(app);
nStep4:-
nCalling the available logger levels:-
n
Priority level = debug < info < warn < error < fatal

n

n

n

n

n

n

n

n

Logger levels

n

n

n

n

n

n

n

n

Adding level to Logger object

n

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.
nExample:- 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.
n
nCode:–
n

n

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
log.setLevel(Level.WARN);
//Creating the layout object
Layout sLayout = new SimpleLayout();
//Creating the Appender object
Appender app = new ConsoleAppender(sLayout);
//Adding appender to logger
log.addAppender(app);
log.debug("First debug Message");
log.info("First info Message");
log.warn("First Warning Message");
log.error("First Error Message");
log.fatal("First Fatal Message"); }}

n

n
nOutput:-
nNote:- 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.
n

n

n

n

n

n

n

n

n

Console output log4j

n


Why to avoid above approach:-

n

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.

Was this article helpful?
YesNo

Similar Posts