Java sl4j 和 logback - 是否可以以编程方式设置包的日志记录级别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21368757/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
sl4j and logback - Is it possible to programmatically set the logging level for package?
提问by Stephane Grenier
I'm able to programmatically set the logging level on the application with the following code, but is it also possible to do this on a package level, say com.somepackage.* where I want the level to be only ERROR rather than DEBUG or INFO on said package?
我可以使用以下代码以编程方式在应用程序上设置日志记录级别,但也可以在包级别执行此操作,例如 com.somepackage.* 我希望级别仅为 ERROR 而不是 DEBUG 或所述包裹的信息?
// Sets the logging level to INFO
LoggerContext loggerContext = (LoggerContext)LoggerFactory.getILoggerFactory();
Logger rootLogger = loggerContext.getLogger(Logger.ROOT_LOGGER_NAME);
rootLogger.setLevel(Level.INFO);
But I can't seem to find a way to set it on a package level...
但我似乎无法找到一种方法将它设置在包级别......
采纳答案by Dimitri Dewaele
You should set the package name as logger-name
您应该将包名称设置为 logger-name
// Sets the package level to INFO
LoggerContext loggerContext = (LoggerContext)LoggerFactory.getILoggerFactory();
Logger rootLogger = loggerContext.getLogger("com.somepackage");
rootLogger.setLevel(Level.INFO);
You should be able to get the package name more elegant, but this is basically it. This follows the tree like hierarchy for the Logger Context: Logger Context
你应该能够得到更优雅的包名,但基本上就是这样。这遵循 Logger Context 的树状层次结构: Logger Context
回答by Nandu Prajapati
You can do it by using logback..
您可以通过使用 logback 来做到这一点。
Logger LOG = (Logger) org.slf4j.LoggerFactory.getLogger(ch.qos.logback.classic.Logger.ROOT_LOGGER_NAME);
LOG.setLevel(Level.WARN);
This solved my problem.
这解决了我的问题。
回答by Abhijit Sarkar
You can get the SLF4J logger for the package and cast it to Logback logger. Less code that @dimitri-dewaele's.
您可以获取包的 SLF4J 记录器并将其转换为 Logback 记录器。@dimitri-dewaele 的代码更少。
((Logger) LoggerFactory.getLogger("com.somepackage")).setLevel(DEBUG)
@nandu-prajapati's approach is similar, except that it sets the root logger level, not the one desired.
@nandu-prajapati 的方法类似,只是它设置了根记录器级别,而不是所需的级别。
回答by Deepak
In case you don't want to change the logback-classic to a compile-time dependency, here is some code that uses reflection to set this level assuming that logback is used as the slf4j runtime-binding. Here AbcClassis the class whose logger level you want to change to TRACE:
如果您不想将 logback-classic 更改为编译时依赖项,这里有一些代码使用反射来设置此级别,假设 logback 用作 slf4j 运行时绑定。这里AbcClass是您想要将其记录器级别更改为TRACE 的类:
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class SomeClass {
private static final Logger logger = LoggerFactory.getLogger(SomeClass .class);
public static void beforeEachTest() throws Exception {
final Logger loggerInterface = LoggerFactory.getLogger(AbcClass.class);
String loggerLevelNew = "TRACE";
if (!loggerInterface.isTraceEnabled()) {
try {
Class<?> levelLogBackClass = Class.forName("ch.qos.logback.classic.Level");
Method toLevelMethod = levelLogBackClass.getDeclaredMethod("toLevel", String.class);
Object traceLvel = toLevelMethod.invoke(null, loggerLevelNew);
Method loggerSetLevelMethod= loggerInterface.getClass().getDeclaredMethod("setLevel", levelLogBackClass);
loggerSetLevelMethod.invoke(loggerInterface, traceLvel);
} catch (Exception e) {
logger.warn("Problem setting logger level to:{}, msg: {}", loggerLevelNew, e.getMessage());
throw e;
}
}
}
}
Something similar can be done for log4j and JDK logging to make this (kind-of) library agnostic
可以对 log4j 和 JDK 日志进行类似的操作,使这个(某种)库不可知