Test
이라는 클래스가 있으며 Thread
에서 확장됩니다. 각 테스트 스레드마다 내 자신의 로그 파일이 작성되도록 log4j2를 구성하는 방법. 스레드에 대한 log4j2 구성
public class Test extends Thread {
private String name;
private Logger LOGGER;
public Test(String name) {
this.name=name;
}
@Override
public void run() {
System.setProperty("taskLogFolder", Global.getInstance().getLogRoot().toString());
System.setProperty("taskLogName", name);
LoggerContext loggerContext = Configurator.initialize(name, new File(Global.getInstance().getAppRoot()+WEB_INF_DIR+"taskLog4j2.xml").toString());
LOGGER = loggerContext.getLogger(name);
for(int i=0;i<100;i++) {
LOGGER.info(LOGGER.getName()+" "+i);
}
//LOGGER.info(LOGGER.getName());
}
}
그러나 시작시 하나 개의 파일이 출력됩니다.
new Test("task-8").start();
new Test("task-2").start();
new Test("task-3").start();
new Test("task-4").start();
new Test("task-5").start();
new Test("task-6").start();
new Test("task-7").start();
이전에 log4j를 사용 했었지만 그 방법은 잘 모르겠습니다.
내가 뭘 잘못하고 있니?
taskLog4j2.xml
파일 :
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="info" monitorInterval = "30">
<Properties>
<Property name="pattern">%d{yyyy-MM-dd HH:mm:ss.SSS} [%level] [thread-id %T] %l - %msg%n</Property>
</Properties>
<Appenders>
<RollingFile name="fileLogger" fileName="${sys:taskLogFolder}/${sys:taskLogName}.log" filePattern="${sys:taskLogFolder}/${sys:taskLogName}-%d{yyyy-MM-dd}.log">
<PatternLayout>
<pattern>${pattern}</pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true" />
</Policies>
</RollingFile>
<Console name="console" target="SYSTEM_OUT">
<PatternLayout pattern="${pattern}" />
</Console>
</Appenders>
<Loggers>
<Root level="info" additivity="false">
<appender-ref ref="console" />
<appender-ref ref="fileLogger" />
</Root>
<Logger name="com.test.*" level="info">
<appender-ref ref="fileLogger" level="info" />
</Logger>
</Loggers>
</Configuration>
@Sudheera 응답
2017-12-12 22:22:46,942 task-3 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
at org.apache.logging.log4j.core.config.plugins.util.PluginBuilder.findFactoryMethod(PluginBuilder.java:229)
at org.apache.logging.log4j.core.config.plugins.util.PluginBuilder.build(PluginBuilder.java:134)
at org.apache.logging.log4j.core.config.AbstractConfiguration.createPluginObject(AbstractConfiguration.java:958)
at org.apache.logging.log4j.core.config.AbstractConfiguration.createConfiguration(AbstractConfiguration.java:898)
at org.apache.logging.log4j.core.appender.routing.RoutingAppender.createAppender(RoutingAppender.java:271)
at org.apache.logging.log4j.core.appender.routing.RoutingAppender.getControl(RoutingAppender.java:255)
at org.apache.logging.log4j.core.appender.routing.RoutingAppender.append(RoutingAppender.java:225)
at org.apache.logging.log4j.core.config.AppenderControl.tryCallAppender(AppenderControl.java:156)
at org.apache.logging.log4j.core.config.AppenderControl.callAppender0(AppenderControl.java:129)
at org.apache.logging.log4j.core.config.AppenderControl.callAppenderPreventRecursion(AppenderControl.java:120)
at org.apache.logging.log4j.core.config.AppenderControl.callAppender(AppenderControl.java:84)
at org.apache.logging.log4j.core.config.LoggerConfig.callAppenders(LoggerConfig.java:448)
at org.apache.logging.log4j.core.config.LoggerConfig.processLogEvent(LoggerConfig.java:433)
at org.apache.logging.log4j.core.config.LoggerConfig.log(LoggerConfig.java:417)
at org.apache.logging.log4j.core.config.LoggerConfig.log(LoggerConfig.java:403)
at org.apache.logging.log4j.core.config.AwaitCompletionReliabilityStrategy.log(AwaitCompletionReliabilityStrategy.java:63)
at org.apache.logging.log4j.core.Logger.logMessage(Logger.java:146)
at org.apache.logging.log4j.spi.AbstractLogger.tryLogMessage(AbstractLogger.java:2116)
at org.apache.logging.log4j.spi.AbstractLogger.logMessageSafely(AbstractLogger.java:2100)
at org.apache.logging.log4j.spi.AbstractLogger.logMessage(AbstractLogger.java:1994)
at org.apache.logging.log4j.spi.AbstractLogger.logIfEnabled(AbstractLogger.java:1966)
at org.apache.logging.log4j.spi.AbstractLogger.info(AbstractLogger.java:1303)
at com.test.Test.run(Test.java:20)
[다른 로그 파일에 여러 스레드를 기록하는 방법] (https://stackoverflow.com/questions/8355847/how-to-log-multiple-threads-in-different-log-files) – clinomaniac
@clinomaniac 그것은 log4j 1.에 해당합니다. Log4j2를 사용합니다. – Tsyklop