Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set the log level on a class in log4j2 properties

Tags:

in log4j I could specify a class in the properties file to log at the debug level like this:

log4j.logger.com.mycompany.mypackage.ClassName=DEBUG 

How do I do this in log4j2? Note I still need to use the new property file (not xml or json).

TIA

like image 720
sproketboy Avatar asked Oct 07 '15 15:10

sproketboy


People also ask

How do I change log level in Log4j2?

You can set a logger's level with the class Configurator from Log4j Core. BUT be aware that the Configurator class is not part of the public API. If you wish to change the root logger level, do something like this : LoggerContext ctx = (LoggerContext) LogManager.

How do you set log file path in Log4j properties dynamically?

System. setProperty("{my. log", "C:/logfile. log");

How do you change log levels?

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)


1 Answers

As the log4j2 configuration documentation states

As of version 2.4, Log4j now supports configuration via properties files. Note that the property syntax is NOT the same as the syntax used in Log4j 1.

It then provides a substantial example for all types of configuration elements.

Concerning your question, you need to specify your loggers in a loggers element, then configure each of them. For example

loggers = mine  logger.mine.name = com.mycompany.mypackage.ClassName logger.mine.level = DEBUG 

Note that log4j2 looks for a .properties file on the classpath by default.

If a test file cannot be located the properties ConfigurationFactory will look for log4j2.properties on the classpath.

But you can also configure the location yourself. You can use the system property

-Dlog4j.configurationFile=conf/log4j.properties 

with an appropriate path.

like image 136
Sotirios Delimanolis Avatar answered Sep 24 '22 22:09

Sotirios Delimanolis