Commit 7ad5425c authored by Stefan Schuhbaeck's avatar Stefan Schuhbaeck
Browse files

add ScenarioAppender to create a separate log file for each ScenarioRun

parent ddcce4b3
......@@ -13,6 +13,7 @@ import java.util.Random;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.apache.log4j.MDC;
import org.jetbrains.annotations.Nullable;
import org.vadere.simulator.control.PassiveCallback;
import org.vadere.simulator.control.Simulation;
......@@ -69,6 +70,9 @@ public class ScenarioRun implements Runnable {
@Override
public void run() {
try {
//add Scenario Name to Log4j Mapped Diagnostic Context to filter log by ScenarioRun
MDC.put("scenario.Name", outputPath.getFileName().toString());
/**
* To make sure that no other Thread changes the scenarioStore object during the initialization of a scenario run
* this is an atomic operation with respect to the scenarioStore. We observed that with Linux 18.04 KUbunto
......@@ -106,6 +110,8 @@ public class ScenarioRun implements Runnable {
throw new RuntimeException("Simulation failed.", e);
} finally {
doAfterSimulation();
//remove Log4j Mapped Diagnostic Context after ScenarioRun
MDC.remove("scenario.Name");
}
}
......@@ -138,6 +144,10 @@ public class ScenarioRun implements Runnable {
}
}
public Path getOutputPath() {
return Paths.get(this.outputPath.toString());
}
public void pause() {
if (simulation != null) { // TODO throw an illegal state exception if simulation is not running
simulation.pause();
......
package org.vadere.simulator.projects.migration;
import org.apache.log4j.AppenderSkeleton;
import org.apache.log4j.PatternLayout;
import org.apache.log4j.RollingFileAppender;
import org.apache.log4j.spi.LoggingEvent;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
public class ScenarioAppender extends AppenderSkeleton {
private final Path basePath;
private HashMap<String, RollingFileAppender> appenders;
public ScenarioAppender(Path basePath) {
this.basePath = basePath;
this.appenders = new HashMap<>();
}
private RollingFileAppender createAppender(String name) {
Path loggingBasePath = basePath.resolve(name);
if (!loggingBasePath.toFile().exists()) {
try {
Files.createDirectories(loggingBasePath);
} catch (IOException e) {
e.printStackTrace();
}
}
String filePath = Paths.get(loggingBasePath.toString(), "log.out").toString();
RollingFileAppender appender = null;
try {
appender = new RollingFileAppender(new PatternLayout("%d{ABSOLUTE} %5p %c{1}:%L - %m%n"), filePath);
} catch (IOException e) {
e.printStackTrace();
}
appender.setName(name);
return appender;
}
@Override
protected void append(LoggingEvent event) {
Object obj = event.getMDC("scenario.Name");
if (obj != null) {
String appenderName = (String) obj;
if (appenders.containsKey(appenderName)) {
appenders.get(appenderName).doAppend(event);
} else {
RollingFileAppender appender = createAppender(appenderName);
appenders.put(appenderName, appender);
appender.doAppend(event);
}
}
}
@Override
public boolean requiresLayout() {
return false;
}
@Override
public void close() {
appenders.forEach((k, v) -> v.close());
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment