Commit e5e85809 authored by Benedikt Kleinmeier's avatar Benedikt Kleinmeier

Merge branch 'set_logname'

parents 8948b6fb 8d71a5ec
Pipeline #98663 passed with stages
in 150 minutes and 27 seconds
......@@ -4,8 +4,11 @@ import net.sourceforge.argparse4j.inf.Argument;
import net.sourceforge.argparse4j.inf.ArgumentAction;
import net.sourceforge.argparse4j.inf.ArgumentParser;
import net.sourceforge.argparse4j.inf.ArgumentParserException;
import org.vadere.util.logging.Logger;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.appender.RollingFileAppender;
import org.apache.logging.log4j.core.config.Configuration;
import org.apache.logging.log4j.core.config.LoggerConfig;
import java.util.Map;
......@@ -13,11 +16,25 @@ public class SetLogNameCommand implements ArgumentAction {
@Override
public void run(ArgumentParser parser, Argument arg, Map<String, Object> attrs, String flag, Object value) throws ArgumentParserException {
String filename = (String) value;
// FIXME: Create a "FileAppender" which writes to "filename".
// See http://logging.apache.org/log4j/2.x/manual/customconfig.html and
// https://stackoverflow.com/questions/15441477/how-to-add-log4j2-appenders-at-runtime-programmatically/33472893#33472893
// Logger.setFileName(filename); //todo set Filename of Log-file
final LoggerContext loggerContext = (LoggerContext) LogManager.getContext(false);
final Configuration config = loggerContext.getConfiguration();
final LoggerConfig logConfig = config.getLoggerConfig("STDOUTERR");
final LoggerConfig rootConfig = config.getRootLogger();
rootConfig.removeAppender("FILE");
logConfig.removeAppender("FILE");
RollingFileAppender oldAppender = (RollingFileAppender) config.getAppender("FILE");
RollingFileAppender newAppender = RollingFileAppender.newBuilder()
.withName("FILE")
.withFileName(filename)
.withFilePattern(filename + "-%d{MM-dd-yy-HH-mm-ss}-%i.log.gz")
.withLayout(oldAppender.getLayout())
.withPolicy(oldAppender.getTriggeringPolicy())
.build();
newAppender.start();
config.addAppender(newAppender);
logConfig.addAppender(newAppender, null, null);
rootConfig.addAppender(newAppender, null, null);
loggerContext.updateLoggers();
}
@Override
......
package org.vadere.simulator.entrypoints.cmd.commands;
import org.junit.Test;
import org.vadere.simulator.entrypoints.cmd.VadereConsole;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import static org.junit.Assert.*;
public class SetLogNameCommandTest {
@Test
public void testSetLogNameCommand(){
String path = "test.log";
VadereConsole.main(new String[] {"--logname", path, "scenario-run", "-f", "../VadereModelTests/TestOSM/scenarios/basic_2_density_discrete_ca.scenario"});
try {
assertTrue(Files.lines(Paths.get(path)).count() > 0);
} catch (IOException e) {
fail(e.getMessage());
}
}
}
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