What are the main components of Log4j

0 votes
What are the main components of log4j?
May 22, 2019 in Selenium by Kiran
841 views

1 answer to this question.

0 votes

Hi Kiran, Log4j has four main components:

  1. LogManager: This is the static class that helps us get loggers with different names and hierarchy. You can consider LogManager as a factory producing logger objects. A sample code will be:

    • package Log4jSample;
      
      import org.apache.log4j.BasicConfigurator;
      import org.apache.log4j.LogManager;
      import org.apache.log4j.Logger;
      
      public class SampleEntry {
      
       //mainLogger is a logger object that we got from LogManager. All loggers are
              //using this method only. We can consider LogManager as a factory to create
              //Logger objects
       static Logger mainLogger = LogManager.getLogger(SampleEntry.class.getName());
      
       public static void main(String[] args) {
        // TODO Auto-generated method stub
      
        BasicConfigurator.configure();
        mainLogger.info("This is just a logger"); 
      
       }
      }
  2. Loggers: This class which helps you log information at different logging levels. In the above sample code you can see that we have created a logger named mainLogger using the LogManager static class. Now we can use it to write logs. As you can see we have mainLogger.info(“Comments that you want to log”) statement which logs the string.

  3. Appenders: Appenders are objects which help Logger objects write logs to different outputs. Appenders can specify a file, console or a data base as the output location. In this code sample you will see that we have used a console appender to print logs like we would do using System.out or System.err

    • package Log4jSample;
      
      import org.apache.log4j.BasicConfigurator;
      import org.apache.log4j.ConsoleAppender;
      import org.apache.log4j.LogManager;
      import org.apache.log4j.Logger;
      
      public class SampleEntry {
      
       //All the loggers that can be used
       static Logger mainLogger = LogManager.getLogger(SampleEntry.class.getName());
      
       public static void main(String[] args) {
        // TODO Auto-generated method stub
      
        BasicConfigurator.configure();
      
        //Create a console appender and attach it to our mainLogger
        ConsoleAppender appender = new ConsoleAppender();
        mainLogger.addAppender(appender);
        mainLogger.info("This is just a logger"); 
       }
      }
  4. Layouts: Layout class help us define how the log information should appear in the outputs. Here is a sample code which uses PatternLayout Class to change the formatting of logs:

    • package Log4jSample;
      
      import java.util.Enumeration;
      
      import org.apache.log4j.Appender;
      import org.apache.log4j.BasicConfigurator;
      import org.apache.log4j.ConsoleAppender;
      import org.apache.log4j.Layout;
      import org.apache.log4j.LogManager;
      import org.apache.log4j.Logger;
      import org.apache.log4j.PatternLayout;
      
      public class SampleEntry {
      
       //All the loggers that can be used
       static Logger mainLogger = LogManager.getLogger(SampleEntry.class.getName());
      
       public static void main(String[] args) {
        // TODO Auto-generated method stub
      
        BasicConfigurator.configure();
        ConsoleAppender appender = new ConsoleAppender();
        appender.activateOptions();
        PatternLayout layoutHelper = new PatternLayout();
        layoutHelper.setConversionPattern("%-5p [%t]: %m%n");
        layoutHelper.activateOptions();
              //mainLogger.getAppender("ConsoleAppender").setLayout(layoutHelper); 
        appender.setLayout(layoutHelper);
        mainLogger.addAppender(appender);
        //Create a console appender and attach it to our mainLogger
        mainLogger.info("Pattern 1 is displayed like this");
        layoutHelper.setConversionPattern("%C %m%n");
        mainLogger.info("Pattern 2 is displayed like this");
      
       }
      
      }
answered May 22, 2019 by Anvi
• 14,150 points

Related Questions In Selenium

0 votes
1 answer

What are the different components of Selenium?

Selenium is a testing tool and comprises ...READ MORE

answered Jan 8, 2019 in Selenium by Fuji
1,551 views
0 votes
1 answer

What are the components of Web-site testing?

The components of Web site testing are ...READ MORE

answered Feb 14, 2019 in Selenium by Nabarupa Das
698 views
0 votes
1 answer

What are the main disadvantages of using implicit wait in Selenium?

Hey Aaron, the main disadvantage of implicit ...READ MORE

answered Jun 1, 2019 in Selenium by Abha
• 28,140 points
8,490 views
0 votes
1 answer

What are the main types of regex quantifiers used in Selenium Webdriver?

Hi Andrew, some of the regex quantifiers ...READ MORE

answered Jun 26, 2019 in Selenium by Abha
• 28,140 points
5,648 views
0 votes
2 answers

Finding WebDriver element with Class Name in java

The better way to handle this element ...READ MORE

answered Apr 10, 2018 in Selenium by nsv999
• 5,500 points
13,738 views
0 votes
2 answers

Problem while using InternetExplorerDriver in Selenium WebDriver

enable trusted connection  in internet explorer by ...READ MORE

answered Aug 31, 2020 in Selenium by Sri
• 3,190 points
9,174 views
0 votes
1 answer

Geo-location microphone camera pop up

To Allow or Block the notification, access using Selenium and you have to ...READ MORE

answered May 11, 2018 in Selenium by Samarpit
• 5,910 points
7,252 views
0 votes
2 answers

How to use such xpath to find web elements

xpath are two types. 1) Absolute XPath:    /html/b ...READ MORE

answered Sep 3, 2020 in Selenium by Sri
• 3,190 points
7,890 views
0 votes
1 answer

What are the main functions of Xpath locator in Selenium?

Hi Danish, for handling complex and dynamic ...READ MORE

answered May 9, 2019 in Selenium by Anvi
• 14,150 points
1,028 views
0 votes
1 answer

What are the major components of Selenium IDE?

Hi Janvi, Selenium IDE is divided into different ...READ MORE

answered May 13, 2019 in Selenium by Anvi
• 14,150 points
4,352 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP