Commons项目Logging组件的办法是将记录日志的功能封装为一组标准的API,使 其底层实现可以任意修改和变换。使用Commons的Logging API非常简单。只需导入Logging的两个必须类、创建一个Log的静态实例
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class CommonLogTest {
private static Log log = LogFactory.getLog(CommonLogTest.class);
public static void main(String[] args) {
log.error("ERROR");
log.debug("DEBUG");
log.warn("WARN");
log.info("INFO");
log.trace("TRACE");
System.out.println(log.getClass());
}
}
1. 在不指定任何属性的情况下运行这个程序,这时默认将使用Jdk14Logger;
2. 然后指定系统属性 -Jorg.apache.commons.logging.Log=org.apache.commons.logging.impl.SimpleLog 再运行程序,这时日志记录工具将是SimpleLog;
3. 把Log4J的类放入CLASSPATH,只要正确设置了log4j的log4j.properties配置文件,就可以得到Log4JLogger输出 的信息。如果没有设置log4j.properties配置文件,则会输出以下提示:
class org.apache.commons.logging.impl.Log4JLogger
log4j:WARN No appenders could be found for logger (CommonLogTest).
log4j:WARN Please initialize the log4j system properly.
public static void main(String[] args) {//自己替换[]
System.setProperty("org.apache.commons.logging.Log",
"org.apache.commons.logging.impl.Jdk14Logger");
Log log = LogFactory.getLog("com.discursive.jccook.SomeApp");
if (log.isTraceEnabled()) {
log.trace("This is a trace message");
}
if (log.isDebugEnabled()) {
log.debug("This is a debug message");
}
log.info("This is an informational message");
log.warn("This is a warning");
log.error("This is an error");
log.fatal("This is fatal");
}
LogFactory.getLog方法会根据底层环境返回一个适当的Log实现。如果用户想指定一个具体的Logging系统实现,可以设置org.apache.commons.logging.Log系统属性。例如:
System.setProperty("org.apache.commons.logging.Log",
"org.apache.commons.logging.impl.Log4JLogger");
这样就会使用Log4J作为Logging系统。
org.apache.commons.logging.Log可以设定为:
•org.apache.commons.logging.impl.Log4JLogger 使用Log4J
•org.apache.commons.logging.impl.Jdk14Logger 使用JDK 1.4 Logging框架
•org.apache.commons.logging.impl.SimpleLog 使用Commons Logging内置的简单Log实现
其他:
总结一下,Commons Logging会按照下列顺序来指定具体的Log实现。
•如果定义了org.apache.commons.logging.Log系统参数,实用指定的Logging实现。
•如果在CLASSPATH里发现了Log4J,使用Log4J。
•如果使用的是JDK1.4,使用JDK1.4内置的Logging框架。
•如果都没有找到,则使用Commons Logging内置的简单Log实现。
分享到:
相关推荐
Commons-logging API apache Commons logging API
Apache Commons Logging 1.2
apache commons logging1.1.2官方JAR包,供亲们下载~~
spring日志commons-loggingjar包,版本1.2
commons-logging-1.0-javadoc.jar, commons-logging-1.0.1-javadoc.jar, commons-logging-1.0.1.jar, commons-logging-1.0.2-javadoc.jar, commons-logging-1.0.2.jar, commons-logging-1.0.3-javadoc.jar, commons-...
Commons Logging.zip
commons-logging-1.2:java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory解决方法
httpclient4.5所需要的全部jar包,包括commonslogging等7个
内涵多个个 java web 常用 jar 包 c3p0-0.9.2-pre1、Commons Logging、dbcp、dom4j、jstl、数据库驱动、dbutils
spring-framework & commons-logging spring-framework & commons-logging spring-framework & commons-logging spring-framework & commons-logging spring-framework & commons-logging spring-framework & ...
commons-logging-1.2的jar包加源码,还有commons-bean utils-1.9.2.jar一起打包!
commons-logging-1.1资源,有需要的可以下载,下载,下载
spring开发中commons-logging.jar包的功能 删除后程序会报错 Java.lang.NoClassDefFoundError 记录日志,通常和 log4j.jar共同使用 原因: 在 sun 开发 logger 前,apache 项目已经开发了功能强大的 log4j ...
Artifact:jdtaus-core-commons-logging Version:1.0 Name:jDTAUS Core RI Commons Logging Description:jDTAUS commons logging reference implementation. URL:...
commons-logging, JAVA Web开发中常用的jar
Jakarta Commons Logging (JCL)提供的是一个日志(Log)接口(interface),同时兼顾轻量级和不依赖于具体的日志实现工具。它提供给中间件/日志工具开发者一个简单的日志操作抽象,允许程序开发人员使用不同的具体日志...
赠送jar包:commons-logging-1.1.3.jar; 赠送原API文档:commons-logging-1.1.3-javadoc.jar; 赠送源代码:commons-logging-1.1.3-sources.jar; 赠送Maven依赖信息文件:commons-logging-1.1.3.pom; 包含翻译后...
搭建spring MVC 必备的日志框架 commons-logging 我也想免费 可是不容我啊
赠送jar包:commons-logging-1.2.jar; 赠送原API文档:commons-logging-1.2-javadoc.jar; 赠送源代码:commons-logging-1.2-sources.jar; 包含翻译后的API文档:commons-logging-1.2-javadoc-API文档-中文...