common logging的使用

<script type="text/javascript"> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>

Apache有很多有意思的开源的便利工具,像读取xml配置的Digist和一些common包。而这个common logging也是一个比较有名的log包。其实它只是简单封装了一些现有的log框架。这里只是简单介绍一下如何快速使用它,尤其是使用log4j和jdk1.4以上自带的log。
首先可以参看它的官方 User Guide,里面有很多基本的重要的信息,尤其是如何判断采用哪个log框架。具体的:
  1. Look for a configuration attribute of this factory named org.apache.commons.logging.Log (for backwards compatibility to pre-1.0 versions of this API, an attribute org.apache.commons.logging.log is also consulted). Configuration attributes can be set explicitly by java code, but they are more commonly set by placing a file named commons-logging.properties in the classpath. When such a file exists, every entry in the properties file becomes an "attribute" of the LogFactory. When there is more than one such file in the classpath, releases of commons-logging prior to 1.1 simply use the first one found. From release 1.1, each file may define a priority key, and the file with the highest priority is used (no priority definition implies priority of zero). When multiple files have the same priority, the first one found is used. Defining this property in a commons-logging.properties file is the recommended way of explicitly selecting a Log implementation.
  2. Look for a system property named org.apache.commons.logging.Log (for backwards compatibility to pre-1.0 versions of this API, a system property org.apache.commons.logging.log is also consulted).
  3. If the Log4J logging system is available in the application class path, use the corresponding wrapper class (Log4JLogger).
  4. If the application is executing on a JDK 1.4 system, use the corresponding wrapper class (Jdk14Logger).
  5. Fall back to the default simple logging wrapper (SimpleLog).
common logging只是一个简单封装不同log框架,所以在实际上日志输出的时候都是调用相应的框架,而日志输出的配置文件也都是各种log框架各自定义的,commong logging本身并不提供统一的定义文件。为了便于切换不同的log框架,我们可以提供一个common-logging.properties(这个文件名是默认的,如果你改成别的名字我不知道能不能找到,可能要进行某种java系统变量的设置吧)。当然如果一直只采用log4j的话,只要把log4j的jar包引入就好了,然后定义一个log4j的配置文件(默认log4j.properties)。所有的properties最好直接放在src文件夹下,其实只要放到classpath下都可以,但是没有必要。下面是common-logging.properties的内容:
#
#org.apache.commons.logging.LogFactory=
#  org.apache.commons.logging.impl.LogFactoryImpl

# SimpleLog
#org.apache.commons.logging.Log = org.apache.commons.logging.impl.SimpleLog

# JDK 1.4 logger
org.apache.commons.logging.Log=org.apache.commons.logging.impl.Jdk14Logger

# Avalon Toolkit
#org.apache.commons.logging.Log=org.apache.commons.logging.impl.LogKitLogger

# Log4j
#org.apache.commons.logging.Log=org.apache.commons.logging.impl.Log4JLogger

内容很容易理解了,就是定义commong logging用哪个log封装类,而这个封装类就会调用相应的log框架进行日志输出。这里采用jdk1.4 logger,也就是jdk自带的。如果你的jdk版本低于1.4的话,commong logging会有一个替代实现。

既然采用了jdk1.4logger,那么接下来就要设置log输出的配置文件logging.properties(放在src下,名字当然有默认的最稳妥),内容如下:
# handlers
handlers=java.util.logging.FileHandler, java.util.logging.ConsoleHandler
   
# general level
.level=INFO
   
# file handler
java.util.logging.FileHandler.pattern = %h/java%u.log
java.util.logging.FileHandler.limit = 50000
java.util.logging.FileHandler.count = 1
java.util.logging.FileHandler.formatter = java.util.logging.XMLFormatter
   
# console handler
java.util.logging.ConsoleHandler.level = FINEST
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter
test.de.jayefem.log4e.logkits.JDK1_4_Logging.level = FINEST

具体内容大致可以猜出来的,如果你了解log4j的话。接下来只要写如下的java代码就可以直接调用熟悉的Log.debug()了。
private static final Log log = LogFactory.getLogger(YourClass.class);
log.debug("debug");

如果要换用log4j的话,只要将common logging.properties中的启用log4j并注释掉jdk1.4,然后增加log4j.properties:
log4j.rootCategory=debug, console

log4j.appender.console=org.apache.log4j.ConsoleAppender

log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%4r [%t] %-5p %c %x - %m%n

对于上面同样的java代码,会采用log4j来做日志。

你还可以在 这里找到更多的一些log框架的配置。

<script type="text/javascript"> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值