Hi Mike,
in our app we implemented a REST-Service to reconfigure log4j2 at runtime.
It's mainly to apply changes to the configuration and call updateLoggers()
LoggerContext loggerContext = (LoggerContext)
LogManager.getContext(false);
Configuration configuration = loggerContext.getConfiguration();
LoggerConfig loggerConfig = configuration.getLoggerConfig(loggerName);
LoggerConfig specificConfig = loggerConfig;
if (!loggerConfig.getName().equals(loggerName)) {
specificConfig = new LoggerConfig(loggerName, level, true);
specificConfig.setParent(loggerConfig);
configuration.addLogger(loggerName, specificConfig);
}
specificConfig.setLevel(level);
loggerContext.updateLoggers();
see attachment for our full Log4j2Util code.
Regards,
Volker
2018-02-08 21:28 GMT+01:00 Mike Kienenberger <[email protected]>:
> As others have reported in years past, the examples in the docs for
>
> Programmatically Modifying the Current Configuration after Initialization
>
> are out of date. They don't compile. They don't work (affect the
> existing logging) even if you do fix the errors.
>
> Here's my situation:
>
> I am working in an environment with EVIL.JAR which includes a log4j2.xml
> file.
> I can't change the jar. I can't specific a System Property to override it.
>
> My code gets called as a loaded module long after the logging system
> is initialized.
>
> I want logging in my own code to go to a different location, and
> preferably I'd like to read the configuration in from a log4j2.xml
> file so that anyone who uses my module isn't victim to the same evil
> hardcoded-logging practices of EVIL.JAR.
>
> Creating an XMLConfiguration and initializing it lets me read the xml
> file easily enough. Looping through the data gets me the Appenders,
> Filters and Loggers. But I still can't use them to modify the
> existing configuration.
>
> Another person took the approach of using JUL instead. I hate JUL and
> I'd really rather not have to go down that route.
>
> Thanks in advance.
> -Mike
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [email protected]
> For additional commands, e-mail: [email protected]
>
>
--
inexso - information exchange solutions GmbH
Ofener Straße 30 | 26121 Oldenburg
www.inexso.de
package de.inexso.commons.logging;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.appender.AbstractAppender;
import org.apache.logging.log4j.core.appender.ConsoleAppender;
import org.apache.logging.log4j.core.appender.FileAppender;
import org.apache.logging.log4j.core.config.Configuration;
import org.apache.logging.log4j.core.config.LoggerConfig;
import org.apache.logging.log4j.core.layout.PatternLayout;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Log4j2Util {
private static final Logger LOG = LoggerFactory.getLogger(Log4j2Util.class);
public static void setLoggerLevel(String loggerName, String levelName) throws IllegalArgumentException {
Level level = toLevel(levelName);
setLoggerLevel(loggerName, level);
}
public static Level toLevel(String levelName) throws IllegalArgumentException {
Level level = Level.toLevel(levelName, null);
if (level == null) {
throw new IllegalArgumentException("Unknown levelName \"" + levelName + "\"");
}
return level;
}
public static void setLoggerLevel(Configuration configuration, String loggerName, String levelName)
throws IllegalArgumentException {
setLoggerLevel(configuration, loggerName, toLevel(levelName));
}
public static void setLoggerLevel(String loggerName, Level level) {
LoggerContext loggerContext = (LoggerContext) LogManager.getContext(false);
Configuration configuration = loggerContext.getConfiguration();
setLoggerLevel(configuration, loggerName, level);
loggerContext.updateLoggers();
}
public static void setLoggerLevel(Configuration configuration, String loggerName, Level level) {
LoggerConfig loggerConfig = configuration.getLoggerConfig(loggerName);
LoggerConfig specificConfig = loggerConfig;
if (!loggerConfig.getName().equals(loggerName)) {
specificConfig = new LoggerConfig(loggerName, level, true);
specificConfig.setParent(loggerConfig);
configuration.addLogger(loggerName, specificConfig);
}
specificConfig.setLevel(level);
}
public static void replaceRootAppender(Configuration configuration, String appenderName, String logFileName, String logLayout) {
AbstractAppender logAppender;
if ("STDOUT".equalsIgnoreCase(logFileName)) {
logAppender = ConsoleAppender.newBuilder()
.withLayout(PatternLayout.newBuilder().withPattern(logLayout).build())
.withName(appenderName)
.build();
} else {
logAppender = FileAppender.newBuilder()
.withFileName(logFileName)
.withAppend(true)
.withLayout(PatternLayout.newBuilder().withPattern(logLayout).build())
.withName(appenderName)
.build();
}
logAppender.start();
LoggerConfig rootLogger = configuration.getRootLogger();
rootLogger.removeAppender(appenderName);
rootLogger.addAppender(logAppender, null, null);
}
public static void createFileAppender(String name, String fileName, String pattern) {
LoggerContext loggerContext = (LoggerContext) LogManager.getContext(false);
Configuration configuration = loggerContext.getConfiguration();
FileAppender fileAppender = FileAppender.newBuilder()
.withFileName(fileName)
.withAppend(true)
.withLayout(PatternLayout.newBuilder().withPattern(pattern).build())
.withName(name)
.build();
configuration.addAppender(fileAppender);
loggerContext.updateLoggers();
}
public static void createLogger(String name, String level, boolean additive, String appenderName) {
LoggerContext loggerContext = (LoggerContext) LogManager.getContext(false);
if (loggerContext.hasLogger(name)) {
LOG.warn("logger \"{}\" already exists!", name);
}
Configuration configuration = loggerContext.getConfiguration();
LoggerConfig loggerConfig = new LoggerConfig(name, toLevel(level), true);
loggerConfig.setAdditive(additive);
loggerConfig.addAppender(configuration.getAppender(appenderName), null, null);
configuration.addLogger(name, loggerConfig);
loggerContext.updateLoggers();
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]