gpt4 book ai didi

java - log4j 用 spring 创建空文件

转载 作者:行者123 更新时间:2023-12-01 13:13:44 25 4
gpt4 key购买 nike

我想将 log4j 1.2.9 与 spring 4.0.1 和 JBOSS 7.1 一起使用,但每次我在控制台上获取日志信息,但文件已创建且为空......

log4j.properties

log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=C:\\log\\loggingFile.log
log4j.appender.file.MaxFileSize=1MB
log4j.appender.file.MaxBackupIndex=1
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n


log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

# Root logger option
log4j.rootLogger=INFO, file, stdout

web.xml

<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/log4j.properties</param-value>
</context-param>

<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>

简单测试 Controller

import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;
@ManagedBean(name="MyController")
public class MyController {
static final Logger logger = Logger.getLogger(MyController.class);
public String test() {
BasicConfigurator.configure();
logger.debug(LoginController.class);
logger.info("Info.. ");
logger.error("Error..");
logger.fatal("Fatal");
return null;
}}

最佳答案

解决方案是在/WEB-INF中添加jboss-deployment-struct.xml以排除org.apache.log4j

<jboss-deployment-structure>
<deployment>
dependencies -->
<exclusions>
<module name="org.apache.log4j" />
</exclusions>
</deployment>
</jboss-deployment-structure>

关于java - log4j 用 spring 创建空文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22629881/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com