I am new with Logging and Log4j. What I want to do is change logger level per request. This means:
Normally, the priority level is set to ERROR, but a user can call the server with a special parameter to set the priority log level to DEBUG, but only for that user/request.
This means that if a user A sends a request http://myServer.com/test it logs only those message that have a priority of ERROR.
But if a user A sends a request http://myServer.com/test?debug=true, the logger logs all messages, however if user B simultaneously sends requests http://myServer.com/test only ERROR messages are logged.
It would be good if those logs can be saved in new appenders.
I think you should use Log4j Filters.
Have this in your log4j2.xml configuration:
<DynamicThresholdFilter key="X-Log-Level" onMatch="ACCEPT" onMismatch="NEUTRAL" defaultThreshold="ERROR">
<KeyValuePair key="TRACE" value="TRACE"/>
<KeyValuePair key="DEBUG" value="DEBUG"/>
</DynamicThresholdFilter>
(...) And set up a filter in your request that will assign "X-Log-Level" to the Thread Context via, for example, MDC.
// Replace the hardcoded logLevel value with something dynamic,
// ideally from the http request header.
String logLevel = "DEBUG";
MDC.put("X-Log-Level", logLevel);