I am using spring boot(2.2.0.RELEASE) configure with log4j2. While deploy war in tomcat getting some run time exception.I have set system property(logging name) then that name mentioned in log4j properties.
Runtime Error :
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>
Main method : In that below main method i have getting context name using class loader then i put the name using system properties.
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);
}
}