{"id":32687,"date":"2022-12-29T05:52:25","date_gmt":"2022-12-28T21:52:25","guid":{"rendered":"http:\/\/gulass.cn\/?p=32687"},"modified":"2022-12-29T05:52:41","modified_gmt":"2022-12-28T21:52:41","slug":"open-logging-logger","status":"publish","type":"post","link":"https:\/\/gulass.cn\/open-logging-logger.html","title":{"rendered":"\u7206\u6599\u55bd\uff01\uff01\uff01\u5f00\u6e90\u65e5\u5fd7\u5e93Logger\u7684\u4f7f\u7528\u79d8\u7c4d"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u65e5\u5fd7\u5bf9\u4e8e\u5f00\u53d1\u6765\u8bf4\u662f\u975e\u5e38\u91cd\u8981\u7684\uff0c\u4e0d\u7ba1\u662f\u8c03\u8bd5\u6570\u636e\u67e5\u770b\u3001bug\u95ee\u9898\u8ffd\u8e2a\u5b9a\u4f4d\u3001\u6570\u636e\u4fe1\u606f\u6536\u96c6\u7edf\u8ba1\uff0c\u65e5\u5e38\u5de5\u4f5c\u8fd0\u884c\u7ef4\u62a4\u7b49\u7b49\uff0c\u90fd\u5927\u91cf\u7684\u4f7f\u7528\u5230\u3002\u4eca\u5929\u4ecb\u7ecd\u8457\u540d\u5f00\u6e90\u65e5\u5fd7\u5e93Logger\u7684\u4f7f\u7528\uff0c\u5e93\u7684\u5730\u5740\uff1ahttps:\/\/github.com\/orhanobut\/logger<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\"\u5f00\u6e90\u65e5\u5fd7\u5e93Logger\u7684\u4f7f\u7528\"<\/p>\n

\u5728Android Studio\u4e2d\u7684gradle\u4e2d\u52a0\u5165\uff0c\u5c31\u53ef\u4ee5\u5f15\u7528\u4f9d\u8d56logger\u5e93\uff1a<\/strong><\/div>\n
dependencies\u00a0{\r\n     compile\u00a0'com.orhanobut:logger:1.15'\r\n     }<\/pre>\n
Logger\u5e93\u80fd\u63d0\u4f9b\u7684\u529f\u80fd:<\/strong><\/div>\n
    \n
  1. \u7ebf\u7a0b\u7684\u4fe1\u606f<\/li>\n
  2. \u7c7b\u7684\u4fe1\u606f<\/li>\n
  3. \u65b9\u6cd5\u7684\u4fe1\u606f<\/li>\n
  4. \u683c\u5f0f\u6253\u5370json\u3001xml\u7b49<\/li>\n
  5. \u70b9\u51fb\u94fe\u63a5\u8df3\u8f6c\u5230\u6e90\u7801\u6253\u5370\u5904<\/li>\n<\/ol>\n
    Logger\u7684\u4f7f\u7528<\/strong><\/div>\n

    \u4f7f\u7528\u975e\u5e38\u7b80\u5355\uff1a<\/p>\n

    \r\nString\u00a0userName\u00a0=\u00a0\"Jerry\";\r\nLogger.i(userName);\r\n<\/pre>\n

    \u6253\u5370\u7684\u6548\u679c\uff1a
    \n\"\u5f00\u6e90\u65e5\u5fd7\u5e93Logger\u7684\u4f7f\u7528\"<\/p>\n

    \u65e5\u5fd7\u6548\u679c<\/p>\n

    \u8fd9\u4e2a\u6548\u679c\u683c\u5f0f\u662f\u4e0d\u662f\u5f88\u6e05\u6670\uff0c\u53ef\u4ee5\u770b\u5230\uff0c\u5f53\u524d\u6253\u5370\u5904\u7684\u7ebf\u7a0b\u540d\u3001\u65b9\u6cd5\u540d\u3001\u65b9\u6cd5\u4f4d\u7f6e\u3001\u6253\u5370\u7684\u4fe1\u606f\u3002\u540c\u65f6\u70b9\u51fb\u65b9\u6cd5\u7684\u4f4d\u7f6e\u8fd8\u53ef\u4ee5\u8df3\u8f6c\u5230\u6253\u5370\u7684\u4f4d\u7f6e\uff0c\u8fd9\u6837\u8c03\u8bd5\u8d77\u6765\u5c31\u975e\u5e38\u7684\u65b9\u4fbf\u4e86\u3002
    \n\u53ef\u4ee5\u770b\u5230\u4e0a\u56fe\u6253\u5370\u7684TAG\u662fPRETTYLOGGER\uff0c\u8fd9\u4e2a\u662fLogger\u9ed8\u8ba4\u7684tag\uff0c\u5982\u679c\u8981\u4fee\u6539\u53ef\u4ee5\uff1a<\/p>\n

    \/\/\u00a0\u4fee\u6539\u6253\u5370\u7684tag\u503c\r\nLogger.init(\"MainActivity\");\r\nString\u00a0userName\u00a0=\u00a0\"Jerry\";\r\nLogger.i(userName);\r\n<\/pre>\n

    \"\u5f00\u6e90\u65e5\u5fd7\u5e93Logger\u7684\u4f7f\u7528\"<\/p>\n

    \u4fee\u6539tag\u7684\u65e5\u5fd7\u6548\u679c<\/p>\n

    \u6709\u7684\u5c0f\u4f19\u4f34\u89c9\u5f97\uff0c\u6211\u4e0d\u60f3\u4e00\u76f4\u90fd\u53ea\u7528\u4e00\u4e2atag\uff0c\u90a3\u5c82\u4e0d\u662f\u8981\u5199\u5f88\u591a\u7684Logger.init(tag)\u6765\u4fee\u6539\uff0c\u6211\u4eec\u770b\u65b9\u6cd5\u540d\u53ebinit\uff0c\u4f5c\u8005\u7684\u610f\u601d\u4f30\u8ba1\u662f\u53ea\u7528\u4e00\u6b21\u5c31\u597d\u4e86\u3002\u4e0b\u9762\u53ef\u4ee5\u901a\u8fc7\uff1a<\/p>\n

    Logger.init(\"MainActivity\");\r\nString\u00a0userName\u00a0=\u00a0\"Jerry\";\r\nLogger.i(userName);\r\n\/\/\u00a0\u7ed9\u5f53\u524d\u6253\u5370\u7684\u6362\u4e00\u4e2a\u5355\u72ec\u7684tag\u540d\r\nLogger.t(\"App\").i(userName);\r\nLogger.e(userName);\r\n<\/pre>\n

    \"\u5f00\u6e90\u65e5\u5fd7\u5e93Logger\u7684\u4f7f\u7528\"<\/p>\n

    \u00a0\u5355\u72ec\u4fee\u6539\u7684tag\u6253\u5370\u6548\u679c<\/p>\n

    \u4ece\u56fe\u4e0a\u53ef\u4ee5\u770b\u51fa\uff0c\u4f7f\u7528Logger.t(tag)\u8fd9\u4e2a\u65b9\u6cd5\u5c31\u4fee\u6539\u4e86tag\uff0c\u800c\u4e14\u8fd8\u4e0d\u4f1a\u5f71\u54cd\u540e\u9762\u5176\u5b83\u7684\u6253\u5370\u7684tag\uff0c\u662f\u4e0d\u662f\u7075\u6d3b\u53c8\u65b9\u4fbf\u5462\u3002<\/p>\n

    \u6211\u4eec\u7ee7\u7eed\u770b\uff0c\u6211\u4eec\u90fd\u77e5\u9053android\u81ea\u5e26\u7684Log\u65e5\u5fd7\uff0c\u662f\u65e0\u6cd5\u76f4\u63a5\u6253\u5370\u9664\u4e86\u5b57\u7b26\u4e32\u4ee5\u4e3a\u7684\u503c\u6216\u53d8\u91cf\u5bf9\u8c61\u7684\u3002
    \n\"\u5f00\u6e90\u65e5\u5fd7\u5e93Logger\u7684\u4f7f\u7528\"<\/p>\n

    \u00a0\u7cfb\u7edf\u81ea\u5e26\u7684\u6253\u5370int\uff0c\u62a5\u9519<\/p>\n

    \u6709\u7684\u65f6\u5019\u786e\u5b9e\u633a\u9ebb\u70e6\uff0c\u8fd8\u5f97\u53ef\u4ee5\u62fc\u63a5\u6210\u5b57\u7b26\u4e32\u6765\u6253\u5370(\u5b9d\u5b9d(\u3065\uff61\u25d5\u203f\u203f\u25d5\uff61)\u3065\u5fc3\u7406\u7684\u82e6\u8eab\u4e3aAndroid\u653b\u57ce\u72ee\u90fd\u4f53\u4f1a\u8fc7)\uff0c\u5f3a\u5927\u7684Logger\u53ef\u4ee5\u529e\u5230\uff1a<\/p>\n

    Logger.i(\"\u5927\u5bb6\u597d\uff0c\u6211\u53eb%s\uff0c\u4eca\u5e74%d\uff0c\u5f88\u9ad8\u5174\u5927\u5bb6\u6765\u770b\u6211\u7684\u6587\u7ae0!!!\", \"Jerry\", 18);<\/pre>\n

    \"\u5f00\u6e90\u65e5\u5fd7\u5e93Logger\u7684\u4f7f\u7528\"<\/p>\n

    \u00a0\u62fc\u63a5int\u6570\u503c\u6253\u5370\u6548\u679c<\/p>\n

    \u5b66\u8fc7C\u8bed\u8a00\u7684\u5e94\u8be5\u90fd\u77e5\u9053\uff1aprintf(\"age: %d\", 16)\u8fd9\u6837\u7684\u6253\u5370\u51fd\u6570\uff0c\u662f\u4e0d\u662f\u6709\u4f3c\u66fe\u76f8\u8bc6\u7684\u611f\u89c9\u3002\u518d\u6765\u6539\u6539\uff1a<\/p>\n

    Logger.t(\"\u4f60\u7279\u4e48\u624d16\u5c81\u4e48\").i(\"\u5927\u5bb6\u597d\uff0c\u6211\u53eb%s\uff0c\u4eca\u5e74%d\u5c81\uff0c\u5f88\u9ad8\u5174\u5927\u5bb6\u6765\u770b\u6211\u7684\u6587\u7ae0!!!\", \"Jerry\", 16);<\/pre>\n

    \"\u5f00\u6e90\u65e5\u5fd7\u5e93Logger\u7684\u4f7f\u7528\"<\/p>\n

    \u00a0\u53ef\u601c\u7684\u65e5\u5fd7\u541b\uff0c\u6709\u79cd\u88ab\u73a9\u574f\u7684\u611f\u89c9<\/p>\n

    \u9664\u4e86\u8fd9\u4e9b\uff0cLogger\u8fd8\u53ef\u4ee5\u6253\u5370\u5f88\u591a\u5f62\u5f0f\u7684\u6570\u636e\uff0c\u6781\u5927\u7684\u65b9\u4fbf\u4e86\u6211\u4eec\u7684\u5f00\u53d1\uff1a<\/p>\n

    Logger.d(\"hello\");\r\nLogger.e(\"hello\");\r\nLogger.w(\"hello\");\r\nLogger.v(\"hello\");\r\nLogger.wtf(\"hello\");\r\n\/\/\u00a0\u6253\u5370json\u683c\u5f0f\r\nString\u00a0json\u00a0=\u00a0createJson().toString();\r\nLogger.json(json);\r\n\/\/\u00a0\u6253\u5370xml\u683c\u5f0f\r\nLogger.xml(XML_CONTENT);\r\n\/\/\u00a0\u6253\u5370\u81ea\u5b9a\u4e49\u7ea7\u522b\u3001tag\u3001\u4fe1\u606f\u7b49\u683c\u5f0f\u65e5\u5fd7\r\nLogger.log(DEBUG,\u00a0\"tag\",\u00a0\"message\",\u00a0throwable);\r\n\/\/\u00a0\u521b\u5efajson\u6570\u636e\r\nprivate\u00a0JSONObject\u00a0createJson()\u00a0{\r\ntry\u00a0{\r\n      JSONObject\u00a0person\u00a0=\u00a0new\u00a0JSONObject();\r\n     \u00a0person.put(\"phone\",\u00a0\"12315\");\r\n      JSONObject\u00a0address\u00a0=\u00a0new\u00a0JSONObject();\r\n      address.put(\"country\",\u00a0\"china\");\r\n      address.put(\"province\",\u00a0\"fujian\");\r\n      ddress.put(\"city\",\u00a0\"xiamen\");\r\n      person.put(\"address\",\u00a0address);\r\n      person.put(\"married\",\u00a0true);\r\n      return\u00a0person;\r\n   \u00a0}\u00a0\r\ncatch\u00a0(JSONException\u00a0e)\u00a0{\r\n            Logger.e(e,\u00a0\"create\u00a0json\u00a0error\u00a0occured\");\r\n            \u00a0}\r\n\u00a0return\u00a0null;\r\n                        }\r\n                                }<\/pre>\n

    \"\u5f00\u6e90\u65e5\u5fd7\u5e93Logger\u7684\u4f7f\u7528\"<\/p>\n

    \u00a0\u6f02\u4eae\u7684json\u683c\u5f0f\u6253\u5370\u6548\u679c<\/p>\n

    \u5173\u4e8e\u6253\u5370json\uff0c\u8fd8\u6709\u4e2a\u5c0f\u63d2\u66f2\uff0c\u535a\u4e3b\u7b2c\u4e00\u6b21\u4f7f\u7528\u7684\u65f6\u5019\uff0c\u534a\u5929\u6ca1\u6709\u51fa\u6548\u679c\uff0c\u6ca1\u6253\u5370\u51fa\u6765\uff0c\u4e8e\u662f\u6211\u6539\u6210\u4e86DDMS\u7684Logcat\uff0c\u8fd8\u662f\u4e00\u6837\u3002\u540e\u6765\u53bb\u8ddf\u8e2a\u6e90\u7801\uff0c\u770bLogger.json()\u662f\u600e\u4e48\u6253\u5370\u7684\uff0c\u6e90\u7801\u6700\u7ec8\u662f\u4f7f\u7528\u7cfb\u7edf\u7684Log.d(tag, msg)\u6765\u6253\u5370\u7684\u3002\u4e8e\u662f\u535a\u4e3b\u7528\u6253\u65ad\u70b9debug\uff0c\u53d1\u73b0\u4e0a\u56fe\u683c\u5f0f\u7684\u6240\u6709\u5b57\u7b26\u4e32\u90fd\u6709\u4f20\u5165\u5230Log.d\u7684msg\u4e2d\uff0c\u5c31\u662f\u4e0d\u6253\u5370(\u5fc3\u7d2f)\uff0c\u4e8e\u662f\u6211\u76f4\u63a5\uff1a<\/p>\n

    Log.d(\"MainActivity\",\u00a0\"onActivityCreated:\u00a0log.d\u8dd1\u54ea\u91cc\u53bb\u4e86...\");<\/pre>\n

    \u65e5\u5fd7\u8fd8\u662f\u4e0d\u663e\u793a\uff0c\u8fd9\u5c31\u8bf4\u660e\u6709\u53ef\u80fd\u662f\u6211\u7684\u9b45\u65cfMX4\u624b\u673a\u7684\u95ee\u9898\uff0cGoogle\u4e86\u4e00\u4e0b\uff0c\u679c\u7136\u6709\u8bf4\u9b45\u65cf\u624b\u673aLog.d()\u65e5\u5fd7\u4e0d\u6253\u5370\u7684\u95ee\u9898\uff0c\u662f\u56e0\u4e3a\u9b45\u65cf\u9ed8\u8ba4\u4e0d\u5f00\u542fDebug\u6a21\u5f0f\u7684\u65e5\u5fd7\u6253\u5370\uff0c\u9700\u8981\u5728\u5f00\u53d1\u8005\u9009\u9879---\u9ad8\u7ea7\u65e5\u5fd7\u8f93\u51fa---\u9009\u62e9\u5168\u90e8\u5141\u8bb8(\u53c2\u8003\u89e3\u51b3\u6587\u7ae0\uff1ahttp:\/\/blog.csdn.net\/u013175701\/article\/details\/51428870)\uff0c\u81f3\u6b64\u65e5\u5fd7\u6253\u5370\u51fa\u6765\u4e86\u3002<\/p>\n

    Logger\u5e93\u8fd8\u9488\u5bf9\u663e\u793a\u53ef\u4ee5\u81ea\u5df1\u81ea\u5b9a\u4e49\uff1a<\/p>\n