The MDC concept:manufacturing Data Collection real-time acquisition and analysis. sometimes the actual development process does not find the bug in the production environment, need to go to the production environment to analyze the real-time log for bug tracking; LOG4J2 provides MDC functionality to output specific users ' logs to a specific file in a separate process.
config keyword threadcontext,dynamicthresholdfilter ,threadcontextmapfilter
1.dynamicthresholdfilter controls the log default output level as a whole and can adjust the log level for specific values <!--threadcontext.put ("LoginId", "User1"); Other than User1, the log is the error level by default, and the USER1 related log is the debug level--
<dynamicthresholdfilter key= "LoginId" defaultthreshold= "ERROR" onmatch= "ACCEPT" onmismatch= "DENY" > < KeyValuePair key= "User1" value= "DEBUG"/> </DynamicThresholdFilter>
2.Threadcontextmapfilter The record contents of the control file, record to the file according to the condition, and all others refuse to write the log <!--I only record USER1 log information, to find production environment bugs. --<file name= "Testuserlog" filename= "Target/testuserlog2" append= "true" > <threadcontextmapfilter onMat Ch= "ACCEPT" onmismatch= "DENY" > <keyvaluepair key= "loginId" value= "User1"/> </THREADCONTEXTMAPFILTER&G T <patternlayout pattern= "%n%t%-5p%c{2} mdc%x-%m"/> </File>
3. In the Web application, you can add the following code to the filter filters:threadcontext.put ("LoginId", Session.getattribute ("userid"));//UserID represents the work number information of the login session In the case of an app, you can use the proxy mode (if you use spring, you can work with AOP, if you don't have spring, you can use Cglib or other) to implement
code engineering based on MAVEN, resources: Http://note.youdao.com/share/?id=75707f4ae039b0d1e2f2cf46c8342d34&type=note
MDC application configuration for LOG4J2