{"id":1003,"date":"2015-10-19T15:14:17","date_gmt":"2015-10-19T07:14:17","guid":{"rendered":"http:\/\/ufqi.com\/blog\/?p=1003"},"modified":"2015-10-19T15:17:27","modified_gmt":"2015-10-19T07:17:27","slug":"a-debug-approach","status":"publish","type":"post","link":"https:\/\/ufqi.com\/blog\/a-debug-approach\/","title":{"rendered":"\u4e00\u79cddebug\u65b9\u6cd5\u7684\u5b9e\u73b0"},"content":{"rendered":"<p>\u636e\u8bf4\u7a0b\u5e8f\u7684\u5f00\u53d1\u6210\u672c\u536020%\uff0c\u7ef4\u62a4\u6210\u672c\u536080%\uff0c\u800c\u7ef4\u62a4\u7684\u4e3b\u8981\u65b9\u5f0f\u4e4b\u4e00\u4f9d\u9760\u7684\u662flog\u8f93\u51fa\u3002<\/p>\n<p>\u8f93\u51falog\u6709\u5f88\u591a\u65b9\u5f0f\uff0c\u5404\u79cd\u5f00\u53d1\u8bed\u8a00\u90fd\u6709\u4e0d\u540c\u7684\u8bbe\u65bd\u6765\u6ee1\u8db3\u8fd9\u4e00\u529f\u80fd\u3002<\/p>\n<p>\u5728PHP\u4e2d\uff0c\u53ef\u4ee5\u8fd9\u4e48\u5b9e\u73b0\u4e00\u4e2adebug\u529f\u80fd\u3002\u5176\u4e3b\u8981\u529f\u80fd\u662f\u8f93\u51fa\u4ec0\u4e48\u65f6\u95f4\u5728\u4ec0\u4e48\u4f4d\u7f6e\uff08\u7a0b\u5e8f\u540d\u3001\u51fd\u6570\u540d\uff09\u8f93\u51fa\u4e86\u6807\u8bb0\u4e3axxx\u7684\u67d0\u4e2a\u5bf9\u8c61$obj\u7684\u6253\u5370\u5f62\u5f0f\u7684\u63cf\u8ff0\u4fe1\u606f\u3002<\/p>\n<p>function debug($obj, $tag=null, $output=null);<br \/>\n# $obj \u662f\u5f85\u8f93\u51fa\u548c\u8ddf\u8e2a\u7684\u5bf9\u8c61\uff0c\u53ef\u80fd\u662fstring\uff0c\u4e5f\u53ef\u80fd\u662farray\u3001hash\u7b49\u4e0d\u540c\u7684\u6570\u636e\u7ed3\u6784\uff1b<br \/>\n# $tag \u662f\u7528\u4e8e\u6807\u8bb0\u5f85\u8f93\u51fa\u5bf9\u8c61\u7684\u5b57\u7b26\u4e32\uff0c\u4e00\u822c\u7528\u4e8e\u8fc7\u6ee4\u76f8\u5173\u8f93\u51fa\u5185\u5bb9\uff1b\u9ed8\u8ba4 $tag \u4e3anull;<br \/>\n# $output \u7c7b\u4f3cloglevel, \u4f46\u53c8\u4e0d\u540c\uff0c\u901a\u5e38 $output==null, \u8868\u793a\u5728\u540e\u5929\u8f93\u51fa\u76f8\u5173\u65e5\u5fd7\uff0c\u4e0e error_log \u51fd\u6570\u7c7b\u4f3c\uff1b $output==1\u65f6\uff0c\u5728\u540e\u53f0\u548c\u524d\u7aef\u540c\u4e8b\u8f93\u51fa\u76f8\u5173\u6d4b\u8bd5\u4fe1\u606f\uff1b$output==2\u65f6\uff0c\u6839\u636ePHP\u4e2d\u7684backtrace\u4fe1\u606f\uff0c\u8f93\u51fa\u66f4\u591a\u5185\u5bb9\u3002<br \/>\n# \u8c03\u7528\u4f8b\u5b50:<br \/>\n# debug($aHash); # \u5728\u540e\u53f0\u8f93\u51fa\u4e00\u4e2ahash\u53d8\u91cf\u7684\u5185\u5bb9\uff1b\u4f7f\u7528 <a href=\"http:\/\/ufqi.com\/naturedns\/?q=-gwa2\" target=\"_blank\">-GWA2<\/a> \u7684 WebApp-&gt;toString \u65b9\u6cd5\u6216\u8005 -PHP \u5185\u7f6e\u7684 serialize ;<br \/>\n# debug($aHash, &#8216;aHash&#8217;); \u00a0# \u5728\u8f93\u51fa\u4e2d\u589e\u52a0\u8bc6\u522b\u6807\u8bb0\u5b57\u7b26 &#8216;aHash&#8217;;<br \/>\n# debug($aHash, &#8216;aHash&#8217;, 2); # \u540c\u65f6\u5728\u524d\u7aef\u548c\u540e\u53f0\u8f93\u51fa\u76f8\u5173\u5185\u5bb9\uff0c\u7136\u540e\u9644\u5e26 -PHP \u7684backtrace\u4fe1\u606f.<\/p>\n<p>\u6e90\u7801 source\uff1a<\/p>\n<p># write log in a simple approach<br \/>\n# by wadelau@ufqi.com, Sat Oct 17 17:38:26 CST 2015<br \/>\n# e.g.<br \/>\n# debug($user);<br \/>\n# debug($user, &#8216;userinfo&#8217;); # with tag &#8216;userinfo&#8217;<br \/>\n# debug($user, &#8216;userinfo&#8217;, 1); # with tag &#8216;userinfo&#8217; and in backend and frontend<br \/>\nfunction debug($obj, $tag=&#8221;, $output=null){<br \/>\n$caller = debug_backtrace();<br \/>\nif(is_array($obj)){<br \/>\nif(isset($user)){<br \/>\n$s .= $user-&gt;toString($obj);<br \/>\n}<br \/>\nelse{<br \/>\n$s .= serialize($obj);<br \/>\n}<br \/>\n}<br \/>\nelse{<br \/>\n$s .= $obj;<br \/>\n}<\/p>\n<p>if($tag != &#8221;){<br \/>\n$s = &#8221; $tag:[$s]&#8221;;<br \/>\n}<br \/>\n$callidx = count($caller) &#8211; 2;<br \/>\n$s .= &#8216; func:[&#8216;.$caller[$callidx][&#8216;function&#8217;].&#8217;] file:[&#8216;.$caller[$callidx][&#8216;file&#8217;].&#8217;]&#8217;;<\/p>\n<p>if($output != null){<br \/>\nif($output == 0){ # in backend only<br \/>\nerror_log($s);<br \/>\n}<br \/>\nelse if($output == 1){ # in backend and frontend<br \/>\nerror_log($s);<br \/>\nprint $s;<br \/>\n}<br \/>\nelse if($output == 2){ # in backend and frontend with backtrace<br \/>\n$s .= &#8221; backtrace:[&#8220;.serialize($caller).&#8221;]&#8221;;<br \/>\nerror_log($s);<br \/>\nprint $s;<br \/>\n}<br \/>\n}<br \/>\nelse{<br \/>\nerror_log($s); # default mode<br \/>\n}<\/p>\n<p>}<\/p>\n<p>\u5176\u4ed6\u7248\u672c\u7684\u8bed\u8a00\u4e5f\u53ef\u53c2\u8003\u6b64\u884c\u4e3a\uff0c\u81ea\u884c\u5b9e\u73b0\u3002\u8fd9\u4e00\u529f\u80fd\u5df2\u7ecf\u66f4\u65b0\u5230\u65b0\u7248\u7684 <a href=\"http:\/\/ufqi.com\/naturedns\/?q=-gmis\" target=\"_blank\">-gMIS<\/a> \u548c -GWA2 \u7684\u6e90\u7801\u4e2d.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u636e\u8bf4\u7a0b\u5e8f\u7684\u5f00\u53d1\u6210\u672c\u536020%\uff0c\u7ef4\u62a4\u6210\u672c\u536080%\uff0c\u800c\u7ef4\u62a4\u7684\u4e3b\u8981\u65b9\u5f0f\u4e4b\u4e00\u4f9d\u9760\u7684\u662flog &hellip; <a href=\"https:\/\/ufqi.com\/blog\/a-debug-approach\/\">\u7ee7\u7eed\u9605\u8bfb <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[41,42,7,6,2],"tags":[72,74,75,73],"_links":{"self":[{"href":"https:\/\/ufqi.com\/blog\/wp-json\/wp\/v2\/posts\/1003"}],"collection":[{"href":"https:\/\/ufqi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ufqi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ufqi.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/ufqi.com\/blog\/wp-json\/wp\/v2\/comments?post=1003"}],"version-history":[{"count":4,"href":"https:\/\/ufqi.com\/blog\/wp-json\/wp\/v2\/posts\/1003\/revisions"}],"predecessor-version":[{"id":1005,"href":"https:\/\/ufqi.com\/blog\/wp-json\/wp\/v2\/posts\/1003\/revisions\/1005"}],"wp:attachment":[{"href":"https:\/\/ufqi.com\/blog\/wp-json\/wp\/v2\/media?parent=1003"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ufqi.com\/blog\/wp-json\/wp\/v2\/categories?post=1003"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ufqi.com\/blog\/wp-json\/wp\/v2\/tags?post=1003"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}