LogFactory.getLog()和Logger.getLogger()的区别

本文探讨了log4j和commons-logging两种日志框架的整合应用,解析了它们之间的区别及如何实现灵活的日志记录。文章介绍了如何配置和使用这两种框架,并详细解释了不同日志级别的含义。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Logger.getLogger
LogFactory.getLog

Logger来自log4j自己的包。如果用Logger.getLogger,需要一个log4j的jar包,用此方式你只能依靠log4j:
LogFactory来自common-logging包。如果用LogFactory.getLog,你可以用任何实现了通用日志接口的日志记录器替换log4j,而程序不受影响。apache的common-logging包是通用日志接口,通过这个中间层,你可以随便指定到底用哪个日志系统。增加系统的灵活性。若log4j它不存在, commons-logging 会另行选择其它的日志实现类。 这样保证了程序中不一定要使用log4j这个日志文件了
增强灵活性的理由:
  1)首先在 classpath 下寻找自己的配置文件 commons-logging.properties ,如果找到,则使用其中定义的 Log 实现类;

2) 如果找不到 commons-logging.properties 文件,则在查找是否已定义系统环境变量  org.apache.commons.logging.Log ,找到则使用其定义的 Log 实现类;

3)  否则,查看 classpath 中是否有 Log4j 的包,如果发现,则自动使用 Log4j 作为日志实现类;

4)   否则,使用 JDK 自身的日志实现类( JDK1.4 以后才有日志实现类);

5)   否则,使用 commons-logging 自己提供的一个简单的日志实现类 SimpleLog ;


为了简化配置 commons-logging ,一般不使用 commons-logging 的配置文件,也不设置与 commons-logging 相关的系统环境变量,而只需将 Log4j 的 Jar 包放置到 classpash 中就可以了。这样就很简单地完成了 commons-logging 与 Log4j 的融合。

根据不同的性质,日志信息通常被分成不同的级别,从低到高依次是:“调试( DEBUG )”“信息( INFO )”“警告( WARN )”“错误(ERROR )”“致命错误( FATAL )”。


基于common-logging的运行方式:
package org;
import org.apache.commons.logging.Log;
import org.apache.log4j.Logger;
public class Test extends TagSupport{
public static Logger log=Logger.getLogger(Test.class);
public static void test()
{
log.debug("111");
log.info("125");
log.warn("485");
log.error("error");

}
public static void main(String[] a)
{
Test.test();
}
}

基于log4j的运行方式
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
public class TestLog4j {

static Logger logger = Logger.getLogger(TestLog4j.class);
   public static void main(String args[]) {
   PropertyConfigurator.configure("log4j.properties");
   logger.debug("Here is some DEBUG");
   logger.info("Here is some INFO");
      logger.warn("Here is some WARN");
      logger.error("Here is some ERROR");
      logger.fatal("Here is some FATAL");
   }
}

-----------
commons-logging 仅仅对 Log4j( 当然还包括其它 LOG 实现 ) 作了一层包装,具体的日志输出还是在内部转交给身后的 Log4j 来处理,还有log4j会默认的去classes目录下去寻找log4j.properties 文件
Exception in thread "main" java.lang.NoClassDefFoundError: Could not initialize class org.apache.logging.log4j.core.LoggerContext at org.apache.logging.log4j.core.selector.ClassLoaderContextSelector.createContext(ClassLoaderContextSelector.java:254) at org.apache.logging.log4j.core.selector.ClassLoaderContextSelector.locateContext(ClassLoaderContextSelector.java:218) at org.apache.logging.log4j.core.selector.ClassLoaderContextSelector.getContext(ClassLoaderContextSelector.java:136) at org.apache.logging.log4j.core.selector.ClassLoaderContextSelector.getContext(ClassLoaderContextSelector.java:123) at org.apache.logging.log4j.core.selector.ClassLoaderContextSelector.getContext(ClassLoaderContextSelector.java:117) at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:150) at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:47) at org.apache.logging.log4j.LogManager.getContext(LogManager.java:194) at org.apache.commons.logging.LogAdapter$Log4jLog.<clinit>(LogAdapter.java:155) at org.apache.commons.logging.LogAdapter$Log4jAdapter.createLog(LogAdapter.java:122) at org.apache.commons.logging.LogAdapter.createLog(LogAdapter.java:89) at org.apache.commons.logging.LogFactory.getLog(LogFactory.java:67) at org.apache.commons.logging.LogFactory.getLog(LogFactory.java:59) at org.springframework.boot.SpringApplication.<clinit>(SpringApplication.java:196) at com.zpkj.eps.outernet.OuternetApplication.main(OuternetApplication.java:21) Caused by: java.lang.ExceptionInInitializerError: Exception java.lang.NoSuchFieldError: EMPTY_BYTE_ARRAY [in thread "main"] at org.apache.logging.log4j.core.config.ConfigurationSource.<clinit>(ConfigurationSource.java:59) at org.apache.logging.log4j.core.config.NullConfiguration.<init>(NullConfiguration.java:32) at org.apache.logging.log4j.core.LoggerContext.<clinit>(LoggerContext.java:74) at org.apache.logging.log4j.core.selector.ClassLoaderContextSelector.createContext(ClassLoaderContextSelector.java:254) at org.apache.logging.log4j.core.selector.ClassLoaderContextSelector.locateContext(ClassLoaderContextSelector.java:218) at org.apache.logging.log4j.core.selector.ClassLoaderContextSelector.getContext(ClassLoaderContextSelector.java:136) at org.apache.logging.log4j.core.selector.ClassLoaderContextSelector.getContext(ClassLoaderContextSelector.java:123) at org.apache.logging.log4j.core.selector.ClassLoaderContextSelector.getContext(ClassLoaderContextSelector.java:117) at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:150) at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:47) at org.apache.logging.log4j.LogManager.getContext(LogManager.java:194) at org.apache.logging.log4j.spi.AbstractLoggerAdapter.getContext(AbstractLoggerAdapter.java:138) at org.apache.logging.slf4j.Log4jLoggerFactory.getContext(Log4jLoggerFactory.java:45) at org.apache.logging.log4j.spi.AbstractLoggerAdapter.getLogger(AbstractLoggerAdapter.java:48) at org.apache.logging.slf4j.Log4jLoggerFactory.getLogger(Log4jLoggerFactory.java:30) at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:363) at reactor.util.Loggers$Slf4JLoggerFactory.getLogger(Loggers.java:205) at reactor.util.Loggers.useSl4jLoggers(Loggers.java:166) at reactor.util.Loggers.resetLoggerFactory(Loggers.java:69) at reactor.util.Loggers.<clinit>(Loggers.java:53) at reactor.core.publisher.Hooks.<clinit>(Hooks.java:550) at java.lang.Class.forName0(Native Method) at java.lang.Class.forName(Class.java:348) at com.intellij.rt.debugger.agent.DebuggerAgent.premain(DebuggerAgent.java:14) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at sun.instrument.InstrumentationImpl.loadClassAndStartAgent(InstrumentationImpl.java:386) at sun.instrument.InstrumentationImpl.loadClassAndCallPremain(InstrumentationImpl.java:401)
最新发布
05-14
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值