{"id":22842,"date":"2024-11-21T12:16:34","date_gmt":"2024-11-21T04:16:34","guid":{"rendered":"https:\/\/fwq.ai\/blog\/22842\/"},"modified":"2024-11-21T12:16:34","modified_gmt":"2024-11-21T04:16:34","slug":"laravel%e6%8a%a5%e9%94%99%e6%80%8e%e4%b9%88%e6%8e%92%e6%9f%a5","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/22842\/","title":{"rendered":"laravel\u62a5\u9519\u600e\u4e48\u6392\u67e5"},"content":{"rendered":"<p>\u5728\u4f7f\u7528laravel\u6846\u67b6\u8fdb\u884c\u5f00\u53d1\u8fc7\u7a0b\u4e2d\uff0c\u96be\u514d\u4f1a\u51fa\u73b0\u5404\u79cd\u9519\u8bef\u6216\u5f02\u5e38\u3002\u5982\u4f55\u5feb\u901f\u6392\u67e5\u9519\u8bef\u5e76\u89e3\u51b3\u95ee\u9898\uff0c\u662f\u57fa\u7840\u5f00\u53d1\u8005\u9762\u4e34\u7684\u6311\u6218\u3002\u672c\u6587\u5c06\u4e3a\u60a8\u4ecb\u7ecdlaravel\u62a5\u9519\u7684\u6392\u67e5\u65b9\u6cd5\uff0c\u5e2e\u52a9\u60a8\u5728\u5f00\u53d1\u8fc7\u7a0b\u4e2d\u4e8b\u534a\u529f\u500d\u3002<\/p>\n<p>\u4e00\u3001\u65e5\u5fd7\u8bb0\u5f55<\/p>\n<p>Laravel\u4f1a\u5c06\u65e5\u5fd7\u4fe1\u606f\u6309\u7167\u4e0d\u540c\u7b49\u7ea7\uff08debug\u3001info\u3001notice\u3001warning\u3001error\u3001critical\u3001alert\u3001emergency\uff09\u5206\u522b\u8bb0\u5f55\u5728\u4e0d\u540c\u6587\u4ef6\u4e2d\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u67e5\u770b\u65e5\u5fd7\u6587\u4ef6\u6765\u5feb\u901f\u627e\u51fa\u4ee3\u7801\u4e2d\u51fa\u73b0\u7684\u9519\u8bef\u3002<\/p>\n<p>1.1 \u67e5\u770b\u65e5\u5fd7\u6587\u4ef6<\/p>\n<p>\u6253\u5f00\u9879\u76ee\u6839\u76ee\u5f55\u4e2d\u7684storage\/logs\u76ee\u5f55\uff0c\u53ef\u4ee5\u770b\u5230laravel.log\u6587\u4ef6\uff0c\u8fd9\u4e2a\u662f\u7cfb\u7edf\u6240\u6709\u7ea7\u522b\u65e5\u5fd7 \u7684\u603b\u65e5\u5fd7\u6587\u4ef6\uff0c\u5982\u679c\u60f3\u4ee5\u67d0\u79cd\u7ea7\u522b\u6765\u5206\u96c6\u65e5\u5fd7\u8bb0\u5f55\uff0c\u53ef\u5728config\/logging.php \u6587\u4ef6\u4e2d\u8fdb\u884c\u914d\u7f6e\uff1b<\/p>\n<p>\u5728\u65e5\u5fd7\u6587\u4ef6\u4e2d\uff0c\u53ef\u4ee5\u9488\u5bf9\u65f6\u95f4\u3001\u8fdb\u7a0b\u3001\u7ea7\u522b\u7b49\u6761\u4ef6\u8fdb\u884c\u7b5b\u9009\u67e5\u770b\u9519\u8bef\u4fe1\u606f\u3002\u5177\u4f53\u4f7f\u7528\u65b9\u5f0f\u5982\u4e0b\uff1a<\/p>\n<pre>tail -f storage\/logs\/laravel.log \/\/ \u76d1\u63a7\u6700\u65b0\u7684\u65e5\u5fd7\u5185\u5bb9\uff0c\u6309Ctrl+C\u505c\u6b62\ncat storage\/logs\/laravel.log | grep '\u5173\u952e\u5b57' \/\/ \u641c\u7d22\u5305\u542b\"\u5173\u952e\u5b57\"\u7684\u65e5\u5fd7\u5185\u5bb9\ntail -n 100 storage\/logs\/laravel.log | grep '\u5173\u952e\u5b57' \/\/ \u67e5\u770b\u6700\u540e100\u884c\u65e5\u5fd7\u662f\u5426\u5305\u542b\"\u5173\u952e\u5b57\"\n<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>1.2 \u7ea7\u522b\u8bbe\u7f6e<\/p>\n<p>\u901a\u8fc7 Laravel \u81ea\u5e26\u7684\u76ee\u5f55 app\/Exceptions\/Handler.php \u7c7b\uff0c\u53ef\u4ee5\u770b\u5230\u65e5\u5fd7\u7684\u7ea7\u522b\u8bbe\u7f6e\u5728\u4e86 report() \u51fd\u6570\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u4e2d\uff0cLaravel \u9ed8\u8ba4\u7684\u65e5\u5fd7\u7ea7\u522b\u4e3a error\uff0c\u8fd9\u610f\u5473\u7740\uff0c\u53ea\u6709 error \u7ea7\u522b\u53ca\u4ee5\u4e0a\u7684\u65e5\u5fd7\u4f1a\u88ab\u8bb0\u5f55\u5728 app\/public\/storage\/logs\/laravel.log \u65e5\u5fd7\u6587\u4ef6\u4e2d\u3002<\/p>\n<p>\u5982\u679c\u60f3\u8981\u5c06 warning \u7ea7\u522b\u53ca\u4ee5\u4e0a\u7684\u65e5\u5fd7\u90fd\u5199\u5165\u65e5\u5fd7\u6587\u4ef6\uff0c\u53ef\u4ee5\u5728report() \u51fd\u6570\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u4e2d\u6dfb\u52a0\u8d77\u59cb\u7ea7\u522b\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre>public function report(Exception $exception)\n{\n    if ($this-&amp;gt;shouldReport($exception)) {\n        Log::channel('daily')-&amp;gt;warning('\u9047\u5230\u9519\u8bef\u5566\uff01', [\n            'error' =&amp;gt; $exception-&amp;gt;getMessage()\n        ]);\n    }\n\n    parent::report($exception);\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u901a\u8fc7Log::channel(&#8216;daily&#8217;)-&gt;warning()\u64cd\u4f5c\uff0c\u5c06\u9519\u8bef\u4fe1\u606f\u4e0e\u7ea7\u522b\u4e00\u8d77\u5199\u5165\u65e5\u5fd7\uff0c\u5e76\u901a\u8fc7shouldReport() \u65b9\u6cd5\u5224\u65ad\u662f\u5426\u9700\u8981\u5199\u5165\u65e5\u5fd7\u3002<\/p>\n<p>\u4e8c\u3001\u5f02\u5e38\u5904\u7406<\/p>\n<p>\u5728Laravel\u5e94\u7528\u4e2d\uff0c\u629b\u51fa\u81ea\u5b9a\u4e49\u5f02\u5e38\u7684\u65b9\u5f0f\u975e\u5e38\u7b80\u5355\uff0c\u53ea\u9700\u8fdb\u884c\u4ee5\u4e0b\u64cd\u4f5c\u5373\u53ef\uff1a<\/p>\n<p>2.1 \u81ea\u5b9a\u4e49\u5f02\u5e38<\/p>\n<p>\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5f02\u5e38\u7c7b\uff0c\u5e76\u7ee7\u627fLaravel\u9ed8\u8ba4\u7684Exception\u7c7b\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre>namespace AppExceptions;\nuse Exception;\n\nclass CustomException extends Exception\n{\n    public function __construct($message = null, $code = 0)\n    {\n        parent::__construct($message, $code); \/\/ \u8c03\u7528\u7236\u7c7b\u7684\u6784\u9020\u65b9\u6cd5\n    }\n     public function report()\n    {\n        \/\/ \u8bb0\u5f55\u9519\u8bef\u65e5\u5fd7 \n        Log::error('CustomException\uff1a'.$this-&amp;gt;getMessage());\n    }\n\n    public function render($request)\n    {\n        return response()-&amp;gt;json(['message' =&amp;gt; $this-&amp;gt;getMessage()], Response::HTTP_INTERNAL_SERVER_ERROR);\n    }\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5728\u6784\u9020\u65b9\u6cd5\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u5b9a\u4e49\u5f02\u5e38\u4fe1\u606f\u53ca\u72b6\u6001\u7801\uff0c\u5e76\u5728 report() \u65b9\u6cd5\u4e2d\u8bb0\u5f55\u9519\u8bef\u65e5\u5fd7\uff0c\u5728 render() \u65b9\u6cd5\u4e2d\u8fd4\u56deJSON\u683c\u5f0f\u7684\u5f02\u5e38\u4fe1\u606f\u53ca\u72b6\u6001\u7801\u3002<\/p>\n<p>\u5f53\u5e94\u7528\u7a0b\u5e8f\u4e2d\u51fa\u73b0\u4e86 CustomException\u65f6\uff0cLaravel\u4f1a\u81ea\u52a8\u6267\u884c report() \u65b9\u6cd5\u5c06\u9519\u8bef\u4fe1\u606f\u8bb0\u5f55\u5230\u65e5\u5fd7\u6587\u4ef6\u4e2d\uff0c\u5e76\u901a\u8fc7 render() \u65b9\u6cd5\u8fd4\u56de\u5f02\u5e38\u4fe1\u606f\u7ed9\u524d\u7aef\u663e\u793a\u3002<\/p>\n<p>2.2 \u89e6\u53d1\u5f02\u5e38<\/p>\n<p>\u5728\u4ee3\u7801\u4e2d\u4f7f\u7528 throw new CustomException ()\u65b9\u6cd5\u5373\u53ef\u629b\u51fa\u5f02\u5e38\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre>public function test(Request $request)\n{\n    \/\/ ...\n    if (! check_validate($request-&amp;gt;all())) {\n        throw new CustomException('\u8bf7\u6c42\u53c2\u6570\u4e0d\u6b63\u786e');\n    } \n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4e09\u3001\u9519\u8bef\u8c03\u8bd5<\/p>\n<p>\u5728\u8fdb\u884c\u5f00\u53d1\u6216\u6d4b\u8bd5\u65f6\uff0c\u6709\u65f6\u9700\u8981\u5728\u4ee3\u7801\u4e2d\u6253\u51fa\u8c03\u8bd5\u4fe1\u606f\u6765\u65b9\u4fbf\u67e5\u627e\u95ee\u9898\u6240\u5728\uff0c\u800c\u5728 Laravel \u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7 dump() \u548c dd() \u4e24\u4e2a\u51fd\u6570\u6765\u5feb\u901f\u8f93\u51fa\u8c03\u8bd5\u4fe1\u606f\u548c\u4e2d\u65ad\u7a0b\u5e8f\u6267\u884c\u7ed3\u679c\u3002<\/p>\n<p>3.1 dump()\u548cdd()<\/p>\n<p>dump() \u51fd\u6570\u7528\u4e8e\u6253\u5370\u53d8\u91cf\u6216\u8005\u6570\u7ec4\u7684\u5185\u5bb9\uff0c\u4e0d\u4f1a\u4e2d\u65ad\u7a0b\u5e8f\u7684\u8fd0\u884c\u3002\u7c7b\u4f3c\u4e8ePHP\u5185\u7f6e\u7684var_dump()\u51fd\u6570\u3002<\/p>\n<p>dd() \u51fd\u6570\u4e0d\u4ec5\u4f1a\u6253\u5370\u53d8\u91cf\u6216\u8005\u6570\u7ec4\u7684\u5185\u5bb9\uff0c\u8fd8\u4f1a\u4e2d\u65ad\u7a0b\u5e8f\u7684\u6267\u884c\uff0c\u5e76\u4e14\u628a\u5904\u7406\u5230\u5f53\u524d\u4f4d\u7f6e\u7684\u4ee3\u7801\u5806\u6808\u4ee5\u53ca\u5f02\u5e38\u7684\u5177\u4f53\u4fe1\u606f\u90fd\u663e\u793a\u51fa\u6765\u3002\u7c7b\u4f3c\u4e8ePHP\u5185\u7f6edie() \u6216exit()\u51fd\u6570\u3002<\/p>\n<pre>Route::get('\/test', function () {\n    dump('Hello Laravel!');\n    return response()-&amp;gt;json(['key' =&amp;gt; 'value']);\n});\n\nRoute::get('\/test2', function () {\n    $array = ['key_one' =&amp;gt; 'value_one', 'key_two' =&amp;gt; 'value_two'];\n    dd($array);\n    return response()-&amp;gt;json(['key' =&amp;gt; 'value']);\n});\n<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7 dump() \u548c dd() \u65b9\u6cd5\u6765\u6253\u5370\u5b57\u7b26\u4e32\u548c\u6570\u7ec4\u7684\u5185\u5bb9\uff0c\u4f7f\u7528\u65b9\u5f0f\u7c7b\u4f3c\u4e8ePHP\u539f\u751f\u51fd\u6570\uff0c\u975e\u5e38\u65b9\u4fbf\u3002<\/p>\n<p>\u56db\u3001\u7ed3\u8bed<\/p>\n<p>\u5728Laravel\u5f00\u53d1\u8fc7\u7a0b\u4e2d\uff0c\u7531\u4e8e\u5404\u79cd\u539f\u56e0\uff0c\u603b\u4f1a\u51fa\u73b0\u5404\u79cd\u5404\u6837\u7684\u62a5\u9519\u4fe1\u606f\u3002\u5982\u4f55\u9ad8\u6548\u5730\u6392\u67e5\u5e76\u89e3\u51b3\u8fd9\u4e9b\u9519\u8bef\uff0c\u662f\u6bcf\u4e2a\u5f00\u53d1\u8005\u5fc5\u987b\u9762\u5bf9\u7684\u95ee\u9898\u3002\u672c\u6587\u4e2d\u6211\u4eec\u4ecb\u7ecd\u4e86Laravel\u65e5\u5fd7\u8bb0\u5f55\u3001\u5f02\u5e38\u5904\u7406\u3001\u9519\u8bef\u8c03\u8bd5\u7b49\u51e0\u4e2a\u5e38\u7528\u7684\u6392\u67e5\u65b9\u5f0f\uff0c\u5e0c\u671b\u5bf9\u60a8\u5728Laravel\u4e2d\u5f00\u53d1\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662flaravel\u62a5\u9519\u600e\u4e48\u6392\u67e5\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u4f7f\u7528laravel\u6846\u67b6\u8fdb\u884c\u5f00\u53d1\u8fc7\u7a0b\u4e2d\uff0c\u96be\u514d\u4f1a\u51fa\u73b0\u5404\u79cd\u9519\u8bef\u6216\u5f02\u5e38\u3002\u5982\u4f55\u5feb\u901f\u6392\u67e5\u9519\u8bef\u5e76\u89e3\u51b3\u95ee\u9898\uff0c\u662f\u57fa\u7840\u5f00\u53d1\u8005\u9762\u4e34\u7684\u6311\u6218\u3002\u672c\u6587\u5c06\u4e3a\u60a8\u4ecb\u7ecdlaravel\u62a5\u9519\u7684\u6392\u67e5\u65b9\u6cd5\uff0c\u5e2e\u52a9\u60a8\u5728\u5f00\u53d1\u8fc7\u7a0b\u4e2d\u4e8b\u534a\u529f\u500d\u3002 \u4e00\u3001\u65e5\u5fd7\u8bb0\u5f55 Laravel\u4f1a\u5c06\u65e5\u5fd7\u4fe1\u606f\u6309\u7167\u4e0d\u540c\u7b49\u7ea7\uff08debug\u3001info\u3001notice\u3001warning\u3001error\u3001critical\u3001alert\u3001emergency\uff09\u5206\u522b\u8bb0\u5f55\u5728\u4e0d\u540c\u6587\u4ef6\u4e2d\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u67e5\u770b\u65e5\u5fd7\u6587\u4ef6\u6765\u5feb\u901f\u627e\u51fa\u4ee3\u7801\u4e2d\u51fa\u73b0\u7684\u9519\u8bef\u3002 1.1 \u67e5\u770b\u65e5\u5fd7\u6587\u4ef6 \u6253\u5f00\u9879\u76ee\u6839\u76ee\u5f55\u4e2d\u7684storage\/logs\u76ee\u5f55\uff0c\u53ef\u4ee5\u770b\u5230laravel.log\u6587\u4ef6\uff0c\u8fd9\u4e2a\u662f\u7cfb\u7edf\u6240\u6709\u7ea7\u522b\u65e5\u5fd7 \u7684\u603b\u65e5\u5fd7\u6587\u4ef6\uff0c\u5982\u679c\u60f3\u4ee5\u67d0\u79cd\u7ea7\u522b\u6765\u5206\u96c6\u65e5\u5fd7\u8bb0\u5f55\uff0c\u53ef\u5728config\/logging.php \u6587\u4ef6\u4e2d\u8fdb\u884c\u914d\u7f6e\uff1b \u5728\u65e5\u5fd7\u6587\u4ef6\u4e2d\uff0c\u53ef\u4ee5\u9488\u5bf9\u65f6\u95f4\u3001\u8fdb\u7a0b\u3001\u7ea7\u522b\u7b49\u6761\u4ef6\u8fdb\u884c\u7b5b\u9009\u67e5\u770b\u9519\u8bef\u4fe1\u606f\u3002\u5177\u4f53\u4f7f\u7528\u65b9\u5f0f\u5982\u4e0b\uff1a tail -f storage\/logs\/laravel.log \/\/ \u76d1\u63a7\u6700\u65b0\u7684\u65e5\u5fd7\u5185\u5bb9\uff0c\u6309Ctrl+C\u505c\u6b62 cat storage\/logs\/laravel.log | grep &#8216;\u5173\u952e\u5b57&#8217; \/\/ \u641c\u7d22\u5305\u542b&#8221;\u5173\u952e\u5b57&#8221;\u7684\u65e5\u5fd7\u5185\u5bb9 tail -n 100 storage\/logs\/laravel.log | grep &#8216;\u5173\u952e\u5b57&#8217; \/\/ \u67e5\u770b\u6700\u540e100\u884c\u65e5\u5fd7\u662f\u5426\u5305\u542b&#8221;\u5173\u952e\u5b57&#8221; \u767b\u5f55\u540e\u590d\u5236 1.2 \u7ea7\u522b\u8bbe\u7f6e \u901a\u8fc7 Laravel \u81ea\u5e26\u7684\u76ee\u5f55 app\/Exceptions\/Handler.php \u7c7b\uff0c\u53ef\u4ee5\u770b\u5230\u65e5\u5fd7\u7684\u7ea7\u522b\u8bbe\u7f6e\u5728\u4e86 report() \u51fd\u6570\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u4e2d\uff0cLaravel \u9ed8\u8ba4\u7684\u65e5\u5fd7\u7ea7\u522b\u4e3a error\uff0c\u8fd9\u610f\u5473\u7740\uff0c\u53ea\u6709 error \u7ea7\u522b\u53ca\u4ee5\u4e0a\u7684\u65e5\u5fd7\u4f1a\u88ab\u8bb0\u5f55\u5728 app\/public\/storage\/logs\/laravel.log \u65e5\u5fd7\u6587\u4ef6\u4e2d\u3002 \u5982\u679c\u60f3\u8981\u5c06 warning \u7ea7\u522b\u53ca\u4ee5\u4e0a\u7684\u65e5\u5fd7\u90fd\u5199\u5165\u65e5\u5fd7\u6587\u4ef6\uff0c\u53ef\u4ee5\u5728report() \u51fd\u6570\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u4e2d\u6dfb\u52a0\u8d77\u59cb\u7ea7\u522b\uff0c\u5982\u4e0b\u6240\u793a\uff1a public function report(Exception $exception) { [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-22842","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/22842","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=22842"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/22842\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=22842"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=22842"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=22842"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}