Java??????
???????????? ???????[ 2015/7/20 10:19:19 ] ??????????????????? ???????
??????????????FasterXML Wiki??????????Jackson?????????
??????????JSON??????
????????????JSON?????????JSON???????????????CodeAcademy????????????????????????????????????γ??????JavaScript?????????Java???????Щ???????????JSONLint??JSON????????????????????????????????JSON????
????NDC??MDC???ThreadContext
????????????????ó????????web?????????????????????????????????????????????????????????????????????????????????????й????????????????г??????????????????????????????г??????????????????????????????????????????????????????е?????????????????Щ??????????????ID????ID?????豸ID????????NDC??MDC???ThreadContext??????????
????NDC??MDC??ThreadContext?????????????????????????????????????????log trails??????Щ????????????????fish tagging???????????????????????????????????????Щ????????????????????й?????????????????????????????????????????????磬??????Web??ó??????????????????μ????????????????????????ID????????????????????????????????и???????????????????????????????????????????
?????????????????NDC??
????NDC?????????????????Nested Diagnostic Context???????????????????????????????????????????е???????Logger?????????Logger?????????????????д????κ????
??????????????????NDC??Log4j????????????????????????????????NDC?????????????????????????????????????????????????NDC?????????????У? NDC.oush(username) ?? NDC.push(sessionID) ????????д洢?????????????admin?????ID??1234??????NDC.pop()???????Щ???????????NDC.remove()??????Java??????棬???????????????
????import java.io.FileReader;
????import org.apache.Log4j.Logger;
????import org.apache.Log4j.NDC;
????...
????String username = "admin";
????String sessionID = "1234";
????NDC.push(username);
????NDC.push(sessionID);
????try {
????// tmpFile doesn't exist?? causing an exception.
????FileReader fr = new FileReader("tmpFile");
????}
????catch (Exception ex) {
????logger.error("Unable to open file.");
????}
????finally {
????NDC.pop();
????NDC.pop();
????NDC.remove();
????}
????Log4j??PatternLayout?????%x????????NDC?????????????????????????????????????NDC???????Log4j??
????<PatternLayout pattern="%x %-5p - %m%n" />
???????г???????????ó???????????
????"admin 1234 ERROR – Unable to open file."
?????????????????MDC??
????MDC?????????????????NDC?????????????????MDC????洢???????У?????????С??????????????????Layout??????????????????MDC.put(key??value) ??????????μ????????????????У??? MDC.remove(key) ???????????????????
??????????????????????????????ID???????????? MDC.put() ?????????????????洢???????
????import java.io.FileReader;
????import org.apache.Log4j.Logger;
????import org.apache.Log4j.MDC;
????...
????MDC.put("username"?? "admin");
????MDC.put("sessionID"?? "1234");
????try {
????// tmpFile doesn't exist?? causing an exception.
????FileReader fr = new FileReader("tmpFile");
????}
????catch (Exception ex) {
????logger.error("Unable to open file!");
????}
????finally {
????MDC.clear();
????}
???????????????????????????Context???????????? MDC.clear() ?????????е??????MDC???????????????????????????????MDC??????????????Щ?????????????
?????????????з???MDC????????????Щ????????洢?????????е??κμ?????????????%X(??)????????????????????????????????? %X(username) ?? %X(sessionID) ??????????????????ID??
????<PatternLayout pattern="%X{username} %X{sessionID} %-5p - %m%n" />
????"admin 1234 ERROR – Unable to open file!"
??????????????????κμ??????MDC????????? {(key?? value)??(key?? value)} ?????????Appender??
????Logback?е?NDC??MDC
??????Log4j?????Logback??????????NDC??????slf4j-ext?????????NDC?????????MDC???????????Logback????????????? MDC.put()??MDC.remove() ?? MDC.clear() ??????????????MDC??
????import org.slf4j.MDC;
????...
????Logger logger = LoggerFactory.getLogger(MDCLogback.class);
????...
????MDC.put("username"?? "admin");
????MDC.put("sessionID"?? "1234");
????try {
????FileReader fr = new FileReader("tmpFile");
????}
????catch (Exception ex) {
????logger.error("Unable to open file.");
????}
????finally {
????MDC.clear();
????}
??????Logback?У????????Logback.xml?н???????????Appender??????????????????Log4j?????????
????<Pattern>[%X{username}] %X{sessionID} %-5p - %m%n</Pattern>
????"[admin] 1234 ERROR - Unable to open file."
???????MDC??????????????PatternLayout??????磬?????JSONFormatter???MDC?е????????????????
????{
????"timestamp":"1431970324945"??
????"level":"ERROR"??
????"thread":"main"??
????"mdc":{
????"username":"admin"??
????"sessionID":"1234"
????}??
????"logger":"MyClass"??
????"message":"Unable to open file."??
????"context":"default"
????}
????ThreadContext
????Version 2 of Log4j merged MDC and NDC into a single concept known as the Thread Context. The Thread Context is an evolution of MDC and NDC?? presenting them respectively as the Thread Context Map and Thread Context Stack. The Thread Context is managed through the static ThreadContext class?? which is implemented similar to Log4j 1’s MDC and NDC classes.
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11