1 . Commons-Loggin 简介
2 .快速入门
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;
3 .使用JCL开发
#commons-logging.properties 文件配置信息
# org.apache.commons.logging.Log=org.apache.commons.logging.impl.SimpleLog
# Must be one of ("trace", "debug", "info", "warn", "error", or "fatal").
#
log4j
org.apache.commons.logging.Log= org.apache.commons.logging.impl.Log4JLogger
#JDK5 Logger
#org.apache.commons.logging.Log=org.apache.commons.logging.impl.Jdk14Logger
|
log.fatal(Object message);
log.fatal(Object message, Throwable t);
log.error(Object message);
log.error(Object message, Throwable t);
log.warn(Object message);
log.warn(Object message, Throwable t);
log.info(Object message);
log.info(Object message, Throwable t);
log.debug(Object message);
log.debug(Object message, Throwable t);
log.trace(Object message);
log.trace(Object message, Throwable t);
|
log.isFatalEnabled();
log.isErrorEnabled();
log.isWarnEnabled();
log.isInfoEnabled();
log.isDebugEnabled();
log.isTraceEnabled();
|
3.4 .信息级别
4 . Apache Commons-logging 使用流程
private static Log log = LogFactory .getLog (Test. class );
|
log .debug( "Debug info." );
|
5.Apache Commons-logging 使用示例
- 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
6.Apache Commons-logging + log4j 使用
org.apache.commons.logging.Log=org.apache.commons.logging.impl.Log4JLogger
|
log4j.rootLogger=info, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
# Pattern to output the caller's file name and line number.
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
|
结果:
log obj = org.apache.commons.logging.impl.Log4JLogger @1ffb8dc 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.