如何配置Spring和SLF4J以便我可以获取日志记录? [英] How do I configure Spring and SLF4J so that I can get logging?

查看:372
本文介绍了如何配置Spring和SLF4J以便我可以获取日志记录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个maven&我想要登录的Spring应用程序。我很想使用SLF4J。

I've got a maven & spring app that I want logging in. I'm keen to use SLF4J.

我想将所有配置文件放入目录{classpath} / config,包括log4j。 xml然后使用spring bean初始化。

I want to put all my config files into a directory {classpath}/config including log4j.xml and then init using a spring bean.

例如

<bean id="log4jInitialization" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
    <property name="targetClass" value="org.springframework.util.Log4jConfigurer"/>
    <property name="targetMethod" value="initLogging"/>
    <property name="arguments">
        <list>
            <value>classpath:config/log4j.xml</value>
        </list>
    </property>
</bean>

但是我收到此警告并且没有记录。

However I get this warning and no logging.


log4j:WARN找不到logger(org.springframework.context.support.ClassPathXmlApplicationContext)的appender。
log4j:WARN请正确初始化log4j系统。
log4j:警告请参阅 http://logging.apache.org/log4j /1.2/faq.html#noconfig 了解更多信息。

我已经google了,找不到了设置它的简单示例。有什么想法吗?

I've googled around and can't find a simple example on setting this up. Any ideas?

推荐答案

除了Jatin的回答:

In addition to Jatin's answer:

Spring使用Jakarta Commons Logging作为日志记录API。要记录到slf4j,您需要确保 commons-logging 不在类路径上。 jcl-over-slf4j 是公共记录的替代jar。

Spring uses Jakarta Commons Logging as a logging API. In order to log to slf4j, you need to make sure commons-logging is not on the classpath. jcl-over-slf4j is a replacement jar for commons-logging.

如果你正在使用maven,你可以使用 mvn依赖:tree 检测公共日志记录的来源,并使用依赖项排除将其从所有需要它的依赖项中排除。您可能需要多次运行 mvn dependency:tree ,因为它只显示传递依赖项的第一次出现。

If you're using maven, you can detect where commons-logging comes from using mvn dependency:tree and exclude it from all dependencies that require it using dependency exclusions. You might need to run mvn dependency:tree several times though, because it only shows the first occurence of a transitive dependency.

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-context</artifactId>
  <version>${org.springframework.version}</version>
  <exclusions>
    <exclusion>
      <artifactId>commons-logging</artifactId>
      <groupId>commons-logging</groupId>
    </exclusion>
  </exclusions>
</dependency>

这篇关于如何配置Spring和SLF4J以便我可以获取日志记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆