Java Logging Techniques Summary(Commons-Logging Example)

本文介绍了如何在已有项目中使用commons-logging替代Log4j进行日志记录,并通过示例代码展示了配置和使用方法。同时,解释了两者之间的区别,包括如何设置日志级别、添加处理器和布局,以及如何通过配置文件进行全局配置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值