Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sl4j and logback - Is it possible to programmatically set the logging level for package?

Tags:

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?

// 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...

like image 426
Stephane Grenier Avatar asked Jan 26 '14 20:01

Stephane Grenier


People also ask

How do you change the log level in SLF4J?

When using log4j, the Logger. log(Priority p, Object message) method is available and can be used to log a message at a log level determined at runtime. We're using this fact and this tip to redirect stderr to a logger at a specific log level. slf4j doesn't have a generic log() method that I can find.

How do you change the log level?

To change log levels as a root user, perform the following: To enable debug logging, run the following command: /subsystem=logging/root-logger=ROOT:change-root-log-level(level=DEBUG) To disable debug logging, run the following command: /subsystem=logging/root-logger=ROOT:change-root-log-level(level=INFO)

Which of the following file is used by Logback logging system?

properties file will be defined as properties in the logback. xml file.


2 Answers

You should set the package name as 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

like image 66
Dimitri Dewaele Avatar answered Oct 13 '22 19:10

Dimitri Dewaele


You can do it by using logback..

Logger LOG = (Logger) org.slf4j.LoggerFactory.getLogger(ch.qos.logback.classic.Logger.ROOT_LOGGER_NAME); LOG.setLevel(Level.WARN); 

This solved my problem.

like image 23
Nandu Prajapati Avatar answered Oct 13 '22 19:10

Nandu Prajapati