Log4j2 - PatternLayout dependend on LogLevel

Viewed 661

Is it possible to define two different PatternLayouts and dependend on the application LogLevel it uses the first or the second one?

Example:

If i run my application with ERROR Level, which is the default, it should print the log statements like:

$java -jar myApp.jar param1=value1

Error Message 1
Error Message 2

But if i run my application with INFO or DEBUG, it should print the log Statements like:

$java -jar myApp.jar --debug param1=value1

DEBUG | 18.03.2016 11:04:43,412 1058 | Debug Message 1
DEBUG | 18.03.2016 11:04:43,412 1058 | Debug Message 2
INFO  | 18.03.2016 11:04:43,414 1060 | Info Message 1
ERROR | 18.03.2016 11:04:43,420 1066 | Error Message 1
ERROR | 18.03.2016 11:04:43,420 1066 | Error Message 2

Internally if the parameter --debug or --info is given, my app is switching the global LogLevel. The default is ERROR.

How should i configure my log4j2.xml to accomplish this?? I could not find any solution. For reference my commandLine parser and the util class where i switch the LogLevel at runtime.

2 Answers
Related