{"id":47264,"date":"2024-12-02T12:43:04","date_gmt":"2024-12-02T04:43:04","guid":{"rendered":"https:\/\/fwq.ai\/blog\/47264\/"},"modified":"2024-12-02T12:43:04","modified_gmt":"2024-12-02T04:43:04","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8-laravel-%e6%a1%86%e6%9e%b6%e6%89%a9%e5%b1%95-php-%e5%87%bd%e6%95%b0%e7%9a%84%e6%97%a5%e5%bf%97%e8%ae%b0%e5%bd%95%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/47264\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528 Laravel \u6846\u67b6\u6269\u5c55 PHP \u51fd\u6570\u7684\u65e5\u5fd7\u8bb0\u5f55\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>\u5982\u4f55\u4f7f\u7528 Laravel \u6846\u67b6\u6269\u5c55 PHP \u51fd\u6570\u7684\u65e5\u5fd7\u8bb0\u5f55\uff1f<\/h1>\n<p>\u5728\u6587\u7ae0\u5b9e\u6218\u5f00\u53d1\u7684\u8fc7\u7a0b\u4e2d\uff0c\u6211\u4eec\u7ecf\u5e38\u4f1a\u9047\u5230\u4e00\u4e9b\u8fd9\u6837\u90a3\u6837\u7684\u95ee\u9898\uff0c\u7136\u540e\u8981\u5361\u597d\u534a\u5929\uff0c\u7b49\u95ee\u9898\u89e3\u51b3\u4e86\u624d\u53d1\u73b0\u539f\u6765\u4e00\u4e9b\u7ec6\u8282\u77e5\u8bc6\u70b9\u8fd8\u662f\u6ca1\u6709\u638c\u63e1\u597d\u3002\u4eca\u5929\u7c73\u4e91\u5c31\u6574\u7406\u5206\u4eab\u300a\u5982\u4f55\u4f7f\u7528 Laravel \u6846\u67b6\u6269\u5c55 PHP \u51fd\u6570\u7684\u65e5\u5fd7\u8bb0\u5f55\uff1f\u300b\uff0c\u804a\u804a\uff0c\u5e0c\u671b\u53ef\u4ee5\u5e2e\u52a9\u5230\u6b63\u5728\u52aa\u529b\u8d5a\u94b1\u7684\u4f60\u3002<\/p>\n<p>Laravel \u63d0\u4f9b\u4e86\u4e00\u79cd\u65b9\u6cd5\u6765\u6269\u5c55 PHP \u51fd\u6570\u7684\u65e5\u5fd7\u8bb0\u5f55\uff1a\u5b89\u88c5 monolog\/monolog \u6269\u5c55\u3002\u5728 config\/logging.php \u4e2d\u914d\u7f6e custom \u65e5\u5fd7\u901a\u9053\u3002\u4f7f\u7528 \\Illuminate\\Support\\Facades\\Log \u95e8\u9762\u8bb0\u5f55\u81ea\u5b9a\u4e49\u65e5\u5fd7\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241026\/1729907689671c4be98e6e3.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u4f7f\u7528 Laravel \u6846\u67b6\u6269\u5c55 PHP \u51fd\u6570\u7684\u65e5\u5fd7\u8bb0\u5f55\uff1f\u63d2\u56fe\" alt=\"\u5982\u4f55\u4f7f\u7528 Laravel \u6846\u67b6\u6269\u5c55 PHP \u51fd\u6570\u7684\u65e5\u5fd7\u8bb0\u5f55\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u4f7f\u7528 Laravel \u6269\u5c55 PHP \u51fd\u6570\u7684\u65e5\u5fd7\u8bb0\u5f55<\/strong><\/p>\n<p>Laravel \u63d0\u4f9b\u4e86\u4e00\u4e2a\u7b80\u6d01\u7684\u65b9\u5f0f\u6765\u6269\u5c55 PHP \u51fd\u6570\u7684\u65e5\u5fd7\u8bb0\u5f55\uff0c\u8ba9\u4f60\u8f7b\u677e\u5730\u4e3a\u5e94\u7528\u7a0b\u5e8f\u4e2d\u7684\u81ea\u5b9a\u4e49\u65e5\u5fd7\u8bb0\u5f55\u6dfb\u52a0\u652f\u6301\u3002<\/p>\n<p><strong>\u5b89\u88c5\u6269\u5c55<\/strong><\/p>\n<p>\u5b89\u88c5 <code>monolog\/monolog<\/code> \u6269\u5c55\uff1a<\/p>\n<pre>composer require monolog\/monolog<\/pre>\n<p><strong>\u914d\u7f6e\u6269\u5c55<\/strong><\/p>\n<p>\u5728 <code>config\/logging.php<\/code> \u914d\u7f6e\u6587\u4ef6\u4e2d\uff0c\u6dfb\u52a0\u4ee5\u4e0b\u914d\u7f6e\uff1a<\/p>\n<pre>'channels' =&gt; [\n    'custom' =&gt; [\n        'driver' =&gt; 'monolog',\n        'handler' =&gt; 'custom',\n        'formatter' =&gt; env('LOG_CUSTOM_FORMATTER', 'default'),\n        'level' =&gt; env('LOG_CUSTOM_LEVEL', 'debug'),\n    ],\n],\n\n'handlers' =&gt; [\n    'custom' =&gt; [\n        'type' =&gt; 'monolog',\n        'path' =&gt; env('LOG_CUSTOM_PATH', storage_path('logs\/custom.log')),\n    ],\n],<\/pre>\n<p><strong>\u4f7f\u7528\u6269\u5c55<\/strong><\/p>\n<p>\u4f7f\u7528 <code>\\Illuminate\\Support\\Facades\\Log<\/code> \u95e8\u9762\u6765\u8bb0\u5f55\u81ea\u5b9a\u4e49\u65e5\u5fd7\uff1a<\/p>\n<pre>Log::channel('custom')-&gt;debug('Custom log message');<\/pre>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p>\u521b\u5efa\u4e00\u4e2a\u7528\u4e8e\u8bb0\u5f55 API \u8bf7\u6c42\u548c\u54cd\u5e94\u7684\u81ea\u5b9a\u4e49\u65e5\u5fd7\uff1a<\/p>\n<pre>class ApiRequestLogMiddleware\n{\n    public function handle($request, $next)\n    {\n        $start = microtime(true);\n\n        $response = $next($request);\n\n        $duration = microtime(true) - $start;\n\n        Log::channel('api')-&gt;info('Request: {method} {uri} - Duration: {duration} ms', [\n            'method' =&gt; $request-&gt;method(),\n            'uri' =&gt; $request-&gt;uri(),\n            'duration' =&gt; round($duration * 1000, 2),\n        ]);\n\n        return $response;\n    }\n}<\/pre>\n<p>\u597d\u4e86\uff0c\u672c\u6587\u5230\u6b64\u7ed3\u675f\uff0c\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u300a\u5982\u4f55\u4f7f\u7528 Laravel \u6846\u67b6\u6269\u5c55 PHP \u51fd\u6570\u7684\u65e5\u5fd7\u8bb0\u5f55\uff1f\u300b\uff0c\u5e0c\u671b\u672c\u6587\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff01\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff0c\u7ed9\u5927\u5bb6\u5206\u4eab\u66f4\u591a\u6587\u7ae0\u77e5\u8bc6\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u4f7f\u7528 Laravel \u6846\u67b6\u6269\u5c55 PHP \u51fd\u6570\u7684\u65e5\u5fd7\u8bb0\u5f55\uff1f \u5728\u6587\u7ae0\u5b9e\u6218\u5f00\u53d1\u7684\u8fc7\u7a0b\u4e2d\uff0c\u6211\u4eec\u7ecf\u5e38\u4f1a\u9047\u5230\u4e00\u4e9b\u8fd9\u6837\u90a3\u6837\u7684\u95ee\u9898\uff0c\u7136\u540e\u8981\u5361\u597d\u534a\u5929\uff0c\u7b49\u95ee\u9898\u89e3\u51b3\u4e86\u624d\u53d1\u73b0\u539f\u6765\u4e00\u4e9b\u7ec6\u8282\u77e5\u8bc6\u70b9\u8fd8\u662f\u6ca1\u6709\u638c\u63e1\u597d\u3002\u4eca\u5929\u7c73\u4e91\u5c31\u6574\u7406\u5206\u4eab\u300a\u5982\u4f55\u4f7f\u7528 Laravel \u6846\u67b6\u6269\u5c55 PHP \u51fd\u6570\u7684\u65e5\u5fd7\u8bb0\u5f55\uff1f\u300b\uff0c\u804a\u804a\uff0c\u5e0c\u671b\u53ef\u4ee5\u5e2e\u52a9\u5230\u6b63\u5728\u52aa\u529b\u8d5a\u94b1\u7684\u4f60\u3002 Laravel \u63d0\u4f9b\u4e86\u4e00\u79cd\u65b9\u6cd5\u6765\u6269\u5c55 PHP \u51fd\u6570\u7684\u65e5\u5fd7\u8bb0\u5f55\uff1a\u5b89\u88c5 monolog\/monolog \u6269\u5c55\u3002\u5728 config\/logging.php \u4e2d\u914d\u7f6e custom \u65e5\u5fd7\u901a\u9053\u3002\u4f7f\u7528 \\Illuminate\\Support\\Facades\\Log \u95e8\u9762\u8bb0\u5f55\u81ea\u5b9a\u4e49\u65e5\u5fd7\u3002 \u4f7f\u7528 Laravel \u6269\u5c55 PHP \u51fd\u6570\u7684\u65e5\u5fd7\u8bb0\u5f55 Laravel \u63d0\u4f9b\u4e86\u4e00\u4e2a\u7b80\u6d01\u7684\u65b9\u5f0f\u6765\u6269\u5c55 PHP \u51fd\u6570\u7684\u65e5\u5fd7\u8bb0\u5f55\uff0c\u8ba9\u4f60\u8f7b\u677e\u5730\u4e3a\u5e94\u7528\u7a0b\u5e8f\u4e2d\u7684\u81ea\u5b9a\u4e49\u65e5\u5fd7\u8bb0\u5f55\u6dfb\u52a0\u652f\u6301\u3002 \u5b89\u88c5\u6269\u5c55 \u5b89\u88c5 monolog\/monolog \u6269\u5c55\uff1a composer require monolog\/monolog \u914d\u7f6e\u6269\u5c55 \u5728 config\/logging.php \u914d\u7f6e\u6587\u4ef6\u4e2d\uff0c\u6dfb\u52a0\u4ee5\u4e0b\u914d\u7f6e\uff1a &#8216;channels&#8217; =&gt; [ &#8216;custom&#8217; =&gt; [ &#8216;driver&#8217; =&gt; &#8216;monolog&#8217;, &#8216;handler&#8217; =&gt; &#8216;custom&#8217;, &#8216;formatter&#8217; [&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-47264","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/47264","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=47264"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/47264\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=47264"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=47264"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=47264"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}