gpt4 book ai didi

java - 如何修复无法调用元素 RollingFile 类 org.apache.logging.log4j.core.appender.RollingFileAppender 中的工厂方法?

转载 作者:行者123 更新时间:2023-12-01 18:07:53 24 4
gpt4 key购买 nike

我正在使用 spring boot(2.2.0.RELEASE) 配置 log4j2。在 tomcat 中部署 war 时出现一些运行时异常。我已设置系统属性(日志记录名称),然后在 log4j 属性中提到该名称。

运行时错误:

ERROR Unable to invoke factory method in class org.apache.logging.log4j.core.appender.RollingFileAppender for element RollingFile: java.lang.IllegalStateException: No factory method found for class org.apache.logging.log4j.core.appender.RollingFileAppender java.lang.IllegalStateException: No factory method found for class org.apache.logging.log4j.core.appender.RollingFileAppender

Log4j.xml:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO">
<Properties>
<Property name="LOG_PATTERN">%d{yyyy-MM-dd'@'HH:mm:ss}|[%t]|[AM]|%p|%X{Slf4jMDCFilter.UUID}|%c{1}()
%M(%L) %m%n </Property>
<Property name="APP_LOG_ROOT"
value="logs/${sys:finlog}">
</Property>
</Properties>
<Appenders>
<RollingFile name="errorlog"
fileName="${APP_LOG_ROOT}/error.log"
filePattern="${APP_LOG_ROOT}/ErrorLog-%d{yyyy-MM-dd}-%i.log">
<LevelRangeFilter minLevel="ERROR" maxLevel="ERROR"
onMatch="ACCEPT" onMismatch="DENY"/>
<PatternLayout pattern="${LOG_PATTERN}"/>
<Policies>
<SizeBasedTriggeringPolicy size="19500KB"/>
</Policies>
</RollingFile>
<RollingFile name="debugLog"
fileName="${APP_LOG_ROOT}/debug.log"
filePattern="${APP_LOG_ROOT}/DebugLog-%d{yyyy-MM-dd}-%i.log">
<LevelRangeFilter minLevel="DEBUG" maxLevel="DEBUG"
onMatch="ACCEPT" onMismatch="DENY"/>
<PatternLayout pattern="${LOG_PATTERN}"/>
<Policies>
<SizeBasedTriggeringPolicy size="19500KB"/>
</Policies>
</RollingFile>

<RollingFile name="infoLog"
fileName="${APP_LOG_ROOT}/info.log"
filePattern="${APP_LOG_ROOT}/InfoLog-%d{yyyy-MM-dd}-%i.log">
<LevelRangeFilter minLevel="INFO" maxLevel="INFO"
onMatch="ACCEPT" onMismatch="DENY"/>
<PatternLayout pattern="${LOG_PATTERN}"/>
<Policies>
<SizeBasedTriggeringPolicy size="19500KB"/>
</Policies>
</RollingFile>

<RollingFile name="warnLog"
fileName="${APP_LOG_ROOT}/warn.log"
filePattern="${APP_LOG_ROOT}/WarnLog-%d{yyyy-MM-dd}-%i.log">
<LevelRangeFilter minLevel="WARN" maxLevel="WARN"
onMatch="ACCEPT" onMismatch="DENY"/>
<PatternLayout pattern="${LOG_PATTERN}"/>
<Policies>
<SizeBasedTriggeringPolicy size="19500KB"/>
</Policies>
<DefaultRolloverStrategy max="10"/>
</RollingFile>
</Appenders>
<Loggers>
<Logger name="com.demo.engine" additivity="false"
level="debug">
<AppenderRef ref="errorlog"/>
<AppenderRef ref="debugLog"/>
<AppenderRef ref="infoLog"/>
<AppenderRef ref="warnLog"/>
</Logger>
<Logger name="org.springframework" additivity="false"
level="error">
<AppenderRef ref="springLog"/>
<AppenderRef ref="console"/>
</Logger>
<Root level="DEBUG">
</Root>
</Loggers>
</Configuration>

主要方法:在下面的主要方法中,我使用类加载器获取上下文名称,然后使用系统属性放置该名称。

public class Main extends SpringBootServletInitializer {
static {
String currentpath = "";
String logAppContext = "";
if (Thread.currentThread().getContextClassLoader().getResource("") != null) {
currentpath = Thread.currentThread().getContextClassLoader().getResource("").toString().replace("%23", "#")
.replace("file:/", "");
logAppContext = currentpath.isEmpty() ? logAppContext
: Paths.get(currentpath).getParent().getParent().getFileName().toString();
System.setProperty("finlog", logAppContext);
System.setProperty("context", logAppContext);
}
}

public static void main(String[] args) {
SpringApplication.run(Main.class, args);
}

@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Main.class);
}
}

最佳答案

就我而言,这是记录的文件路径的权限问题。更改权限为我解决了这个问题

关于java - 如何修复无法调用元素 RollingFile 类 org.apache.logging.log4j.core.appender.RollingFileAppender 中的工厂方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60527039/

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