Creates a Class that lets us configure the RootLogger for everything (BudaLogConfig)

This commit is contained in:
Seraina 2022-03-31 12:42:21 +02:00
parent b634f16481
commit 25690c14f4
3 changed files with 30 additions and 30 deletions

View File

@ -0,0 +1,23 @@
package ch.unibas.dmi.dbis.cs108;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.config.Configuration;
import org.apache.logging.log4j.core.config.LoggerConfig;
public class BudaLogConfig {
public Logger LOGGER;
public BudaLogConfig(Logger LOGGER) {
this.LOGGER = LOGGER;
LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
Configuration config = ctx.getConfiguration();
LoggerConfig loggerConfig = config.getLoggerConfig(LogManager.ROOT_LOGGER_NAME);
loggerConfig.setLevel(Level.INFO); // change level here
ctx.updateLoggers(); // This causes all Loggers to refetch information from their LoggerConfig.
}
}

View File

@ -1,30 +0,0 @@
package ch.unibas.dmi.dbis.cs108.Spiellogikentwurf;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.config.Configuration;
import org.apache.logging.log4j.core.config.LoggerConfig;
public class LoggingExp {
public static final Logger LOGGER = LogManager.getLogger();
public static void main(String[] args) {
/* change root logger level
* copied from https://stackoverflow.com/questions/23434252/ - I think it works, but I don't know why
*/
LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
Configuration config = ctx.getConfiguration();
LoggerConfig loggerConfig = config.getLoggerConfig(LogManager.ROOT_LOGGER_NAME);
loggerConfig.setLevel(Level.DEBUG); // change level here
ctx.updateLoggers(); // This causes all Loggers to refetch information from their LoggerConfig.
LOGGER.debug("Im not printed by default ");
LOGGER.error("An error , printed by default ");
LOGGER.info("We can log variables with string concatenation :" + args);
LOGGER.info("Or like this :{} ", args);
}
}

View File

@ -1,6 +1,11 @@
package ch.unibas.dmi.dbis.cs108.Spiellogikentwurf;
import ch.unibas.dmi.dbis.cs108.BudaLogConfig;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class Train {
public static final Logger LOGGER = LogManager.getLogger();
public static final BudaLogConfig l = new BudaLogConfig(LOGGER);
int[] orderOfTrain; //gives the random order in which the passengers enter the train
int positionOfGhost;
@ -38,6 +43,7 @@ public class Train {
}
}
LOGGER.debug("A bug");
this.orderOfTrain = userTrain;
this.positionOfGhost = nrOfPlayers / 2;
}
@ -57,6 +63,7 @@ public class Train {
}
i++;
}
LOGGER.info("An information");
return false;
}