Java 如何配置 Spring 和 SLF4J 以便获取日志记录?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3387441/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I configure Spring and SLF4J so that I can get logging?
提问by Peter Wilkinson
I've got a maven & spring app that I want logging in. I'm keen to use SLF4J.
我有一个想要登录的 maven & spring 应用程序。我很想使用 SLF4J。
I want to put all my config files into a directory {classpath}/config including log4j.xml and then init using a spring bean.
我想将我所有的配置文件放入一个目录 {classpath}/config 中,包括 log4j.xml,然后使用 spring bean 进行初始化。
e.g.
例如
<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 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#noconfigfor more info.
log4j:WARN 找不到记录器的附加程序(org.springframework.context.support.ClassPathXmlApplicationContext)。log4j:WARN 请正确初始化 log4j 系统。log4j:WARN 有关更多信息,请参阅http://logging.apache.org/log4j/1.2/faq.html#noconfig。
I've googled around and can't find a simple example on setting this up. Any ideas?
我已经用谷歌搜索了,找不到一个简单的例子来设置它。有任何想法吗?
采纳答案by Stijn Van Bael
In addition to Jatin's answer:
除了 Jatin 的回答:
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.
Spring 使用 Jakarta Commons Logging 作为日志 API。为了登录到 slf4j,您需要确保commons-logging
不在类路径上。jcl-over-slf4j
是 commons-logging 的替代 jar。
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.
如果您使用的是 maven,则可以检测 commons-logging 来自何处mvn dependency:tree
,并使用依赖项排除将其从需要它的所有依赖项中排除。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>
回答by Jatin
You'll find an example at https://github.com/mbogoevici/spring-samples/tree/master/mvc-basic/trunk. You need to include some dependencies in your POM file to enable logging.
您可以在https://github.com/mbogoevici/spring-samples/tree/master/mvc-basic/trunk找到一个示例。您需要在 POM 文件中包含一些依赖项以启用日志记录。
<!-- Logging -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${org.slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>${org.slf4j.version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${org.slf4j.version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
<scope>runtime</scope>
</dependency>
回答by TaherT
keep log4j file in default package
将 log4j 文件保存在默认包中
回答by Chandra Sekhar
Use blow configuration for implementation of the JCL API
on the classpath
:
使用的吹瓶配置执行JCL API
的classpath
:
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.0.0.RELEASE</version>
<scope>runtime</scope>
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>1.5.8</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.5.8</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.5.8</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.14</version>
<scope>runtime</scope>
</dependency>
</dependencies>
for More information check here
欲了解更多信息,请点击此处
回答by Boris Treukhov
Just for the sake of completeness, a logback-classic
variant:
只是为了完整起见,一个logback-classic
变体:
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>1.6.6</version>
<scope>runtime</scope>
</dependency>
Do not forget however to disable commons-logging
dependencywhich sprouts from Spring dependency like in the accepted (Stijn's) answer.
但是不要忘记禁用commons-logging
从 Spring依赖中萌芽的依赖,就像在接受的(Stijn 的)答案中一样。
回答by Jianyu
I like the logback way, and for slf4j, we do the similar config:
我喜欢 logback 方式,对于 slf4j,我们做了类似的配置:
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
</dependency>
slf4j-log4j12 will automatically introduce slf4j-api and log4j, so don't need to put so many dependencies
slf4j-log4j12会自动引入slf4j-api和log4j,所以不需要放那么多依赖
回答by Nashvi
Just add lazy-init="false"
to eagerly load the bean for log4j configuration in your root context. That should solve the WARN message log4j:WARN No appenders could be found for logger
只需添加lazy-init="false"
即可在您的根上下文中热切加载用于 log4j 配置的 bean。那应该可以解决 WARN 消息log4j:WARN No appenders could be found for logger
Example:
例子:
<bean id="log4jInitialization" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean" lazy-init="false">
A more better approach would be to have the configuration in web.xml or as a JVM parameter (-Dlog4j.configuration=.../conf/log4j.xml
or with 'file:' prefix as -Dlog4j.configuration=file:conf/log4j.properties
for some cases)
更好的方法是在 web.xml 中进行配置或作为 JVM 参数(-Dlog4j.configuration=.../conf/log4j.xml
或-Dlog4j.configuration=file:conf/log4j.properties
在某些情况下使用 'file:' 前缀)