????Log4j?汾2?н?MDC??NDC??????????????????У????????????????????????????????????MDC??NDC???????????????????????Map??????????????????MDC??NDC????????????ThreadContext???????????????????????????????????????Log4j?汾1?е?MDC??NDC??
????When using the Thread Context Stack?? data is pushed to and popped from a stack just like with NDC:
???????????????????????????????NDC???????????????????????
????import org.apache.logging.Log4j.ThreadContext;
????...
????ThreadContext.push(username);
????ThreadContext.push(sessionID);
????// Logging methods go here
????ThreadContext.pop();
????...
?????????????????????????????????MDC??????????????????
????import org.apache.logging.Log4j.ThreadContext;
????...
????ThreadContext.put(“username”??"admin");
????ThreadContext.put("sessionID"?? "1234");
????// Logging methods go here
????ThreadContext.clearMap();
????...
????ThreadContext???????Щ???????????????????MDC??????洢?????????е??????????????????ThreadContext.clearAll()??ThreadContext.clearMap()??ThreadContext.clearStack()??
????????MDC???NDC?????????????????Layouts????????????з?????Щ??????PatternLayout???%x??????????л?????%X??%X(??)?????л?????
????ThreadContext????
?????Щ?????????????Щ???????????й???????磬Log4j??DynamicThresholdFilter ??????????????????????£???????????????????磬??????????????TRACE?????????????????????????????trace-logging-enabled?????????log4j???????????????????????
????<Configuration name="MyApp">
????<DynamicThresholdFilter key="trace-logging-enabled" onMatch="ACCEPT" onMismatch="NEUTRAL">
????<KeyValuePair key="true" value="TRACE" />
????</DynamicThresholdFilter>
????...
???????ThreadContext??????????trace-logging-enabled?????onMatch ?? onMismatch ???????δ??????????? onMatch ?? onMismatch?????????????????ACCEPT??????????????????DENY???????????????????NEUTRAL?????????????????????????????Щ????????????????????????true???????????TRACE??????????
???????????trace-logging-enabled?????ó?true????????Logger???????????????TRACE??Appender?????TRACE??????????
????????????????Щ???????????????Appender?У?Log4j??????ThreadContextMapFilter????????????????????????????????Appender?????????????????TRACE????????????????????username????????ThreadContextMapFilter??
????<Console name="ConsoleAppender" target="SYSTEM_OUT">
????<ThreadContextMapFilter onMatch="ACCEPT" onMismatch="DENY">
????<KeyValuePair key="username" value="admin" />
????</ThreadContextMapFilter>
????...
??????????????????????????Log4j??Logback????й???DynamicThresholdFilter?????
????Markers
????Markers??????????????????????Щ????????????????????????????з??飬?????Щ???????????????????й????????????????????????Appender?С????????????Markers??ThreadContext????????????????????????????????????????
???????磬?????????????????????????????????????????????????????????????????????fatal????????????????????DB_ERROR??Marker??????????????????У?
????import org.apache.logging.Log4j.Marker;
????import org.apache.logging.Log4j.MarkerManager;
????...
????final static Marker DB_ERROR = MarkerManager.getMarker("DATABASE_ERROR");
????...
????logger.fatal(DB_ERROR?? "An exception occurred.");
????????????????????Marker??????????????PatternLayout?????%marker???????
????<PatternLayout pattern="%p %marker: %m%n" />
????[FATAL] DATABASE_ERROR: An exception occurred.
???????????JSON??XML?????Layouts?????????????а???Marker?????
????...
????"thread" : "main"??
????"level" : "FATAL"??
????"loggerName" : "DBClass"??
????"marker" : {
????"name" : "DATABASE_ERROR"
????}??
????"message" : "An exception occurred."??
????...
?????????Marker???????????????????????????????????????????????????????????
????Markers????
????Marker???????????????????ЩMarker????ЩLogger???????marker??λ????????????????Marker??????????????????Logger????к?????????????磬??Log4j?У???????????????Appender????????Щ?????DB_ERROR Marker???????????????log4j2.xml?е?Appender????????????????
????<MarkerFilter marker="DATABASE_ERROR" onMatch="ACCEPT" onMismatch="DENY" />
?????????????????????Marker????????????marker??Σ????onMatch???????δ??????????????????????????????????????Marker????????onMismatch???????δ????????????????onMatch??onMismatch???????3???????ACCEPT???????????????DENY???????????????NEUTRAL?????????????????κδ????
??????Logback?У?????????????Щ???á????????Appender?????????μ?EvaluatorFilter???????????????onMatch??onMismatch??????????????OnMarkerEvaluator????Marker??????????????
????<filter class="ch.qos.Logback.core.filter.EvaluatorFilter">
????<evaluator class="ch.qos.Logback.classic.boolex.OnMarkerEvaluator">
????<marker>DATABASE_ERROR</marker>
????</evaluator>
????<onMatch>ACCEPT</onMatch>
????<onMismatch>DENY</onMismatch>
????</filter>
??????Markers??NDC??MDC???ThreadContext??????
????Marker??????ThreadContext?????????????????????????????????Щ????????Appender?????????????????????????????????????????????????????????????????????????????????????????????????????
????NDC??MDC??ThreadContext????????????????????????????????ó????????????????????ThreadContext??????????????????????????????????????????ThreadContext???????????????????????????????????????????????????????????????顣
??????????棬Marker???????????????????Щ???????????????????У????????DB_ERROR Marker????????????з?????SQL?????????????????DB_ERROR Marker??????Щ??????????????????????????????????????????SMTP Appender??????Щ?????????????????????????
???????????
??????????
????Java Logging??Jakob Jenkov??——???Java Logging API??????????????
????Java Logging Overview??Oracle??—— Oracle??????Java?н???????????????
????Log4J Tutorial??Tutorials Point??——???log4j ?汾1????????????????
????????????
????Apache Commons Logging??Apache??——???Log4j??Avalon LogKit??java.util.logging??????
????SLF4J??QOS.ch??——????????????????????????????????Log4j??Logback???java.util.logging
??????????
????Java Logging API??Oracle??—— Java??????????
????Log4j??Apache??——?????????
????Logback??Logback Project??——??????????????Log4j?汾1??????汾
????tinylog??tinylog??——?????????logger