Showing posts with label Log4J. Show all posts
Showing posts with label Log4J. Show all posts

Thursday, May 31, 2012

Log4j with multiple appenders using xml file.


Please follow the following program to understand log4j implementation for multiple appenders using xml file.
Java : MultipleAppenders
import org.apache.log4j.Logger;
import org.apache.log4j.xml.DOMConfigurator;

public class MultipleAppenders {

      private static final Logger logger = Logger.getLogger(MultipleAppenders.class);

      public static void main(String[] args) {
            DOMConfigurator.configure("log.xml");
            logger.debug("Debug Message.");// Method to Method.
            logger.info("Info Message.");// Information passing and recieving.
            logger.warn("Warning Message.");// Retrying again.
      }
}
XML: log.xml
<xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/'>
      <appender name="Console" class="org.apache.log4j.ConsoleAppender">
            <layout class="org.apache.log4j.PatternLayout">
                  <param name="ConversionPattern" value="%p %c - %m%n" />          
            </layout>        
      </appender>
      <appender name="Html" class="org.apache.log4j.FileAppender">
            <param name="File" value="sample.html"/>       
            <layout class="org.apache.log4j.HTMLLayout"/>                    
      </appender>
      <appender name="File" class="org.apache.log4j. FileAppender ">
            <param name="File" value="sample.log"/>
            <layout class="org.apache.log4j.PatternLayout">                   
                  <param name="ConversionPattern" value="%p %c - %m%n" />           
            </layout>        
      </appender>
      <root>
            <level value="trace" />
            <appender-ref ref="Console" />
            <appender-ref ref="File" />
            <appender-ref ref="Html" />
      </root>
</log4j:configuration>

Log4j with multiple appenders using properties.


Please follow the following program to understand log4j implementation for multiple appenders using properties file.

Java : MultipleAppenders
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

public class MultipleAppenders {

      private static final Logger logger = Logger.getLogger(MultipleAppenders.class);

      public static void main(String[] args) {
            PropertyConfigurator.configure("log.properties");
            logger.debug("Debug Message.");// Method to Method.
            logger.info("Info Message.");// Information passing and recieving.
            logger.warn("Warning Message.");// Retrying again.
      }
}
Properties: log.properties
log4j.rootLogger=DEBUG, Console, File, Html

#Console Appender
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=%d %p %c --> %M - %m%n
log4j.appender.Console.Threshold = DEBUG

#File Appender
log4j.appender.File=org.apache.log4j.FileAppender
log4j.appender.File.File=file.log
log4j.appender.File.layout=org.apache.log4j.PatternLayout
log4j.appender.File.layout.ConversionPattern=%d %p %c --> %M - %m%n
log4j.appender.File.Threshold = WARN

#File Appender with HTMLLayout
log4j.appender.Html=org.apache.log4j.FileAppender
log4j.appender.Html.File=file.html
log4j.appender.Html.layout=org.apache.log4j.HTMLLayout
log4j.appender.Html.Threshold = TRACE

Log4j using XML configuration.


Please follow the following program to understand log4j implementation using xml file.
Here I have used console appender to log the events.

Java : XMLConfigurationDemo
import org.apache.log4j.Logger;
import org.apache.log4j.xml.DOMConfigurator;

public class XMLConfigurationDemo {

      private static final Logger logger = Logger.getLogger(XMLConfigurationDemo.class);

      public static void main(String[] args) {
            DOMConfigurator.configure("log.xml");
            logger.trace("Trace Message.");// Point to Point.
            logger.debug("Debug Message.");// Method to Method.
      }
}
XML:log.xml
<xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/'>
      <appender name="Console" class="org.apache.log4j.ConsoleAppender">
            <layout class="org.apache.log4j.PatternLayout">
                  <param name="ConversionPattern" value="%p %c - %m%n" />
            </layout>
      </appender>
      <root>
            <level value="trace" />
            <appender-ref ref="Console" />
      </root>
</log4j:configuration>

Log4j using properties file.


Please follow the following program to understand log4j implementation.
Here I have used console appender to log the events.
Java File: PropertyConfiguratorDemo
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

public class PropertyConfiguratorDemo {

      private static final Logger logger = Logger.getLogger(PropertyConfiguratorDemo.class);

      public static void main(String[] args) {
            PropertyConfigurator.configure("log.properties");
            logger.trace("Trace Message.");//Point to Point.
            logger.debug("Debug Message.");//Method to Method.         
            logger.info("Info Message.");//Information passing and recieving.
            logger.warn("Warning Message.");//Retrying again.
            logger.error("Error Message.");//Error occured and can continue further.
            logger.fatal("Fatal Message.");//Error occured and needs to stop application.
      }
}
Properties File: log.properties
log4j.rootLogger=DEBUG, Console
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=%d %p %c --> %M - %m%n

Output:
2012-05-31 16:19:36,781 DEBUG com.lenin.log4j.PropertyConfiguratorDemo --> main - Debug Message.
2012-05-31 16:19:36,781 INFO com.lenin.log4j.PropertyConfiguratorDemo --> main - Info Message.
2012-05-31 16:19:36,781 WARN com.lenin.log4j.PropertyConfiguratorDemo --> main - Warning Message.
2012-05-31 16:19:36,781 ERROR com.lenin.log4j.PropertyConfiguratorDemo --> main - Error Message.
2012-05-31 16:19:36,781 FATAL com.lenin.log4j.PropertyConfiguratorDemo --> main - Fatal Message.


Pattern Descriptions in Log4j


Please follow the following program to understand the various letters used for pattern in log4j.

import java.io.IOException;

import org.apache.log4j.Appender;
import org.apache.log4j.ConsoleAppender;
import org.apache.log4j.Layout;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;

public class PatternDescriptions {

      private static final Logger logger = Logger.getLogger(PatternDescriptions.class);

      public static void main(String[] args) throws IOException {
            Layout layout = new PatternLayout("%c %C %d %F %l %L %m %M %p %t %n");
            //c --> Used to output the category of the logging event.
            //C --> Used to output the fully qualified class name of the caller.
            //d --> Used to output the date of the logging event.
            //F --> Used to output the file name where the logging request was issued.
            //l --> Used to output location information of the caller which generated the logging event.
            //L --> Used to output the line number from where the logging request was issued.
            //m --> Used to output the application supplied message associated with the logging event.
            //M --> Used to output the method name where the logging request was issued.
            //p --> Used to output the priority of the logging event.
            //t --> Used to output the name of the thread that generated the logging event.
            //n --> Outputs the platform dependent line separator character or characters.
            Appender appender = new ConsoleAppender(layout);
            logger.addAppender(appender);
            logger.trace("Trace Message.");
            logger.debug("Debug Message.");
            logger.info("Info Message.");
            logger.warn("Warning Message.");
            logger.error("Error Message.");
            logger.fatal("Fatal Message.");
      }
}

Logger with Java Configuration


Please follow the following program to implement log4j with Java Configuration and use the provided scenario's to use the specific logger methods.
import java.io.IOException;

import org.apache.log4j.Appender;
import org.apache.log4j.FileAppender;
import org.apache.log4j.Layout;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;

public class JavaLoggerDemo {

      private static final Logger logger = Logger.getLogger(JavaLoggerDemo.class);

      public static void main(String[] args) throws IOException {
            Layout layout = new PatternLayout("%-4r [%t] %-5p %c %m %n");
            Appender appender = new FileAppender(layout, "hello.log");
            logger.addAppender(appender);
            logger.trace("Trace Message.");
            logger.debug("Debug Message.");
            logger.info("Info Message.");
            logger.warn("Warning Message.");
            logger.error("Error Message.");
            logger.fatal("Fatal Message.");
      }
}

Monday, May 14, 2012

Logger with BasicConfigurator.

Please follow the following program to implement log4j with BasicConfigurator and use the provided scenario's to use the specific logger methods.


import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;

public class BasicConfiguratorDemo {

      private static final Logger loggerLogger.getLogger(BasicConfiguratorDemo.class);

      public static void main(String[] args) {
            BasicConfigurator.configure();
            logger.trace("Trace Message.");
            logger.debug("Debug Message.");
            logger.info("Info Message.");
            logger.warn("Warning Message.");
            logger.error("Error Message.");
            logger.fatal("Fatal Error Message.");
      }
}