What are the main components of Log4j?

0 votes
What are the main components of log4j?
May 21 in Selenium by Kiran
51 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 21 by Anvi
• 13,960 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 7 in Selenium by Fuji
327 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 in Selenium by Nabarupa Das
41 views
0 votes
1 answer
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 in Selenium by Abha
• 27,180 points
114 views
0 votes
1 answer

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,110 points
1,147 views
0 votes
1 answer
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,150 points
1,246 views
0 votes
1 answer

How to use such xpath to find web elements

It's much simpler to use: #MyDiv123 > div.super or ...READ MORE

answered Jun 14, 2018 in Selenium by Samarpit
• 5,150 points
1,002 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 in Selenium by Anvi
• 13,960 points
85 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 in Selenium by Anvi
• 13,960 points
172 views