Skip to main content

Log4j Configuration

First, include  Log4j  jar file in your project (e.g. log4j-1.2.8.jar)

From http://www.javabeat.net/tips/82-baisc-steps-to-configure-log4j-using-xml-and.html

Configure Log4j

This example demonstrated how to configure Log4j setup using the Proerties file and XML file. These are the two most widely used techniques for configuring the Log4j for your application. But, in the recent days configuring with properties files are considered to be old technique and recommended using XML.
This example program uses simple satndalone java program for running the example. But, in most of the project scenarios it will be used in the web application. However the configuration file will be the same.

log4j.properties


# Set root logger level to DEBUG and its only appender to Appender1.
log4j.rootLogger=INFO, Appender1,Appender2

# Appender1 is set to be a ConsoleAppender.
log4j.appender.Appender1=org.apache.log4j.ConsoleAppender
log4j.appender.Appender2=org.apache.log4j.RollingFileAppender
log4j.appender.Appender2.File=sample.log


# Appender2 uses PatternLayout.
log4j.appender.Appender1.layout=org.apache.log4j.PatternLayout
log4j.appender.Appender1.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n

log4j.appender.Appender2.layout=org.apache.log4j.PatternLayout
log4j.appender.Appender2.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n


log4j.xml



   

Log4jPropertyTest.java


package javabeat.net.log4j;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

/**
 * source : www.javabeat.net
 */
public class Log4jPropertyTest {
    private static Logger logger = Logger.getLogger(Log4jPropertyTest.class);
    public static void main (String args[]){        
        PropertyConfigurator.configure("log4j.properties");
        logger.info("Test Log");
    }
}


Log4jXmlTest.java


package javabeat.net.log4j;

import org.apache.log4j.Logger;
import org.apache.log4j.xml.DOMConfigurator;

/**
 * source : www.javabeat.net
 */
public class Log4jXmlTest {
    private static Logger logger = Logger.getLogger(Log4jXmlTest.class);
    public static void main (String args[]){        
        DOMConfigurator.configure("log4j.xml");
        logger.info("Test Log");
    }

}

Comments

Popular posts from this blog

Regression Testing

From  http://www.softwaretestinghelp.com/regression-testing-tools-and-methods/ The selective retesting of a  software  system that has been modified to ensure that any  bugs  have been fixed and that no other previously working functions have failed as a result of the reparations and that newly added features have not created problems with previous versions of the  software . Also referred to as  verification testing , regression testing is initiated after a  programmer  has attempted to fix a recognized problem or has added  source code  to a program that may have inadvertently introduced errors. It is a  quality  control measure to ensure that the newly modified code still complies with its specified requirements and that unmodified code has not been affected by the maintenance activity. What is Regression Software Testing? Regression means retesting the unchanged parts of the application. Test cases are re-execu...

ThreadPoolExecutor使用

From 洞玄的博客 http://dongxuan.iteye.com/blog/901689 jdk官方文档(javadoc)是学习的最好,最权威的参考。 文章分上中下。上篇中主要介绍ThreadPoolExecutor接受任务相关的两方面入参的意义和区别,池大小参数 corePoolSize和 maximumPoolSize,BlockingQueue选型( SynchronousQueue, LinkedBlockingQueue, ArrayBlockingQueue );中篇中主要聊聊与 keepAliveTime这个参数相关的话题;下片中介绍一下一些比较少用的该类的API,及他的近亲: ScheduledThreadPoolExecutor 。 如果理解错误,请直接指出。 查看JDK帮助文档,可以发现该类比较简单,继承自AbstractExecutorService,而AbstractExecutorService实现了ExecutorService接口。 ThreadPoolExecutor的完整构造方法的签名是: ThreadPoolExecutor (int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit  unit, BlockingQueue < Runnable > workQueue, ThreadFactory  threadFactory, RejectedExecutionHandler  handler)   先记着,后面慢慢解释。 ===============================神奇分割线================================== 其实对于ThreadPoolExecutor的构造函数网上有N多的解释的,大多讲得都很好,不过我想先换个方式, 从Executors这个类入手 。因为他的几个构造工厂构造方法名字取得令人很容易了解有什么特点。但是其实Executors类的底层实现便是ThreadPoolExecutor! ThreadPoolEx...

Spring MVC 3.2 Preview: Introducing Servlet 3, Async Support

Continuing the Spring 3.0 "simplification series" started by Keith and Chris , I would like to provide a quick overview of simplifications in scheduling and task execution enabled by Spring 3.0. I will be walking through a basic sample application that you can checkout from the spring-samples Subversion repository. It has been designed to be as simple as possible while showcasing both annotation-driven and XML-based approaches to scheduling tasks in Spring 3.0. Let's begin with the annotation-driven approach. You can run it directly via the main() method in AnnotationDemo. If you take a look, you'll see that it's nothing more than a bootstrap for a Spring ApplicationContext: public static void main(String[] args) {      new ClassPathXmlApplicationContext( "config.xml" , AnnotationDemo. class ); } The reason nothing else is necessary is that the ApplicationContext contains an "active" component, which we will...