Hi Kiran, Log4j has four main components:
-
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:
-
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.
-
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
-
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: