Java – sl4j and logback – can the logging level of a package be set programmatically?

I can use the following code to programmatically set the logging level of the application, but I can also do this at the package level, such as com somepackage.* Do I want this level to be only error, not debug or info on the package?

// 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 at the package level

Solution

You should set the package name to 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 a more elegant package name, but this is basically the case This follows the tree hierarchy of the logger context: logger context

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>