我有一个要登录的 maven 和 spring 应用程序。我热衷于使用 SLF4J。
我想将我所有的配置文件放到一个目录 {classpath}/config 中,包括 log4j.xml,然后使用 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>
但是我得到了这个警告并且没有记录。
log4j:WARN No appenders could be found for logger (org.springframework.context.support.ClassPathXmlApplicationContext). log4j:WARN Please initialize the log4j system properly. log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
我在 Google 上四处搜索,但找不到一个简单的设置示例。有什么想法吗?
最佳答案
除了Jatin的回答:
Spring 使用 Jakarta Commons Logging 作为日志记录 API。为了登录到 slf4j,您需要确保 commons-logging
不在类路径中。 jcl-over-slf4j
是 commons-logging 的替代 jar。
如果您使用 maven,您可以使用 mvn dependency:tree
检测 commons-logging 的来源,并使用依赖排除将其从所有需要它的依赖项中排除。您可能需要多次运行 mvn dependency:tree
,因为它只显示传递依赖项的第一次出现。
<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>
关于java - 如何配置 Spring 和 SLF4J 以便获得日志记录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3387441/