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. Windows Server 2003 R2 With Sp2 序列号

    下载地址 ed2k://|file|cn_win_srv_2003_r2_enterprise_x64_with_sp2_vl_cd1_X13-47314.iso|647686144|107F10D2 ...

  2. Python基础-configparser和hashlib模块

    configparser模块 import configparser config = configparser.ConfigParser() #将配置写入到文件 config[', 'Compres ...

  3. zabbix-2.4.8-1添加MySQL状态监控

    1.安装zabbix-agentyum -y install zabbix-2.4.8-1.el6.x86_64.rpm zabbix-agent-2.4.8-1.el6.x86_64.rpm 安装以 ...

  4. SQL SREVER, ORACLE数据库连接字符串

    采用windows身份验证模式 Data Source=myServerAddress;Initial Catalog=myDataBase;Integrated Security=SSPI;

  5. JavaWeb:实现文件上传与下载

    JavaWeb:实现文件上传与下载 文件上传前端处理 本模块使用到的前端Ajax库为Axio,其地址为GitHub官网. 关于文件上传 上传文件就是把客户端的文件发送给服务器端. 在常见情况(不包含文 ...

  6. 安装MySQL的详细步骤

    安装步骤如下: 1.打开网址:http://www.mysql.com/ ↓ 2.选择“Download”->“Windows”(此安装步骤只是在Window10 中进行,如有需要,其他系统可参 ...

  7. Hibernate Student_Course_Score设计

    示例: 设计代码,实现在数据库中建student表.course表.和score表,展现三者关系 student表:id.name course表:id.name score表:id.score.st ...

  8. php数组函数-array_rand()

    array_rand()函数返回数组中的一个随机键名,或者如果指定函数返回键名不止一个,则返 回一个包含随机键名的数组. array_rand(array,number); array:必需.规定数组 ...

  9. 吴恩达深度学习笔记(十一)—— dropout正则化

    主要内容: 一.dropout正则化的思想 二.dropout算法流程 三.dropout的优缺点 一.dropout正则化的思想 在神经网络中,dropout是一种“玄学”的正则化方法,以减少过拟合 ...

  10. different between web api and web service

     https://stackoverflow.com/questions/19336347/what-is-the-difference-between-a-web-api-and-a-web-ser ...