Advertisement

Udemy WW Udemy WW

Tuesday, 29 November 2011

Sample code to configure Logger (Log4j)

Jar file needed:
log4j-1.2.17 or newer

Configuration of Logger (log4j.xml)

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration debug="true"
                     xmlns:log4j='http://jakarta.apache.org/log4j/'>

   <appender name="consoleAppender" class="org.apache.log4j.ConsoleAppender">
      <layout class="org.apache.log4j.PatternLayout">
         <param name="ConversionPattern" value="%d{dd MMM yyyy HH:mm:ss} %5p %c{1} - %m%n"/>
      </layout>
   </appender>

   <appender name="fileAppender" class="org.apache.log4j.RollingFileAppender">
      <param name="append" value="true"/>
      <param name="file" value="log/pushLog.log"/>
      <layout class="org.apache.log4j.PatternLayout">
         <param name="ConversionPattern" value="%d{dd MMM yyyy HH:mm:ss} %5p %c{1} - %m%n"/>
      </layout>
   </appender>

   <root>
      <level value="INFO"/>
      <appender-ref ref="consoleAppender"/>
      <appender-ref ref="fileAppender"/>
   </root>

</log4j:configuration>

Java class 


import org.apache.log4j.Logger;
public class SampleLogger {
private static final Logger m_log = Logger.getLogger(SampleLogger.class);
public static void main(String arg[])
{

m_log.info("Application start...");
try{
// your code
}catch(Exception e)
{
m_log.info("Exception occured...");
m_log.info(e.getMessage());

m_log.info("Application End...");

}

}

2 comments:

  1. Everything is very open with a precise explanation of the challenges.
    It was definitely informative. Your website is very useful.
    Many thanks for sharing!

    Feel free to surf to my webpage ... garcinia cambogia select where to buy

    ReplyDelete
  2. I hardly leave responses, but after reading through
    a few of the comments on this page "site to shrink url http://adf.ly/3wJT9".
    I do have 2 questions for you if you don't mind. Is it just me or do some of these remarks come across like they are coming from brain dead people?
    :-P And, if you are writing on other places, I'd like to follow
    anything new you have to post. Would you make a list of every
    one of all your social sites like your twitter feed, Facebook
    page or linkedin profile?

    Here is my weblog buy garcinia cambogia select; ,

    ReplyDelete

Advertisement

Udemy WW