?????????
????Log4j??Logback?е?PatternLayout??????????????????????????δ????????????????????????ζ???????и?????????????????Щ????????????????Log4j??Logback??????????Щ???????ζ?????????????????????е???ζ??????????????????????????????????Log4j??Logback??PatternLayout?????

???????磬?????PatternLayout????????????x?????????????????????????????????????
????[%p] %t: %m
??????????????????????????????????????
????[INFO] main: initializing worker threads
????[DEBUG] worker: listening on port 12222[INFO] worker: received request from 192.168.1.200[ERROR] worker: unknown request ID from 192.168.1.200
???????????????
???????????Java????????ù???????????п????????????????????????????????????з????????????????????λ??????е?λ?á????磬????????????????????????????????????????????
????[ERROR] main: Unable to open file! java.io.FileNotFoundException: foo.file (No such file or directory)
????at java.io.FileInputStream.open(Native Method) ~[?:1.7.0_79]
????at java.io.FileInputStream.<init>(FileInputStream.java:146) ~[?:1.7.0_79]
????at java.io.FileInputStream.<init>(FileInputStream.java:101) ~[?:1.7.0_79]
????at java.io.FileReader.<init>(FileReader.java:58) ~[?:1.7.0_79]
????at FooClass.main(FooClass.java:47)
?????????????????????FooClass?????????????main????????????47?У?FileReader???????????????foo.file?????????????????????????????foo.file??????????Java?????????????FileNotFoundException??????????????????????try-catch????У?????????????????????????????????????????????????????
???????PatternLayout???????????
??????д?????????°汾??Log4j??Logback?У??????Layout????к???????????????????????????%xEx??????????????????????η??????????????????PatternLayout?С??????????????????????????£?
????[%p] %t: %m
????????????
????[%p] %t: %m%xEx
??????????????????????????????????????????????????????
????[ERROR] main: Unable to open file! java.io.FileNotFoundException: foo.file (No such file or directory)
????at java.io.FileInputStream.open(Native Method) ~[?:1.7.0_79]
????at java.io.FileInputStream.<init>(FileInputStream.java:146) ~[?:1.7.0_79]
????at java.io.FileInputStream.<init>(FileInputStream.java:101) ~[?:1.7.0_79]
????at java.io.FileReader.<init>(FileReader.java:58) ~[?:1.7.0_79]
????at FooClass.main(FooClass.java:47)
????%xEx?е?????????????????????????????????????????????????????????????????磺
????// ...
????} catch (FileNotFoundException ex) {
????logger.error(“Unable to open file!”?? ex);
????}
?????????????????????????????%ex???????????????????????????
????[%p] %t: %m%ex
??????????????????????%xEx(none)????????????Log4j???????е????????
????[%p] %t: %m%xEx{none}
??????????Logback?????%nopex??
????[%p] %t: %m%nopex
??????y??????????????????
??????????“????????????????”??????????????????????????????y??????????????????????????JSON??XML?? ??Щ???????????????????????????????з????????????????????????????????????????????С?????????????????????????JSON??????????????????£?
????...
????"loggerName" : "FooClass"??
????"message" : "Foo?? oh no! "??
????"thrown" : {
????"commonElementCount" : 0??
????"localizedMessage" : "foo.file (No such file or directory)"??
????"message" : "foo.file (No such file or directory)"??
????"name" : "java.io.FileNotFoundException"??
????"extendedStackTrace" : [ {
????"class" : "java.io.FileInputStream"??
????"method" : "open"??
????"file" : "FileInputStream.java"??
????...
???????δ??????
???????????£???????????????????????????????????б????????????????3??????????????????κ?????????????????????????????????????????e???????????????????????????????????????????????????ν?????????????????????????Щ????
????Thread??????????????????????????????δ?????????????ExceptionHandler??
????setDefaultUncaughtExceptionHandler ???????????κ??????????κ?????setUncaughtExceptionHandler???????????????????????趨??????????????????ThreadGroup?????????趨?????????????????????????????????????
?????????????????????趨?????????????????????????????????????????????????UncaughtExceptionHandler??
????import java.util.logging.*;
????public class ExceptionDemo {
????private static final Logger logger = Logger.getLogger(ExceptionDemo.class);
????public static void main(String[] args) {
????Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
????public void uncaughtException(Thread t?? Throwable e) {
????logger.log(Level.SEVERE?? t + " ExceptionDemo threw an exception: "?? e);
????};
????});
????class adminThread implements Runnable {
????public void run() {
????throw new RuntimeException();
????}
????}
????Thread t = new Thread(new adminThread());
????t.start();
????}
????}
?????????????δ????????????????
????May 29?? 2015 2:21:15 PM ExceptionDemo$1 uncaughtException
????SEVERE: Thread[Thread-1??5??main] ExceptionDemo threw an exception:
????java.lang.RuntimeException
????at ExceptionDemo$1adminThread.run(ExceptionDemo.java:15)
????at java.lang.Thread.run(Thread.java:745)
????JSON
????JSON??JavaScript Object Notation????????????洢?????????????????????洢?????????????????HashMap????Hashtable??JSON???е????????????????????????????????????????????????????????????????????
????JSON?????????????????????????????????????????????null?????磬?????????????JSON????????????????
????{
????"manufacturer": "Dell"??
????"model": "Inspiron"??
????"hardware": {
????"cpu": "Intel Core i7"??
????"ram": 16384??
????“cdrom”: null
????}??
????"peripherals": [
????{
????"type": "monitor"??
????"manufacturer": "Acer"??
????"model": "S231HL"
????}
????]
????}
????JSON???????????????????洢???????????JSON??Java?????????κ??????JSON??????????????????????????????????????JSON???????????????????????
????Java?е?JSON
????????Java??????д?????JSON?????????????JSON.simple??JSON.simple??????????????????????????????JSON?????
??????????????computer?????????????Java????????????????ж??JSON????????????JSON.simple??????????Object??????????????Object?????JSONObject??
????Object computer = JSONValue.parse(new FileReader("computer.json"));
????JSONObject computerJSON = (JSONObject)computer;
?????????????ü???????????????????κ???????????????JSONObject??JSONObject??????????toString()?????? ???????JSON??????????
????2015-05-06 14:54:32??878 INFO  JSONTest main {"peripherals":[{"model":"S231HL"??"manufacturer":"Acer"??"type":"monitor"}]??"model":"Inspiron"??"hardware":{"cdrom":null??"ram":16384??"cpu":"Intel Core i7"}??"manufacturer":"Dell"}
?????????????????????????JSONObject???????????y?????Layouts??????JSONLayout????XMLLayout?????????????????????
????...
????"message" : "{"peripherals":[{"model":"S231HL"??"manufacturer":"Acer"??"type":"monitor"}]??"model":"Inspiron"??"hardware":{"cdrom":null??"ram":16384??"cpu":"Intel Core i7"}??"manufacturer":"Dell"}"??
????...
????Log4j?е?JSONLayout???????????????JSON???????????????????????Layout????????????JSONObject??Σ????Layout???л????滻JSONLayout?????????????????????????????????????????????????????Щ???????????????????????????????Layout????JSONObject?洢??message????У???????????????????????String????????????????????????JSON????洢?????????У?????????????????????洢?????????????С?
????????JSON??
????????JSON.simple??Java?л??к??????JSON??JSON-java????JSON???????????????ο??????????????????Щ?????????????????????????????web??????????JSON-java???????????????????????
?????????JSON?????????Java????????????????Google???????Gson?????Gson???????????? toJson() ?? fromJson() ??????????JSON?????????????????????Java?????????JSON??????????JSON??????????Java????Gson????????????????????У????????????Щ???????????????
????Jackson
????Jackson?????????????е??????????????????Java?й???JSON???????Щ??????????Jackson????????JSONLayouts???????????????????Jackson???????????????????????????????
????Logback???logback-jackson??logback-json-classic??????Jackson?????????????logback-contrib??????????????????Jackson?????????????JSON???????????κ?Appender?С?
????Logback Wiki???????????ν?JSON????logback?У???Wiki????е?????????LogglyAppender????????????????????????Appender??????????????????ν?JSON?????????????д?????myLog.json??????У?
????...
????<appender name="file" class="ch.qos.Logback.core.FileAppender">
????<file>myLog.json</file>
????<encoder class="ch.qos.Logback.core.encoder.LayoutWrappingEncoder">
????<layout class="ch.qos.Logback.contrib.json.classic.JsonLayout">
????<jsonFormatter class="ch.qos.Logback.contrib.jackson.JacksonJsonFormatter"/>
????</layout>
????</encoder>
????</appender>
????...