






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An overview of the log4j logging framework, focusing on its three main components: appenders, loggers, and layouts. It explains how to use different appenders such as consoleappender, fileappender, and writerappender, and discusses the methods of logger class like debug(), warn(), and error(). The document also covers the configuration of log4j using properties file and xml file, as well as the use of basicconfigurator.
Typology: Lecture notes
1 / 11
This page cannot be seen from the preview
Don't miss anything!







In this section we will learn about the architectural component of Log4J. Understanding the architecture of Log4J is important. Three main component of Log4J The architecture of Log4J framework is layered and consists of three main components. There components of the Log4J are:
LogExample1.java import org.apache.log4j.Logger; public class LogExample1 { public LogExample() { } static Logger log = Logger.getLogger(LogExample. class ); public static void main(String argsp[]) { log.debug("Here is some DEBUG"); log.info("Here is some INFO"); log.warn("Here is some WARN"); log.error("Here is some ERROR"); log.fatal("Here is some FATAL"); } } log4j.properties log4j.rootLogger=debug, stdout log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.Target=System.out log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=[%5p] %d{mm:ss} (%F:%M:%L)%n%m%n%n This Example shows you how to create a log in a Servlet. Description of the code: Logger.getLogger(): Logger class is used for handling the majority of log operations and getLogger method is used for return a logger according to the value of the parameter. If the logger already exists, then the existing instance will be returned. If the logger is not already exist, then create a new instance. log.info(): This method is used to check that the specified category is INFO enabled or not, if yes then it converts the massage passed as a string argument to a string by using appropriate object renderer of class ObjectRenderer.
log4jexample2.java import org.apache.log4j.Logger; class CustomerOrder { private String productName; private int productCode; private int productPrice; public int getProductCode() { return productCode; } public void setProductCode( int productCode) { this .productCode = productCode; } public String getProductName() { return productName; } public void setProductName(String productName) { this .productName = productName; } public int getProductPrice() { return productPrice; } public void setProductPrice( int productPrice) { this .productPrice = productPrice; } public CustomerOrder(String productName, int productCode, int productPrice) { this .productName = productName; this .productCode = productCode; this .productPrice = productPrice; } } public class Log4jExample2 { private static Logger logger = Logger.getLogger("name"); public void processOrder(CustomerOrder order) { logger.info(order.getProductName()); } public static void main(String args[]) { CustomerOrder order1 = new CustomerOrder("Beer", 101, 20); CustomerOrder order2 = new CustomerOrder("Lemonade", 95, 10); CustomerOrder order3 = new CustomerOrder("Chocolate", 223, 5);
BasicConfigurator.configure(); logger.debug("Hello world."); } } Now after running this code you will get the following output on your console: Output: 0 [main] DEBUG SimpleLog.class ‐ Hello world. where "0" shows time taken in milliseconds from start of program to the logging request. "Hello world" is the message that we have given. SimpleLog.class is the logger name and in bracket [main] is the thread who have invoked logging.
In earlier example you have seen that we can define properties of log4j logging file with the help of log4j.properties. Properties can also be defined within log4j.xml file. You have to add following contents to this XML file. log4j.xml
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L ‐ %m%n log4j.rootLogger=debug, stdout Here is our SimpleLog class code: SimpleLog.java import org.apache.log4j.*; public class SimpleLog { static Logger logger = Logger.getLogger("SimpleLog.class"); public static void main(String[] args) { logger.debug("Welcome to Log4j"); } } Output: 13:07:40,875 DEBUG class:5 ‐ Welcome to Log4j
In this log4j console appender tutorial you will be introduced with ConsoleAppender which is used in Log4j for appending output on the console each and every time when any info(), debug(), error() method is invoked by the logger. n this log4j console appender tutorial you will be introduced with ConsoleAppender which is used in Log4j for appending output on the console each and every time when any info() , debug() , error() method is invoked by the logger. In our example we have made a class ConsoleAppenderExample. After this we have created a logger object by invoking static method getLogger(). After this we have created an object of ConsoleAppender and to add this logger message we need to add appender to logger. Following code does it for us. "logger.addAppender(conappender)" where conappender is ConsoleAppender's object. Here is the example code for ConsoleAppender example : ConsoleAppenderExample.java import org.apache.log4j.*; public class ConsoleAppenderExample { static Logger logger = Logger.getLogger( "ConsoleAppenderExample.class"); public static void main(String[] args) { ConsoleAppender conappender =
FileAppenderExample.java import org.apache.log4j.*; public class FileAppenderExample { static Logger logger = Logger.getLogger("FileAppenderExample.class"); public static void main(String[] args) throws Exception { FileAppender fileappender = new FileAppender( new PatternLayout(),"output.txt"); logger.addAppender(fileappender); logger.info("Log has been appended to your output.txt"); logger.info("See your output.txt"); } } Compilation and execution of FileAppenderExample class will create "output.txt" file for the first time then will append log events to it. Output:
In our previous section we have seen that we can append log events in simple file (plain/text) file and to the console also. In our previous section we have seen that we can append log events in simple file (plain/text) file and to the console also. If we want to write files then we have to use WriterAppender. WriterAppender appends log events to a Writer or an OutputStream depending on the user's choice. In our example we have created a class WriterAppenderExample. First we need to create logger object and then we have to add appender to it. So we have created an object of WriterAppender and since it requires Writer or OutputStream we are using a file "Writer.html". We have taken layout of HTML format that therefore we have to use HTMLLayout. logger.addAppender(writeappender); will add writerappender to logger and all logging events will be added to this HTML file. Here is the example code for WriterAppenderExample class:
import java.io.; import org.apache.log4j.; public class WriterAppenderExample { static Logger logger = Logger.getLogger("WriterAppenderExample.class"); public static void main(String[] args) { try { FileOutputStream filename= new FileOutputStream("Writer.html"); WriterAppender writeappender = new WriterAppender( new HTMLLayout(),filename); logger.addAppender(writeappender); logger.info("Welcome"); logger.info("to"); logger.info("Rose India"); logger.info("Writer Appender"); logger.info("Example"); logger.info("‐‐‐‐‐‐‐‐‐‐‐"); } catch (Exception e){ System.out.println("Exception is ="+e.getMessage()); } } }