Asma Zinneera Jabir
Asma Zinneera Jabir

Reputation: 841

log4j2.properties set logger level to many packages

AFAIK the logger level for a package/class in log4j2 properties file should be set like below.

loggers = abc
logger.abc.name = com.abc.MyClass
logger.abc.level = INFO

So if I have 50 classes/packages to specify, does it mean I have to have 50 entries in loggers and 50 of entries for each name and level.

Is not there an alternative way to do it with one line for each logger so it can be done in 50 lines?

Upvotes: 23

Views: 19738

Answers (3)

albert_nil
albert_nil

Reputation: 1658

In the name you can put a package (or subpackage), you don't need to specify specific class name; unless you want specific configuration for that class obviously.

Also, you can specify default configuration for all classes that do not fall in specific configurations (rootLogger).

Look for "Configuration with Properties" section in here for more details.

Upvotes: 2

Dimitar II
Dimitar II

Reputation: 2509

You can specify the logging... for each package. Like this:

logger.mongodb.name = org.mongodb.driver
logger.mongodb.level = warn

logger.ehcache.name = org.ehcache.core
logger.ehcache.level = warn

Upvotes: 28

Woody
Woody

Reputation: 844

  • Configuring different levels of logs in property file is mostly usefull in case of external librairies (for instance Hibernate logs could be very verbose)
  • For your application is easier to define a root level of logging and choose in your code the level of the log message you want like this

Property file

rootLogger.level = info

Java code

final Level DEBUG = Level.forName("debug", 550); // could also be a custom level

final Logger logger = Logger.getLogger(MyClass.class);
logger.log(DEBUG, "a verbose message");

Upvotes: 0

Related Questions