Commit c04905cd authored by Stephan Korsholm's avatar Stephan Korsholm
Browse files

#176: Fix issue with empty log files

parent b23a16b7
No related merge requests found
Showing with 36 additions and 14 deletions
+36 -14
......@@ -6,6 +6,7 @@ import java.util.TimeZone;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.Appender;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.appender.FileAppender;
import org.apache.logging.log4j.core.config.Configuration;
......@@ -68,17 +69,41 @@ public class Simulation {
+ "." + cal.get(Calendar.MINUTE) + ".log");
LoggerContext lc = (LoggerContext) LogManager.getContext(false);
FileAppender fa =
FileAppender.newBuilder()
.withName("DEBUG")
.withAppend(true)
.withFileName(logfile.toString())
.withLayout(PatternLayout.newBuilder().withPattern("%d{HH:mm:ss.SSS} %F %5p: %msg%n").build())
.setConfiguration(lc.getConfiguration())
.build();
fa.start();
lc.getConfiguration().addAppender(fa);
lc.getRootLogger().addAppender(lc.getConfiguration().getAppender(fa.getName()));
FileAppender fa = (FileAppender) lc.getConfiguration().getAppender("DEBUG");
if (fa != null) {
// Stop the old appender
fa.stop();
// Reconfigure the appender with the new file path (and possibly other parameters if needed)
FileAppender newAppender = FileAppender.newBuilder()
.setConfiguration(lc.getConfiguration())
.withName("DEBUG")
.withAppend(true)
.withFileName(logfile.toString())
.withLayout(fa.getLayout())
.build();
// Start the new appender
newAppender.start();
// Replace old appender with the new one in the configuration and logger
lc.getConfiguration().getAppenders().put("DEBUG", newAppender);
lc.getRootLogger().removeAppender(fa);
lc.getRootLogger().addAppender(newAppender);
} else {
fa = FileAppender.newBuilder()
.withName("DEBUG")
.withAppend(true)
.withFileName(logfile.toString())
.withLayout(PatternLayout.newBuilder().withPattern("%d{HH:mm:ss.SSS} %F %5p: %msg%n").build())
.setConfiguration(lc.getConfiguration())
.build();
fa.start();
lc.getConfiguration().addAppender(fa);
lc.getRootLogger().addAppender(lc.getConfiguration().getAppender(fa.getName()));
}
Configuration config = lc.getConfiguration();
LoggerConfig loggerConfig = config.getRootLogger();
loggerConfig.setLevel(Level.DEBUG);
......@@ -93,9 +118,6 @@ public class Simulation {
public void removeDebugLog() {
LOG.debug("Removing debug logger");
LoggerContext lc = (LoggerContext) LogManager.getContext(false);
FileAppender debug = lc.getConfiguration().getAppender("DEBUG");
debug.stop();
lc.getRootLogger().removeAppender(debug);
Configuration config = lc.getConfiguration();
LoggerConfig loggerConfig = config.getRootLogger();
loggerConfig.setLevel(Level.INFO);
......
Supports Markdown
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