Michael W
Michael W

Reputation: 3555

Is it possible to change the log4j level in my packages but not change the levels in api's im using e.g spring

So I have this code:

public class LoggingManager {

Logger root = Logger.getRootLogger();

    public void setLogger(String fullClassName, String level) {

    LogManager.getLogger(fullClassName).setLevel(Level.toLevel(level));
}

public void logLevelAll(String level) {

    root.setLevel(Level.toLevel(level));
}
}

My problem is this. The first method takes in values: p1: com.domain.data.Object and p2: DEBUG and will change that classes log level to debug. The second class will change every logger in the project including libraries referenced like spring.

I want to create a method that only changes the logging level of my packages. So changes com.domain.* if you like.

Can this be done?

Upvotes: 0

Views: 758

Answers (2)

Michael W
Michael W

Reputation: 3555

Just realised that. Here is the full answer I got for others:

Log4j provides a default root logger that all user-defined loggers inherit from. Root logger is at the top of the logger hierarchy; in other words, root logger is either parent or ancestor of all logger objects created.

For example: A class 'MyClass' in com.domain.sampleapp application package can have a logger named com.domain.sampleapp.MyClass you can use my method above to set the class:

LogManager.getLogger("com.domain.sampleapp.MyClass").setLevel(Level.toLevel("DEBUG"));

But you can also set the package log level like so:

LogManager.getLogger("com.domain.sampleapp").setLevel(Level.toLevel("DEBUG"));

And finally you could set a whole group of packages (i.e ur whole project) by using:

LogManager.getLogger("com.domain").setLevel(Level.toLevel("DEBUG"));

Upvotes: 2

Joachim Sauer
Joachim Sauer

Reputation: 308031

Simply set the log level for com.domain. By default each logger will inherit the log level from its parent logger.

Upvotes: 3

Related Questions