Apache Commons-logging+log4j配置
来源:互联网 发布:淘宝双11交易额2016 编辑:程序博客网 时间:2024/05/16 17:08
1) 首先在classpath下寻找自己的配置文件commons-logging.properties,如果找到,则使用其中定义的Log实现类;
2) 如果找不到commons-logging.properties文件,则在查找是否已定义系统环境变量org.apache.commons.logging.Log,找到则使用其定义的Log实现类;
如果在Tomact中可以建立一个叫 :CATALINA_OPTS 的环境变量
给他的值 : - Dorg.apache.commons.logging.Log = org.apache.commons.logging.impl.SimpleLog - Dorg.apache.commons.logging.simplelog.defaultlog = warn
3) 否则,查看classpath中是否有Log4j的包,如果发现,则自动使用Log4j作为日志实现类;
4) 否则,使用JDK自身的日志实现类(JDK1.4以后才有日志实现类);
5) 否则,使用commons-logging自己提供的一个简单的日志实现类SimpleLog;
- import org.apache.commons.logging.Log;
- import org.apache.commons.logging.LogFactory;
- public class TestLog {
- private static Log log = LogFactory.getLog(TestLog.class);
- public void log() {
- log.debug("Debug info.");
- log.info("Info info");
- log.warn("Warn info");
- log.error("Error info");
- log.fatal("Fatal info");
- }
- public static void main(String[] args) {
- System.out.println("log obj = " + log);
- TestLog test = new TestLog();
- test.log();
- }
- }
- import org.apache.commons.logging.Log;
- import org.apache.commons.logging.LogFactory;
- public class TestLog {
- private static Log log = LogFactory.getLog(TestLog.class);
- public void log() {
- log.debug("Debug info.");
- log.info("Info info");
- log.warn("Warn info");
- log.error("Error info");
- log.fatal("Fatal info");
- }
- public static void main(String[] args) {
- System.out.println("log obj = " + log);
- TestLog test = new TestLog();
- test.log();
- }
- }
log obj = org.apache.commons.logging.impl.Jdk14Logger @173a10f
2009-11-19 22:48:48 TestLog log
信息: Info info
2009-11-19 22:48:49 TestLog log
警告: Warn info
2009-11-19 22:48:49 TestLog log
严重: Error info
2009-11-19 22:48:49 TestLog log
严重: Fatal info
INFO [main] - Info info
WARN [main] - Warn info
ERROR [main] - Error info
FATAL [main] - Fatal info
如果把 commons-logging.properties配置成如下:
org.apache.commons.logging.Log=org.apache.commons.logging.impl.SimpleLog
则输出:
log obj = org.apache.commons.logging.impl.SimpleLog @69b332
[INFO] TestLog - Info info
[WARN] TestLog - Warn info
[ERROR] TestLog - Error info
[FATAL] TestLog - Fatal info
为了简化配置,我们可以不使用commons-logging的配置文件,也不设置commons-logging相关的环境变量,只需log4j的包放入classpath就可以了,这样就可以完成commons-logging与Log4j的结合。如果以后不想使用log4j,只需将log4j的包从classpath中移除就可以了。 当然使用时Log4j会根据指定的日志器名(LogFactory.getLog(TestLog.class) )自动搜索classpath下log4j.properties或 log4j.xml配置文件中对应的日志器,如果没有找到,可能会显示以下错误信息:
log4j:WARN No appenders could be found for logger (TestLog).
log4j:WARN Please initialize the log4j system properly.
- Apache Commons-logging+log4j配置
- Apache Commons-logging+log4j配置
- Apache Commons-logging+log4j配置
- Apache Commons-logging+log4j配置
- Apache Commons-logging+log4j配置
- Apache Commons-logging+log4j配置
- Apache Commons-logging+log4j配置
- Log4J + commons-logging 配置
- Log4j配合Apache Commons Logging
- apache commons-logging和log4j
- Mybatis与SLF4J、Apache Commons Logging、Apache Log4J、JDK Logging等日志打印配置
- Spring 配置 Apache Commons Logging
- Apache Commons Logging日志与Log4j
- log4j结合commons-logging配置总结
- commons-logging,log4j配置及使用
- commons logging和log4j 通用配置
- apache commons Logging与Log4j关系和只导入apache commons Logging为何能打印日志
- Commons-logging + Log4j 介绍
- LeetCode—Factorial Trailing Zeroes
- OpenGL绘制凹多变形
- String StringBuffer StringBuilder
- 九度 题目1000:计算a+b
- 2d-x 拷贝机制
- Apache Commons-logging+log4j配置
- 关联、组合、聚合、依赖关系比较
- Java代码标准化
- 判断浏览器类型,区分手机和PC
- Log4j的应用实例
- Listview中图片加载使用Volley框架时,出现图片显示不正确的问题的解析
- LINUX下动态链接库的使用-dlopen dlsym dlclose dlerror
- Centos下使用subversion
- MonoBehaviour的事件和具体功能总结