我在我的应用程序中使用Log4j2。我们需要提供一种在运行时重新配置日志记录以帮助应用程序调试的工具。例如,我们应该能够在运行时添加新的包级记录器,类级记录器等。我在这里提出的问题陈述是针对包级记录器配置的。请检查以下代码:
LogConfigurer.java
public class LogConfigurer {
public static void addPackageLogger(final String packageName, final Level level) {
final LoggerContext context = (LoggerContext) LogManager.getContext(false);
final Configuration configuration = context.getConfiguration();
final Appender appender = configuration.getAppender("RollingRandomAccessFile");
final LoggerConfig loggerConfig = new LoggerConfig(packageName, level, false);
loggerConfig.addAppender(appender, level, null);
configuration.addLogger(packageName, loggerConfig);
context.updateLoggers();
}
}
PackageLoggingMock.java (示例类)
public class PackageLoggingMock {
// Logger
private final static org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(PackageLoggingMock.class);
public void mockMethod(final String msg) {
logger.debug("[PackageLoggingMock] Debug statement...{}", msg);
logger.info("[PackageLoggingMock] Info statement...{}", msg);
logger.warn("[PackageLoggingMock] Warn statement...{}", msg);
logger.error("[PackageLoggingMock] Error statement...{}", msg);
}
}
PackageLoggingDemo.java
public class PackageLoggingDemo {
public static void main(final String[] args) {
final PackageLoggingMock mock = new PackageLoggingMock();
// Running with default settings provided in log4j2.xml
mock.mockMethod("BEFORE");
// Reset log level of package "com.logging.demo" to DEBUG
try {
LogConfigurer.addPackageLogger("com.logging.demo", Level.DEBUG);
} catch (final Exception e) {
e.printStackTrace();
}
// After resetting to DEBUG level
mock.mockMethod("AFTER SETTING TO DEBUG");
// Reset log level of package "com.logging.demo" to INFO
try {
LogConfigurer.addPackageLogger("com.logging.demo", Level.INFO);
} catch (final Exception e) {
e.printStackTrace();
}
// After resetting to DEBUG level
mock.mockMethod("AFTER SETTING TO INFO");
}
}
输出
[PackageLoggingMock] Error statement...BEFORE
[PackageLoggingMock] Debug statement...AFTER SETTING TO DEBUG
[PackageLoggingMock] Info statement...AFTER SETTING TO DEBUG
[PackageLoggingMock] Warn statement...AFTER SETTING TO DEBUG
[PackageLoggingMock] Error statement...AFTER SETTING TO DEBUG
[PackageLoggingMock] Debug statement...AFTER SETTING TO INFO
[PackageLoggingMock] Info statement...AFTER SETTING TO INFO
[PackageLoggingMock] Warn statement...AFTER SETTING TO INFO
[PackageLoggingMock] Error statement...AFTER SETTING TO INFO
即使将级别重新配置为DEBUG
,它仍会打印INFO
语句。
我无法理解这种行为;是预期的吗?
作为一种解决方法,在首次重新配置(到级别DEBUG
)之后,我尝试重置记录器以获得原始配置,然后以编程方式应用INFO
级别并且它有效。
有没有办法在不重置日志的情况下多次配置日志级别?
答案 0 :(得分:2)
你刚才加入。要进行更改,您必须删除该软件包的旧配置并添加新配置。
遵循此提示,OP使用
configuration.removeLogger(packageName);
configuration.addLogger(packageName, loggerConfig);
解决问题。