Commit 8425cf7b authored by Stefan Schuhbaeck's avatar Stefan Schuhbaeck
Browse files

replace log4j with log4j2

parent 3f68e2c3
log4j.rootCategory=TRACE, file, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=./log.out
log4j.appender.file.Append=false
log4j.appender.file.MaxFileSize=10000KB
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%-5p [%t] %c{1}: %m%n
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
\ No newline at end of file
name=PropertiesConfig
property.filename = logs
appenders = console, file
appender.console.type = Console
appender.console.name = STDOUT
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = %d{ABSOLUTE} %5p %c{1}:%L - %m%n
appender.file.type = RollingFile
appender.file.name = FILE
appender.file.fileName = log.out
appender.file.filePattern = out-%d{MM-dd-yy-HH-mm-ss}-%i.log.gz
appender.file.layout.type = PatternLayout
appender.file.layout.pattern = %-5p [%t]: %m%n
appender.file.policies.type = Policies
appender.file.policies.size.type = SizeBasedTriggeringPolicy
appender.file.policies.size.size = 10000KB
rootLogger.level = trace
rootLogger.appenderRefs = console, file
rootLogger.appenderRef.console.ref = STDOUT
rootLogger.appenderRef.file.ref = FILE
\ No newline at end of file
......@@ -52,7 +52,7 @@ public class DelaunayHierarchy<P extends IPoint, V extends IVertex<P>, E extends
private static Logger log = Logger.getLogger(DelaunayHierarchy.class);
static {
log.setLevel(LogLevel.DEBUG);
log.setDebug();
}
/**
......
......@@ -81,7 +81,7 @@ public class IncrementalTriangulation<P extends IPoint, V extends IVertex<P>, E
private static Logger log = Logger.getLogger(IncrementalTriangulation.class);
static {
ITriConnectivity.log.setLevel(LogLevel.INFO);
ITriConnectivity.log.setInfo();
}
/**
......
log4j.rootCategory=TRACE, file, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=./log.out
log4j.appender.file.Append=false
log4j.appender.file.MaxFileSize=10000KB
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%-5p [%t]: %m%n
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
\ No newline at end of file
name=PropertiesConfig
property.filename = logs
appenders = console, file
appender.console.type = Console
appender.console.name = STDOUT
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = %d{ABSOLUTE} %5p %c{1}:%L - %m%n
appender.file.type = RollingFile
appender.file.name = FILE
appender.file.fileName = log.out
appender.file.filePattern = out-%d{MM-dd-yy-HH-mm-ss}-%i.log.gz
appender.file.layout.type = PatternLayout
appender.file.layout.pattern = %-5p [%t]: %m%n
appender.file.policies.type = Policies
appender.file.policies.size.type = SizeBasedTriggeringPolicy
appender.file.policies.size.size = 10000KB
rootLogger.level = trace
rootLogger.appenderRefs = console, file
rootLogger.appenderRef.console.ref = STDOUT
rootLogger.appenderRef.file.ref = FILE
\ No newline at end of file
......@@ -5,7 +5,6 @@ import net.sourceforge.argparse4j.inf.ArgumentAction;
import net.sourceforge.argparse4j.inf.ArgumentParser;
import net.sourceforge.argparse4j.inf.ArgumentParserException;
import org.vadere.util.logging.LogLevel;
import org.vadere.util.logging.Logger;
import java.util.Map;
......@@ -13,8 +12,7 @@ import java.util.Map;
public class SetLogLevelCommand implements ArgumentAction {
@Override
public void run(ArgumentParser parser, Argument arg, Map<String, Object> attrs, String flag, Object value) throws ArgumentParserException {
LogLevel level = LogLevel.toLogLevel((String) value);
Logger.getRootLogger().setLevel(level);
Logger.setLevel((String) value);
}
@Override
......
......@@ -13,7 +13,7 @@ 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;
Logger.setFileName(filename);
// Logger.setFileName(filename); //todo set Filename of Log-file
}
@Override
......
......@@ -54,7 +54,7 @@ public class EikonalSolverFMMTriangulation<P extends IPotentialPoint, V extends
private Map<Triple<P, P, P>, Double> cosPhis = new HashMap();
static {
logger.setLevel(LogLevel.INFO);
logger.setInfo();
}
/**
......
......@@ -78,8 +78,6 @@ 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());
simulationResult.startTime();
/**
......@@ -123,8 +121,6 @@ public class ScenarioRun implements Runnable {
} finally {
simulationResult.stopTime();
doAfterSimulation();
//remove Log4j Mapped Diagnostic Context after ScenarioRun
// MDC.remove("scenario.Name");
}
}
......
......@@ -7,7 +7,6 @@ import org.vadere.simulator.entrypoints.Version;
import org.vadere.simulator.projects.migration.jolttranformation.JoltTransformation;
import org.vadere.state.util.StateJsonConverter;
import org.vadere.util.io.IOUtils;
import org.vadere.util.logging.LogBufferAppender;
import org.vadere.util.logging.Logger;
import java.io.File;
......@@ -26,13 +25,12 @@ import static org.vadere.util.io.IOUtils.SCENARIO_DIR;
public class JoltMigrationAssistant extends MigrationAssistant {
private final static Logger logger = Logger.getLogger(JoltMigrationAssistant.class);
private final LogBufferAppender appender;
private MigrationLogger migrationLogger;
public JoltMigrationAssistant(MigrationOptions options) {
super(options);
appender = new LogBufferAppender();
logger.addAppender(appender);
migrationLogger = new MigrationLogger();
}
public JoltMigrationAssistant() {
......@@ -41,12 +39,12 @@ public class JoltMigrationAssistant extends MigrationAssistant {
@Override
public String getLog() {
return appender.getMigrationLog();
return migrationLogger.getLog();
}
@Override
public void restLog() {
appender.rest();
migrationLogger.rest();
}
......@@ -79,24 +77,28 @@ public class JoltMigrationAssistant extends MigrationAssistant {
throw new MigrationException("Could not read JsonFile or create Json representation" + e.getMessage());
}
restLog();
logger.info(">> analyzing JSON tree of scenario <" + node.get("name").asText() + ">");
migrationLogger.info(">> analyzing JSON tree of scenario <" + node.get("name").asText() + ">");
logger.info(migrationLogger.last());
Version version;
if (node.get("release") != null) {
version = Version.fromString(node.get("release").asText());
if (version == null || version.equalOrSamller(Version.UNDEFINED)) {
logger.error("release version " + node.get("release").asText() + " is unknown or not " +
migrationLogger.error("release version " + node.get("release").asText() + " is unknown or not " +
"supported. If this is a valid release create a version transformation and a new idenity transformation");
logger.error(migrationLogger.last());
throw new MigrationException("release version " + node.get("release").asText() + " is unknown or not " +
"supported. If this is a valid releasecreate a version transformation and a new idenity transformation");
}
} else {
logger.warn("Version is unknown of scenario <" + node.get("name").asText() + ">! Try to use " + Version.NOT_A_RELEASE.label() + " as Version for transformation.");
migrationLogger.warn("Version is unknown of scenario <" + node.get("name").asText() + ">! Try to use " + Version.NOT_A_RELEASE.label() + " as Version for transformation.");
logger.warn(migrationLogger.last());
version = Version.NOT_A_RELEASE;
}
if (version.equals(targetVersion)) {
logger.info("Nothing to do current version and target version match");
migrationLogger.info("Nothing to do current version and target version match");
logger.info(migrationLogger.last());
restLog();
return null;
}
......@@ -104,7 +106,8 @@ public class JoltMigrationAssistant extends MigrationAssistant {
JsonNode transformedNode = node;
// apply all transformation from current to latest version.
for (Version v : Version.listVersionFromTo(version, targetVersion)) {
logger.info("<" + node.get("name").asText() + "> Start Transform to Version: " + v.label());
migrationLogger.info("<" + node.get("name").asText() + "> Start Transform to Version: " + v.label());
logger.info(migrationLogger.last());
transformedNode = transform(transformedNode, v);
}
......@@ -192,16 +195,17 @@ public class JoltMigrationAssistant extends MigrationAssistant {
}
} catch (MigrationException e) {
moveFileAddExtension(scenarioFilePath, legacyDir, migrationOptions.getNonmigratabelExtension(), !dirName.equals(SCENARIO_DIR));
logger.error("!> Can't migrate the scenario to latest version, removed it from the directory (" +
migrationLogger.error("!> Can't migrate the scenario to latest version, removed it from the directory (" +
e.getMessage() + ") If you can fix this problem manually, do so and then remove ." +
migrationOptions.getNonmigratabelExtension() + " from the file in the " + LEGACY_DIR + "-directory "
+ "and move it back into the scenarios-directory, it will be checked again when the GUI restarts.");
logger.error(migrationLogger.last());
stats.notmigratable++;
}
}
if (stats.legacy + stats.notmigratable > 0)
IOUtils.writeTextFile(legacyDir.resolve("_LOG-" + getTimestamp() + ".txt").toString(), getLog());
migrationLogger.writeLog(legacyDir.resolve("_LOG-" + getTimestamp() + ".txt").toString());
// clean appender for next run with same JoltMigrationAssistant instance
restLog();
......@@ -220,7 +224,8 @@ public class JoltMigrationAssistant extends MigrationAssistant {
String parentPath = dirName.equals(SCENARIO_DIR) ? SCENARIO_DIR + "/" : OUTPUT_DIR + "/" + scenarioFilePath.getParent().getFileName().toString() + "/";
logger.info(">> analyzing JSON tree of scenario <" + parentPath + node.get("name").asText() + ">");
migrationLogger.info(">> analyzing JSON tree of scenario <" + parentPath + node.get("name").asText() + ">");
logger.info(migrationLogger.last());
Version version;
......@@ -228,8 +233,9 @@ public class JoltMigrationAssistant extends MigrationAssistant {
version = Version.fromString(node.get("release").asText());
if (version == null || version.equalOrSamller(Version.UNDEFINED)) {
logger.error("release version " + node.get("release").asText() + " is unknown or not " +
migrationLogger.error("release version " + node.get("release").asText() + " is unknown or not " +
"supported. If this is a valid release create a version transformation and a new idenity transformation");
logger.error(migrationLogger.last());
throw new MigrationException("release version " + node.get("release").asText() + " is unknown or not " +
"supported. If this is a valid releasecreate a version transformation and a new idenity transformation");
}
......@@ -250,18 +256,21 @@ public class JoltMigrationAssistant extends MigrationAssistant {
return false;
}
} else {
logger.warn("Version is unknown of scenario <" + parentPath + node.get("name").asText() + ">! Try to use " + Version.NOT_A_RELEASE.label() + " as Version for transformation.");
migrationLogger.warn("Version is unknown of scenario <" + parentPath + node.get("name").asText() + ">! Try to use " + Version.NOT_A_RELEASE.label() + " as Version for transformation.");
logger.warn(migrationLogger.last());
version = Version.NOT_A_RELEASE;
}
JsonNode transformedNode = node;
// apply all transformation from current to latest version.
for (Version v : Version.listToLatest(version)) {
logger.info("<" + node.get("name").asText() + "> Start Transform to Version: " + v.label());
migrationLogger.info("<" + node.get("name").asText() + "> Start Transform to Version: " + v.label());
logger.info(migrationLogger.last());
transformedNode = transform(transformedNode, v);
}
if (legacyDir != null) {
logger.info("Scenario Migrated - OK. Move copy of old version to legacllyDir");
migrationLogger.info("Scenario Migrated - OK. Move copy of old version to legacllyDir");
logger.info(migrationLogger.last());
moveFileAddExtension(scenarioFilePath, legacyDir, migrationOptions.getLegacyExtension(), false);
}
IOUtils.writeTextFile(scenarioFilePath.toString(), StateJsonConverter.serializeJsonNode(transformedNode));
......
package org.vadere.simulator.projects.migration;
import org.vadere.util.io.IOUtils;
import java.io.IOException;
/**
* Semi-Wrapper for logging in MigrationAssistant. For each project a separate logfile should be
* created, containing the standard log output. This Class will collect the logging and write it
* to the given file if needed. To remove duplicate code, the {@link #last()} method returns the
* last added log statement to be used for the logging framework.
*/
public class MigrationLogger {
private StringBuilder sb;
private String last;
public MigrationLogger(){
sb = new StringBuilder();
}
public void rest(){
sb.setLength(0);
}
public void writeLog(String filePath) throws IOException {
IOUtils.writeTextFile(filePath, sb.toString());
rest();
}
public String getLog(){
return sb.toString();
}
public void error(String message){
sb.append("ERROR:").append(message).append("\n");
last = message;
}
public void warn(String message){
sb.append("WARN:").append(message).append("\n");
last = message;
}
public void info(String message){
sb.append("Info:").append(message).append("\n");
last = message;
}
public String last(){
return last;
}
}
log4j.rootCategory=TRACE, file, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=./log.out
log4j.appender.file.Append=false
log4j.appender.file.MaxFileSize=10000KB
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%-5p [%t] %c{1}: %m%n
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
\ No newline at end of file
name=PropertiesConfig
property.filename = logs
appenders = console, file
appender.console.type = Console
appender.console.name = STDOUT
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = %d{ABSOLUTE} %5p %c{1}:%L - %m%n
appender.file.type = RollingFile
appender.file.name = FILE
appender.file.fileName = log.out
appender.file.filePattern = out-%d{MM-dd-yy-HH-mm-ss}-%i.log.gz
appender.file.layout.type = PatternLayout
appender.file.layout.pattern = %-5p [%t]: %m%n
appender.file.policies.type = Policies
appender.file.policies.size.type = SizeBasedTriggeringPolicy
appender.file.policies.size.size = 10000KB
rootLogger.level = trace
rootLogger.appenderRefs = console, file
rootLogger.appenderRef.console.ref = STDOUT
rootLogger.appenderRef.file.ref = FILE
\ No newline at end of file
package org.vadere.util.logging;
import org.apache.log4j.AppenderSkeleton;
public abstract class Appender extends AppenderSkeleton {
}
//package org.vadere.util.logging;
//
//import org.apache.log4j.AppenderSkeleton;
//
//public abstract class Appender extends AppenderSkeleton {
//
//}
package org.vadere.util.logging;
import org.apache.log4j.Layout;
import org.apache.log4j.PatternLayout;
import org.apache.log4j.spi.LoggingEvent;
/**
* This appender is used to collect all log statements create by the underling logger and to
* programmatically create the String representation of the log events.
*
* This useful if you want to show the Log of a specific Logger to the user at runtime.
* At the time of writing this is used in the Migration assistant to show the user any
* problems during scenario file migration. This Appender can be used with any Logger in vader.
*/
public class LogBufferAppender extends Appender {
private StringBuffer sb;
private Layout layout;
public LogBufferAppender() {
sb = new StringBuffer();
layout = new PatternLayout("%d{ABSOLUTE} %5p %c{1}:%L - %m%n");
}
public String getMigrationLog(){
return sb.toString();
}
public void rest(){
sb.setLength(0);
}
@Override
protected void append(LoggingEvent loggingEvent) {
sb.append(layout.format(loggingEvent));
}
@Override
public boolean requiresLayout() {
return true;
}
@Override
public void close() {
sb.setLength(0);
sb = null;
}
}
//package org.vadere.util.logging;
//
//
//import org.apache.logging.log4j.core.Filter;
//import org.apache.logging.log4j.core.Layout;
//import org.apache.logging.log4j.core.LogEvent;
//import org.apache.logging.log4j.core.appender.AbstractAppender;
//import org.apache.logging.log4j.core.config.plugins.Plugin;
//import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
//import org.apache.logging.log4j.core.config.plugins.PluginElement;
//import org.apache.logging.log4j.core.config.plugins.PluginFactory;
//import org.apache.logging.log4j.core.layout.PatternLayout;
//
//import java.io.Serializable;
//
///**
// * This appender is used to collect all log statements create by the underling logger and to
// * programmatically create the String representation of the log events.
// *
// * This useful if you want to show the Log of a specific Logger to the user at runtime.
// * At the time of writing this is used in the Migration assistant to show the user any
// * problems during scenario file migration. This Appender can be used with any Logger in vader.
// */
//@Plugin(name="MigrationAppender", category="Core", elementType="appender", printObject=true)
//public class LogBufferAppender extends AbstractAppender {
//
// private StringBuffer sb;
// private Layout layout;
//
// protected LogBufferAppender(String name, Filter filter, Layout<? extends Serializable> layout,
// boolean ignoreExceptions) {
// super(name, filter, layout, ignoreExceptions);
// sb = new StringBuffer();
// }
//
// public String getMigrationLog(){
// return sb.toString();
// }
//
// public void rest(){
// sb.setLength(0);
// }
//
// @Override
// public void append(LogEvent logEvent) {
// sb.append(new String(getLayout().toByteArray(logEvent)));
// }
//
// @PluginFactory
// public static LogBufferAppender createAppender(
// @PluginAttribute("name") String name,
// @PluginElement("Layout") Layout<? extends Serializable> layout,
// @PluginElement("Filter") final Filter filter,
// @PluginAttribute("otherAttribute") String otherAttribute) {
// if (name == null) {
// LOGGER.error("No name provided for MyCustomAppenderImpl");
// return null;
// }
// if (layout == null) {
// layout = PatternLayout.createDefaultLayout();
// }
// return new LogBufferAppender(name, filter, layout, true);
// }
//}
package org.vadere.util.logging;
import org.apache.log4j.Level;
import org.apache.log4j.PatternLayout;
import org.apache.log4j.RollingFileAppender;
public class Logger {
org.apache.log4j.Logger logger;
public static Logger getLogger(Class clazz){
return new Logger(org.apache.log4j.Logger.getLogger(clazz));
}
public static Logger getRootLogger() {
return new Logger(org.apache.log4j.Logger.getRootLogger());
}
public static void setFileName(String fileName){
RollingFileAppender appender = new RollingFileAppender();
appender.setName(fileName);
appender.setFile(fileName);
appender.setAppend(false);
appender.setMaxFileSize("10000KB");
appender.setLayout(new PatternLayout("%d{ABSOLUTE} %5p [%t] %c{1}:%L - %m%n"));
appender.activateOptions();
org.apache.log4j.Logger.getRootLogger().addAppender(appender);
}
private Logger(org.apache.log4j.Logger logger){
this.logger = logger;
}
public void setLevel(LogLevel logLevel){
logger.setLevel(Level.toLevel(logLevel.getLevelStr()));
}
public LogLevel getLevel(){
Level level = logger.getLevel();
LogLevel logLevel;
switch (level.toInt()){
case Level.ALL_INT : logLevel = LogLevel.ALL;
break;
case Level.DEBUG_INT: logLevel = LogLevel.DEBUG;
break;
case Level.ERROR_INT: logLevel = LogLevel.ERROR;
break;
case Level.FATAL_INT: logLevel = LogLevel.FATAL;
break;
case Level.INFO_INT: logLevel = LogLevel.INFO;
break;
case Level.OFF_INT: logLevel = LogLevel.OFF;
break;
case Level.WARN_INT: logLevel = LogLevel.WARN;
default: logLevel = LogLevel.INFO;
}
return logLevel;