沃梦达 / 编程技术 / 服务器 / 正文

Apache CommonLogging + Log4J

package cn.byref.demo.logging;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;public class CommonLoggingTest {public static void main(String[] args) {Log logger = Lo...

            package cn.byref.demo.logging;

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

public class CommonLoggingTest {
    public static void main(String[] args) {
        
        Log logger = LogFactory.getLog(CommonLoggingTest.class);
//        Logger logger = Logger.getLogger(CommonLoggingTest.class);
//        log.info("load test");
        logger.debug("CommonLoggingTest log");
    }
}
        
            # An example log4j configuration file that outputs to System.out.  The
# output information consists of relative time, log level, thread
# name, logger name, nested diagnostic context and the message in that
# order.

# For the general syntax of property based configuration files see the
# documenation of org.apache.log4j.PropertyConfigurator.

log4j.rootLogger=DEBUG,B1,A2
log3j.logger.cn.byref.demo.logging.CommonLoggingTest = DEBUG, B1

# A1 is set to be a ConsoleAppender which outputs to System.out. 
log4j.appender.B1=org.apache.log4j.ConsoleAppender

# A1 uses PatternLayout.
log4j.appender.B1.layout=org.apache.log4j.PatternLayout

# The conversion pattern uses format specifiers. You might want to
# change the pattern an watch the output format change.
log4j.appender.B1.layout.ConversionPattern=%-4r %-5p [%t] %37c %3x - %m%n

# In this example, we are not really interested in INNER loop or SWAP
# messages. See the effects of uncommenting and changing the levels of
# the following loggers.
# log4j.logger.org.apache.log4j.examples.SortAlgo.INNER=WARN
# log4j.logger.org.apache.log4j.examples.SortAlgo.SWAP=WARN

# Appender A2 writes to the file "test".
log4j.appender.A2=org.apache.log4j.FileAppender
log4j.appender.A2.File=test.log

# Truncate ‘test‘ if it aleady exists.
log4j.appender.A2.Append=true

# Appender A2 uses the PatternLayout.
log4j.appender.A2.layout=org.apache.log4j.PatternLayout
log4j.appender.A2.layout.ConversionPattern=%-5r %-5p [%t] %c{2} - %m%n
 

有必要详细说明一下调用LogFactory.getLog()时发生的事情。调用该函数会启动一个发现过程,即找出必需的底层日志记录功能的实现,具体的发现过程在下面列出。注意,不管底层的日志工具是怎么找到的,它都必须是一个实现了Log接口的类,且必须在CLASSPATH之中。Commons Logging API直接提供对下列底层日志记录工具的支持:Jdk14Logger,Log4JLogger,LogKitLogger,NoOpLogger (直接丢弃所有日志信息),还有一个SimpleLog。

⑴ Commons的Logging首先在CLASSPATH中查找commons-logging.properties文件。这个属性文件至少定义org.apache.commons.logging.Log属性,它的值应该是上述任意Log接口实现的完整限定名称。如果找到org.apache.commons.logging.Log属相,则使用该属相对应的日志组件。结束发现过程。

⑵ 如果上面的步骤失败(文件不存在或属相不存在),Commons的Logging接着检查系统属性org.apache.commons.logging.Log。 如果找到org.apache.commons.logging.Log系统属性,则使用该系统属性对应的日志组件。结束发现过程。

⑶ 如果找不到org.apache.commons.logging.Log系统属性,Logging接着在CLASSPATH中寻找log4j的类。如果找到了,Logging就假定应用要使用的是log4j。不过这时log4j本身的属性仍要通过log4j.properties文件正确配置。结束发现过程。

⑷ 如果上述查找均不能找到适当的Logging API,但应用程序正运行在JRE 1.4或更高版本上,则默认使用JRE 1.4的日志记录功能。结束发现过程。

⑸ 最后,如果上述操作都失败(JRE 版本也低于1.4),则应用将使用内建的SimpleLog。SimpleLog把所有日志信息直接输出到System.err。结束发现过程。

原文:http://www.cnblogs.com/byxxw/p/4895346.html

本文标题为:Apache CommonLogging + Log4J

基础教程推荐