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。结束发现过程。

Apache CommonLogging + Log4J的更多相关文章

  1. Java日志框架:SLF4J,Common-Logging,Log4J,Logback说明

    Log4j  Apache的一个开放源代码项目,通过使用Log4j,我们可以控制日志信息输送的目的地是控制台.文件.GUI组件.甚至是套接口服务 器.NT的事件记录器.UNIX Syslog守护进程等 ...

  2. 【JUnit 报错】java.lang.NoClassDefFoundError: org/apache/logging/log4j/message/Message

    使用JUnit的时候,报错:java.lang.NoClassDefFoundError: org/apache/logging/log4j/message/Message 原因是因为项目中导入的架包 ...

  3. 解决java log4j 配置log4jCaused by: java.lang.ClassNotFoundException: org.apache.logging.log4j.LogManager

    前提安装http://mirror.bit.edu.cn/apache/logging/log4j/2.11.2/apache-log4j-2.11.2-bin.zip Buildpath 配置add ...

  4. 解决log4j.xml问题http//jakarta.apache.org/log4j/ uri is not registered

    在Eclipse中,配置log4j.xml出现"http //jakarta.apache.org/log4j/ uri is not registered"的错误信息. 原始的l ...

  5. mybatis + log4j2 问题 java.lang.NoClassDefFoundError: org/apache/logging/log4j/spi/AbstractLoggerWrapper

    root cause java.lang.NoClassDefFoundError: org/apache/logging/log4j/spi/AbstractLoggerWrapper 网上资料比较 ...

  6. Log4j2 - Unable to invoke factory method in class org.apache.logging.log4j.core.appender.RollingFileAppender for element RollingFileAppender for element RollingFile

    问题与分析 在使用Log4j2时,虽然可以正确读取配置文件并生成log文件,但偶然发现控制台打印了异常信息如下: 2018-12-31 17:28:14,282 Log4j2-TF-19-Config ...

  7. Jetty启动报Error scanning entry META-INF/versions/9/org/apache/logging/log4j/util/ProcessIdUtil.class

    近日在项目中集成Elasticsearch后,Jetty启动报错. 错误日志如下: Suppressed: |java.lang.RuntimeException: Error scanning en ...

  8. java日志组件介绍(common-logging,log4j,slf4j,logback )

    转自:http://www.blogjava.net/daiyongzhi/archive/2014/04/13/412364.html common-logging是apache提供的一个通用的日志 ...

  9. 转:java日志组件介绍(common-logging,log4j,slf4j,logback )

    原网址:http://www.blogjava.net/daiyongzhi/archive/2014/04/13/412364.html common-logging common-logging是 ...

随机推荐

  1. Activity重要函数

    一.onConfigurationChanged 与 android:configChanges Lists configuration changes that the activity will ...

  2. 运行jupyter notebook 出错 Error executing Jupyter command 'notebook'

    实际上是安装jupyter时候有错误, 仔细看日志发现需要缺少 Microsoft Visual C++ Compiler for Python 2.7 下载安装后,重新安装jupyter即可 htt ...

  3. Java访问网络url,获取网页的html代码

    在Java中,Java.net包里面的类是进行网络编程的,其中,java.net.URL类和java.net.URLConection类是编程者方便地利用URL在Internet上进行网络通信.有两种 ...

  4. OpenGL纹理上下颠倒翻转的三种解决办法

    http://blog.csdn.net/narutojzm1/article/details/51940817 综述 在使用OpenGL函数加载纹理到图形时,经常遇到纹理上下颠倒的问题.原因是因为O ...

  5. linux性能调分析及调优

    转:https://blog.csdn.net/luokehua789789/article/details/53007456 Linux 性能分析以及调优介绍 写在前面:计算机要解决的基本问题之一是 ...

  6. linux音频 DAPM之二:audio paths与dapm kcontrol

    转:https://blog.csdn.net/wh_19910525/article/details/12749293 在用alsa_amixer controls时,除了我们之前提到的snd_so ...

  7. 写时拷贝(Copy On Write)方案详解

    本文旨在通过对 写时拷贝 的四个方案(Copy On Write)分析,让大家明白写时拷贝的实现及原理. 关于浅拷贝与深拷贝,我在之前的博客中已经阐述过了  浅拷贝容易出现指针悬挂的问题,深拷贝效率低 ...

  8. QGIS 编译

    QGIS 编译 在编译的过程中花费了很长时间,特别是编译Debug版本.release版本的编译可以从晚上找到很多的资料,但是Debug的编译相对较少.在Debug编译的过程中,需要单独build工程 ...

  9. maven项目在打war包时出现非法字符: '\ufeff' 解决方案

    问题描述: 开发工具MyEclipse 的总体开发环境,编码格式总体设置为UTF-8,在将web项目打包的时候出现:非法字符:'\ufeff" 错误. 解决方案: 利用notePad++打开 ...

  10. Fatal error: cannot create 'R_TempDir'

    [user@mgmt dir]$ R Fatal error: cannot create 'R_TempDir' [user@mgmt dir]$ ll -ad /tmp drwxrwxrwt. 2 ...