{"id":23559,"date":"2024-11-21T12:45:45","date_gmt":"2024-11-21T04:45:45","guid":{"rendered":"https:\/\/fwq.ai\/blog\/23559\/"},"modified":"2024-11-21T12:45:45","modified_gmt":"2024-11-21T04:45:45","slug":"swoole%e5%ae%9e%e8%b7%b5%ef%bc%9a%e9%ab%98%e6%95%88%e7%8e%87%e7%9a%84%e6%97%a5%e5%bf%97%e5%bc%82%e5%b8%b8%e5%a4%84%e7%90%86","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/23559\/","title":{"rendered":"Swoole\u5b9e\u8df5\uff1a\u9ad8\u6548\u7387\u7684\u65e5\u5fd7\u5f02\u5e38\u5904\u7406"},"content":{"rendered":"<p>\u8fd1\u5e74\u6765\uff0c\u968f\u7740\u7f51\u7edc\u5e94\u7528\u7684\u4e0d\u65ad\u53d1\u5c55\uff0c\u8d8a\u6765\u8d8a\u591a\u7684\u5f00\u53d1\u4eba\u5458\u5f00\u59cb\u4f7f\u7528\u8fd9\u4e2a\u9ad8\u6027\u80fd\u7684\u5f02\u6b65\u7f51\u7edc\u901a\u4fe1\u6846\u67b6\u6765\u63d0\u5347\u7a0b\u5e8f\u7684\u6548\u7387\u3002swoole\u4e0d\u4ec5\u53ef\u4ee5\u8ba9php\u5e94\u7528\u5b9e\u73b0\u591a\u8fdb\u7a0b\u3001\u534f\u7a0b\u5316\u3001\u5f02\u6b65io\u7b49\u9ad8\u7ea7\u7279\u6027\uff0c\u800c\u4e14\u8fd8\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u65e5\u5fd7\u3001\u5f02\u5e38\u5904\u7406\u529f\u80fd\uff0c\u53ef\u4ee5\u5e2e\u52a9\u5f00\u53d1\u4eba\u5458\u66f4\u597d\u5730\u8fdb\u884c\u8c03\u8bd5\u548c\u4f18\u5316\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528swoole\u8fdb\u884c\u9ad8\u6548\u7387\u7684\u65e5\u5fd7\u548c\u5f02\u5e38\u5904\u7406\u3002<\/p>\n<p>\u4e00\u3001Swoole\u7684\u65e5\u5fd7\u7ec4\u4ef6<\/p>\n<p>\u5728Swoole\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528SwooleCoroutineLog\u6765\u5b9e\u73b0\u65e5\u5fd7\u8bb0\u5f55\u3002\u76f8\u5bf9\u4e8e\u4f20\u7edf\u7684file_put_contents\u51fd\u6570\uff0cSwooleCoroutineLog\u6709\u4ee5\u4e0b\u4f18\u70b9\uff1a<\/p>\n<ol>\n<li>SwooleCoroutineLog\u7684\u5e95\u5c42\u4f7f\u7528\u4e86Linux\u7684aio\uff08\u5f02\u6b65IO\uff09\u7279\u6027\uff0c\u53ef\u4ee5\u8ba9\u591a\u4e2a\u534f\u7a0b\u5e76\u53d1\u5730\u5199\u5165\u65e5\u5fd7\u6587\u4ef6\uff0c\u63d0\u9ad8\u4e86\u65e5\u5fd7\u7684\u5199\u5165\u6548\u7387\u3002<\/li>\n<li>SwooleCoroutineLog\u652f\u6301\u65e5\u5fd7\u7b49\u7ea7\uff08debug\u3001info\u3001notice\u3001warning\u3001error\uff09\uff0c\u53ef\u4ee5\u6839\u636e\u4e0d\u540c\u7684\u65e5\u5fd7\u7ea7\u522b\u7b5b\u9009\u51fa\u9700\u8981\u8f93\u51fa\u7684\u65e5\u5fd7\u4fe1\u606f\u3002<\/li>\n<li>SwooleCoroutineLog\u5141\u8bb8\u5728\u4e0d\u540c\u7684\u534f\u7a0b\u4e2d\u4f7f\u7528\u540c\u4e00\u4e2aLogger\u5bf9\u8c61\uff0c\u53ef\u4ee5\u907f\u514d\u4e0d\u540c\u534f\u7a0b\u4e4b\u95f4\u5199\u5165\u540c\u4e00\u4e2a\u65e5\u5fd7\u6587\u4ef6\u65f6\u4ea7\u751f\u7684\u9501\u7ade\u4e89\u95ee\u9898\u3002<\/li>\n<\/ol>\n<p>\u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528SwooleCoroutineLog\u8fdb\u884c\u65e5\u5fd7\u8bb0\u5f55\u7684\u793a\u4f8b\uff1a<\/p>\n<pre>&lt;?php use SwooleCoroutineLog;\n\n$log = new Log('\/path\/to\/log\/file.log');\n\n$log-&gt;debug('this is a debug message');\n$log-&amp;gt;info('this is a info message');\n$log-&amp;gt;notice('this is a notice message');\n$log-&amp;gt;warning('this is a warning message');\n$log-&amp;gt;error('this is a error message');\n?&amp;gt;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4e8c\u3001Swoole\u7684\u5f02\u5e38\u5904\u7406\u7ec4\u4ef6<\/p>\n<p>\u5728Swoole\u4e2d\uff0c\u4f7f\u7528try\/catch\u8bed\u53e5\u548cSwooleCoroutineException\u7ec4\u4ef6\u6765\u5904\u7406\u5f02\u5e38\u3002SwooleCoroutineException\u662f\u4e00\u4e2a\u4e13\u95e8\u7528\u6765\u5904\u7406\u534f\u7a0b\u4e2d\u5f02\u5e38\u7684\u7ec4\u4ef6\uff0c\u5176\u4e0e\u666e\u901a\u7684PHP\u5f02\u5e38\u5904\u7406\u65b9\u5f0f\u76f8\u6bd4\uff0c\u6709\u4ee5\u4e0b\u4f18\u70b9\uff1a<\/p>\n<ol>\n<li>SwooleCoroutineException\u53ef\u4ee5\u5728\u534f\u7a0b\u5207\u6362\u65f6\u6b63\u5e38\u5de5\u4f5c\uff0c\u53ef\u4ee5\u907f\u514d\u7531\u4e8e\u534f\u7a0b\u5207\u6362\u5bfc\u81f4\u7684\u5f02\u5e38\u5904\u7406\u9519\u8bef\u3002<\/li>\n<li>SwooleCoroutineException\u53ef\u4ee5\u8bb0\u5f55\u5f02\u5e38\u53d1\u751f\u65f6\u7684\u534f\u7a0b\u3001\u884c\u53f7\u7b49\u4fe1\u606f\uff0c\u65b9\u4fbf\u5f00\u53d1\u4eba\u5458\u8fdb\u884c\u8c03\u8bd5\u3002<\/li>\n<\/ol>\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528SwooleCoroutineException\u5904\u7406\u534f\u7a0b\u5f02\u5e38\u7684\u793a\u4f8b\uff1a<\/p>\n<pre>&lt;?php use SwooleCoroutine;\nuse SwooleCoroutineException;\n\nCoroutineun(function () {\n    try {\n        \/\/ do something\n        throw new Exception('error occurs', 100);\n    } catch (Exception $e) {\n        echo $e-&gt;getMessage(), PHP_EOL;\n        echo $e-&amp;gt;getCode(), PHP_EOL;\n        echo $e-&amp;gt;getFile(), PHP_EOL;\n        echo $e-&amp;gt;getLine(), PHP_EOL;\n        echo $e-&amp;gt;getTraceAsString(), PHP_EOL;\n    }\n});\n?&amp;gt;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4e09\u3001Swoole\u7684\u65e5\u5fd7\u5f02\u5e38\u5904\u7406\u5b9e\u8df5<\/p>\n<p>\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06Swoole\u7684\u65e5\u5fd7\u7ec4\u4ef6\u548c\u5f02\u5e38\u5904\u7406\u7ec4\u4ef6\u7ed3\u5408\u8d77\u6765\u4f7f\u7528\uff0c\u8fdb\u4e00\u6b65\u63d0\u9ad8\u7a0b\u5e8f\u7684\u53ef\u9760\u6027\u548c\u6548\u7387\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528SwooleCoroutineLog\u548cSwooleCoroutineException\u8fdb\u884c\u65e5\u5fd7\u548c\u5f02\u5e38\u5904\u7406\u7684\u793a\u4f8b\uff1a<\/p>\n<pre>&lt;?php use SwooleCoroutine;\nuse SwooleCoroutineException;\nuse SwooleCoroutineLog;\n\n$log = new Log('\/path\/to\/log\/file.log');\n\nCoroutineun(function () use ($log) {\n    try {\n        \/\/ do something\n        throw new Exception('error occurs', 100);\n    } catch (Exception $e) {\n        $log-&gt;error($e-&amp;gt;getMessage(), [\n            'file' =&amp;gt; $e-&amp;gt;getFile(),\n            'line' =&amp;gt; $e-&amp;gt;getLine(),\n            'trace' =&amp;gt; $e-&amp;gt;getTraceAsString()\n        ]);\n    }\n});\n?&amp;gt;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u5f53\u7a0b\u5e8f\u51fa\u73b0\u5f02\u5e38\u65f6\uff0c\u4f1a\u5c06\u5f02\u5e38\u7684\u76f8\u5173\u4fe1\u606f\u4ee5error\u7ea7\u522b\u8bb0\u5f55\u5230\u65e5\u5fd7\u6587\u4ef6\u4e2d\u3002\u8fd9\u6837\u53ef\u4ee5\u53ca\u65f6\u53d1\u73b0\u95ee\u9898\u3001\u8fdb\u884c\u8ddf\u8e2a\u548c\u6392\u67e5\uff0c\u63d0\u9ad8\u7a0b\u5e8f\u7684\u7a33\u5b9a\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<p>\u603b\u7ed3<\/p>\n<p>Swoole\u662f\u4e00\u6b3e\u8fd0\u884c\u5728PHP\u73af\u5883\u4e2d\u7684\u9ad8\u6027\u80fd\u7f51\u7edc\u901a\u4fe1\u6846\u67b6\uff0c\u652f\u6301\u591a\u8fdb\u7a0b\u3001\u534f\u7a0b\u5316\u3001\u5f02\u6b65IO\u7b49\u7279\u6027\uff0c\u5e76\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u65e5\u5fd7\u3001\u5f02\u5e38\u5904\u7406\u7ec4\u4ef6\u3002\u901a\u8fc7\u4f7f\u7528Swoole\u7684\u65e5\u5fd7\u7ec4\u4ef6\u548c\u5f02\u5e38\u5904\u7406\u7ec4\u4ef6\uff0c\u53ef\u4ee5\u8ba9\u6211\u4eec\u66f4\u597d\u5730\u8fdb\u884c\u7a0b\u5e8f\u7684\u8c03\u8bd5\u548c\u4f18\u5316\uff0c\u63d0\u9ad8\u7a0b\u5e8f\u7684\u6548\u7387\u548c\u53ef\u9760\u6027\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fSwoole\u5b9e\u8df5\uff1a\u9ad8\u6548\u7387\u7684\u65e5\u5fd7\u5f02\u5e38\u5904\u7406\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>\u8fd1\u5e74\u6765\uff0c\u968f\u7740\u7f51\u7edc\u5e94\u7528\u7684\u4e0d\u65ad\u53d1\u5c55\uff0c\u8d8a\u6765\u8d8a\u591a\u7684\u5f00\u53d1\u4eba\u5458\u5f00\u59cb\u4f7f\u7528\u8fd9\u4e2a\u9ad8\u6027\u80fd\u7684\u5f02\u6b65\u7f51\u7edc\u901a\u4fe1\u6846\u67b6\u6765\u63d0\u5347\u7a0b\u5e8f\u7684\u6548\u7387\u3002swoole\u4e0d\u4ec5\u53ef\u4ee5\u8ba9php\u5e94\u7528\u5b9e\u73b0\u591a\u8fdb\u7a0b\u3001\u534f\u7a0b\u5316\u3001\u5f02\u6b65io\u7b49\u9ad8\u7ea7\u7279\u6027\uff0c\u800c\u4e14\u8fd8\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u65e5\u5fd7\u3001\u5f02\u5e38\u5904\u7406\u529f\u80fd\uff0c\u53ef\u4ee5\u5e2e\u52a9\u5f00\u53d1\u4eba\u5458\u66f4\u597d\u5730\u8fdb\u884c\u8c03\u8bd5\u548c\u4f18\u5316\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528swoole\u8fdb\u884c\u9ad8\u6548\u7387\u7684\u65e5\u5fd7\u548c\u5f02\u5e38\u5904\u7406\u3002 \u4e00\u3001Swoole\u7684\u65e5\u5fd7\u7ec4\u4ef6 \u5728Swoole\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528SwooleCoroutineLog\u6765\u5b9e\u73b0\u65e5\u5fd7\u8bb0\u5f55\u3002\u76f8\u5bf9\u4e8e\u4f20\u7edf\u7684file_put_contents\u51fd\u6570\uff0cSwooleCoroutineLog\u6709\u4ee5\u4e0b\u4f18\u70b9\uff1a SwooleCoroutineLog\u7684\u5e95\u5c42\u4f7f\u7528\u4e86Linux\u7684aio\uff08\u5f02\u6b65IO\uff09\u7279\u6027\uff0c\u53ef\u4ee5\u8ba9\u591a\u4e2a\u534f\u7a0b\u5e76\u53d1\u5730\u5199\u5165\u65e5\u5fd7\u6587\u4ef6\uff0c\u63d0\u9ad8\u4e86\u65e5\u5fd7\u7684\u5199\u5165\u6548\u7387\u3002 SwooleCoroutineLog\u652f\u6301\u65e5\u5fd7\u7b49\u7ea7\uff08debug\u3001info\u3001notice\u3001warning\u3001error\uff09\uff0c\u53ef\u4ee5\u6839\u636e\u4e0d\u540c\u7684\u65e5\u5fd7\u7ea7\u522b\u7b5b\u9009\u51fa\u9700\u8981\u8f93\u51fa\u7684\u65e5\u5fd7\u4fe1\u606f\u3002 SwooleCoroutineLog\u5141\u8bb8\u5728\u4e0d\u540c\u7684\u534f\u7a0b\u4e2d\u4f7f\u7528\u540c\u4e00\u4e2aLogger\u5bf9\u8c61\uff0c\u53ef\u4ee5\u907f\u514d\u4e0d\u540c\u534f\u7a0b\u4e4b\u95f4\u5199\u5165\u540c\u4e00\u4e2a\u65e5\u5fd7\u6587\u4ef6\u65f6\u4ea7\u751f\u7684\u9501\u7ade\u4e89\u95ee\u9898\u3002 \u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528SwooleCoroutineLog\u8fdb\u884c\u65e5\u5fd7\u8bb0\u5f55\u7684\u793a\u4f8b\uff1a &lt;?php use SwooleCoroutineLog; $log = new Log(&#8216;\/path\/to\/log\/file.log&#8217;); $log-&gt;debug(&#8216;this is a debug message&#8217;); $log-&amp;gt;info(&#8216;this is a info message&#8217;); $log-&amp;gt;notice(&#8216;this is a notice message&#8217;); $log-&amp;gt;warning(&#8216;this is a warning message&#8217;); $log-&amp;gt;error(&#8216;this is a error message&#8217;); ?&amp;gt; \u767b\u5f55\u540e\u590d\u5236 \u4e8c\u3001Swoole\u7684\u5f02\u5e38\u5904\u7406\u7ec4\u4ef6 \u5728Swoole\u4e2d\uff0c\u4f7f\u7528try\/catch\u8bed\u53e5\u548cSwooleCoroutineException\u7ec4\u4ef6\u6765\u5904\u7406\u5f02\u5e38\u3002SwooleCoroutineException\u662f\u4e00\u4e2a\u4e13\u95e8\u7528\u6765\u5904\u7406\u534f\u7a0b\u4e2d\u5f02\u5e38\u7684\u7ec4\u4ef6\uff0c\u5176\u4e0e\u666e\u901a\u7684PHP\u5f02\u5e38\u5904\u7406\u65b9\u5f0f\u76f8\u6bd4\uff0c\u6709\u4ee5\u4e0b\u4f18\u70b9\uff1a SwooleCoroutineException\u53ef\u4ee5\u5728\u534f\u7a0b\u5207\u6362\u65f6\u6b63\u5e38\u5de5\u4f5c\uff0c\u53ef\u4ee5\u907f\u514d\u7531\u4e8e\u534f\u7a0b\u5207\u6362\u5bfc\u81f4\u7684\u5f02\u5e38\u5904\u7406\u9519\u8bef\u3002 SwooleCoroutineException\u53ef\u4ee5\u8bb0\u5f55\u5f02\u5e38\u53d1\u751f\u65f6\u7684\u534f\u7a0b\u3001\u884c\u53f7\u7b49\u4fe1\u606f\uff0c\u65b9\u4fbf\u5f00\u53d1\u4eba\u5458\u8fdb\u884c\u8c03\u8bd5\u3002 \u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528SwooleCoroutineException\u5904\u7406\u534f\u7a0b\u5f02\u5e38\u7684\u793a\u4f8b\uff1a &lt;?php use SwooleCoroutine; use SwooleCoroutineException; Coroutineun(function () { try [&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-23559","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/23559","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=23559"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/23559\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=23559"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=23559"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=23559"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}