`
DavyJones2010
  • 浏览: 151899 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

Java Logging Techniques Summary(Commons-Logging Example)

阅读更多

1. In last chapter, we introduced Log4j in detail.

    How can we use commons-logging for the existing project?

    1) Add dependency for commons-logging in pom.

<dependency>
	<groupId>commons-logging</groupId>
	<artifactId>commons-logging</artifactId>
	<version>1.1.2</version>
</dependency>

    2) Test case

package edu.xmu.log4j;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.Test;

public class CommonsLoggingExampleTest
{
	private static Log logger = LogFactory.getLog("edu.xmu");

	@Test
	public void test()
	{
		// TRACE < DEBUG < INFO < WARN < ERROR
		logger.trace("[TRACE] This is TRACE messate");
		logger.debug("[DEBUG] This is DEBUG message");
		logger.info("[INFO] This is INFO message");
		logger.warn("[WARN] This is WARN message");
		logger.error("[ERROR] This is ERROR message");
	}

}

   3) The configuration file is still log4j.properties in class path.

   4) Now we start test. And the result same with the result using Log4j.

   Summary:

        1) The integration of Log4j and Commons-logging is quite simple. We just change the way we get Logger.

        2) But in Log4j/JUL, we can change the Appender/Layout and their level in the Logger in code. And this is disabled by JCL.

        3) You may wonder how it comes using Log4j instead of JUL or other Logging System.

            Please refer to part 3 which illustrates the work flow of getLog in detail.

 

2. Introduction to Commons-Logging

        1) It's really simple using commons-logging which we should merely use two important class:

            1) org.apache.commons.logging.Log

            2) org.apache.commons.logging.LogFactory

        2) The level for each logger and appender/handler is defined in thier configuration files.

            Add appender/handler and layout/formatter is depreciated and cannot migrate to JCL.

 

3. What happened when we call LogFactory.getLog()?


 

 4. So we can add commons-logging.properties under class path to make it clear which Log System we are actually using.

# Set Log4j as logging implementation
org.apache.commons.logging.Log=org.apache.commons.logging.impl.Log4JLogger
org.apache.commons.logging.LogFactory=org.apache.commons.logging.impl.LogFactoryImpl
# Set JUL as logging implementation
# org.apache.commons.logging.Log=org.apache.commons.logging.impl.Jdk14Logger
# Set LogKit as logging implementation
# org.apache.commons.logging.Log=org.apache.commons.logging.impl.LogKitLogger
# Set SimpleLog as logging implementation
# org.apache.commons.logging.Log=org.apache.commons.logging.impl.SimpleLog

Reference Links:

    1) http://commons.apache.org/proper/commons-logging/guide.html

    2) http://blog.163.com/haizai219@126/blog/static/444125552008102135348299/

    3) http://articles.qos.ch/thinkAgain.html

  • 大小: 46.9 KB
分享到:
评论

相关推荐

    commons-logging.jar

    commons-logging-java1.1.jar, commons-logging-optional.jar, commons-logging-osgi-1.0.jar, commons-logging-osgi.jar, commons-logging-REDUCED.jar, commons-logging-tests.jar, commons-logging_1.0.3.jar, ...

    commons-logging-1.1.3-API文档-中文版.zip

    赠送jar包:commons-logging-1.1.3.jar; 赠送原API文档:commons-logging-1.1.3-javadoc.jar; 赠送源代码:commons-logging-1.1.3-sources.jar; 赠送Maven依赖信息文件:commons-logging-1.1.3.pom; 包含翻译后...

    commons-logging-1.2-API文档-中英对照版.zip

    赠送jar包:commons-logging-1.2.jar; 赠送原API文档:commons-logging-1.2-javadoc.jar; 赠送源代码:commons-logging-1.2-sources.jar; 包含翻译后的API文档:commons-logging-1.2-javadoc-API文档-中文...

    commons-logging-1.2-bin.zip下载

    总结来说,Apache Commons Logging-1.2是一个为Java应用程序提供日志服务的工具包,它的核心优势在于其灵活性和可扩展性,使得开发者可以根据需求轻松更换日志实现。通过下载和使用这个"commons-logging-1.2-bin.zip...

    spring-framework & commons-logging

    spring-framework & commons-logging spring-framework & commons-logging spring-framework & commons-logging spring-framework & commons-logging spring-framework & commons-logging spring-framework & ...

    commons-logging-1.1.3-API文档-中英对照版 (2).zip

    赠送jar包:commons-logging-1.1.3.jar; 赠送原API文档:commons-logging-1.1.3-javadoc.jar; 赠送源代码:commons-logging-1.1.3-sources.jar; 赠送Maven依赖信息文件:commons-logging-1.1.3.pom; 包含翻译后...

    commons-logging-1.2-API文档-中文版.zip

    赠送jar包:commons-logging-1.2.jar; 赠送原API文档:commons-logging-1.2-javadoc.jar; 赠送源代码:commons-logging-1.2-sources.jar; 包含翻译后的API文档:commons-logging-1.2-javadoc-API文档-中文...

    commons-logging-1.1.1-API文档-中文版.zip

    赠送jar包:commons-logging-1.1.1.jar 赠送原API文档:commons-logging-1.1.1-javadoc.jar 赠送源代码:commons-logging-1.1.1-sources.jar 包含翻译后的API文档:commons-logging-1.1.1-javadoc-API文档-中文...

    jar包下载 axis mysql-connector-java commons-beanutils commons-logging apache-cxf

    本人曾经花费很多积分在各处下载的jar包,现将所用过得常用jar包打包下载...axis.jar,poi-3.17.jar,mysql-connector-java-5.0.4-bin.jar,java.util学习包,commons-beanutils与commons-logging,apache-cxf-3.2.1 包

    开发工具 commons-logging-1.2

    开发工具 commons-logging-1.2开发工具 commons-logging-1.2开发工具 commons-logging-1.2开发工具 commons-logging-1.2开发工具 commons-logging-1.2开发工具 commons-logging-1.2开发工具 commons-logging-1.2开发...

    commons-logging-1.2.JAR开源包

    标题中的"commons-logging-1.2.JAR"是Commons Logging 1.2版本的JAR包,它是该库的二进制发行版,包含了所有用于提供日志服务的类和接口。开发者可以将这个JAR文件添加到项目的类路径中,以便利用其提供的日志功能。...

    commons-logging-1.1.1.jar,commons-httpclient-3.1.jar,commons-codec-1.4.jar

    本场景中涉及的三个关键库是“commons-logging-1.1.1.jar”,“commons-httpclient-3.1.jar”以及“commons-codec-1.4.jar”。这三个jar包分别提供了日志记录、HTTP客户端通信和编码解码功能,是实现短信发送不可或...

    commons-logging-1.2_commonslogging_

    在标题"commons-logging-1.2_commonslogging_"中提到的"commons-logging-1.2.jar"就是这个库的1.2版本,它是Spring框架中常用的一个依赖,用于处理日志记录。 Spring框架广泛使用Commons Logging作为其默认的日志...

    commons-beanutils/commons-collections-3.2.1/commons-lang-2.5/commons-logging-1.1

    Json开发所必需的jar包,以及关联的所以jar包。 commons-beanutils.jar commons-collections-3.2.1.jar commons-lang-2.5.jar commons-logging-1.1.jar ezmorph-1.0.6.jar json-lib-2.3-jdk15.jar

    commons-logging-1.2-bin2014最新版

    标题提到的"commons-logging-1.2-bin2014最新版"是Apache Commons Logging项目的1.2版本,发布于2014年,它是这个库的一个二进制分发包。 在Java开发中,Spring框架广泛使用了Apache Commons Logging。这是因为...

    commons-httpclient3.1.jar,commons-codec1.3.jar,commons-logging1.1.1.jar

    首先,`commons-logging-1.1.1.jar`为HTTP客户端提供日志服务;接着,`commons-codec-1.3.jar`提供编码和解码功能,使得HTTP请求和响应的数据能够正确处理;最后,`commons-httpclient-3.1.jar`作为主要的HTTP客户端...

    JavaEE源代码 commons-logging-1.0.4

    JavaEE源代码 commons-logging-1.0.4JavaEE源代码 commons-logging-1.0.4...commons-logging-1.0.4JavaEE源代码 commons-logging-1.0.4JavaEE源代码 commons-logging-1.0.4JavaEE源代码 commons-logging-1.0.4Java

    commons-logging-1.1.1.jar和java-unrar-0.3.jar

    总之,"java-unrar-0.3.jar"和"commons-logging-1.1.1.jar"为Java开发者提供了处理RAR文件的强大工具,使得在Java环境中解压RAR文件变得简单易行,同时通过Commons Logging提供了灵活的日志记录机制。在项目中正确...

    commons-codec-1.3.jar,commons-httpclient-3.1.jar,commons-logging-1.1.jar)

    3. **commons-logging-1.1.jar**:这个JAR文件是Apache Commons Logging库的1.1版本。Logging库提供了一个接口层,允许应用程序在不关心具体日志实现的情况下进行日志记录。开发者可以使用它来跟踪图片上传过程中的...

    commons-logging-1.2-bin.rar jar包

    commons-logging-1.2-bin.rar jar包,包含commons-compress-1.12.jar、commons-logging-1.2-javadoc.jar、xmlbeans-2.3.0.jar、commons-lang-2.6-bin.zip等jar包

Global site tag (gtag.js) - Google Analytics