{"id":53604,"date":"2024-12-03T08:47:56","date_gmt":"2024-12-03T00:47:56","guid":{"rendered":"https:\/\/fwq.ai\/blog\/53604\/"},"modified":"2024-12-03T08:47:56","modified_gmt":"2024-12-03T00:47:56","slug":"logging-system-with-proxy-and-fetch","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/53604\/","title":{"rendered":"Logging System with Proxy and Fetch"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>Logging System with Proxy and Fetch<\/h1>\n<p><span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span>    <\/p>\n<p>\u5728IT\u884c\u4e1a\u8fd9\u4e2a\u53d1\u5c55\u66f4\u65b0\u901f\u5ea6\u5f88\u5feb\u7684\u884c\u4e1a\uff0c\u53ea\u6709\u4e0d\u505c\u6b62\u7684\u5b66\u4e60\uff0c\u624d\u4e0d\u4f1a\u88ab\u884c\u4e1a\u6240\u6dd8\u6c70\u3002\u5982\u679c\u4f60\u662f\u6587\u7ae0\u5b66\u4e60\u8005\uff0c\u90a3\u4e48\u672c\u6587\u300aLogging System with Proxy and Fetch\u300b\u5c31\u5f88\u9002\u5408\u4f60\uff01\u672c\u7bc7\u5185\u5bb9\u4e3b\u8981\u5305\u62ec##content_title##\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u52a9\u529b\u5b9e\u6218\u5f00\u53d1\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241202\/1733132744674d81c80370d.jpg\" class=\"aligncenter\" title=\"Logging System with Proxy and Fetch\u63d2\u56fe\" alt=\"Logging System with Proxy and Fetch\u63d2\u56fe\" \/><\/p>\n<ol>\n<li>\n<p>\u4ee3\u7406\u5bf9\u8c61\uff1afetchlogger \u5305\u88c5\u4e86 fetch \u51fd\u6570\u3002<br \/> \u5b83\u4f7f\u7528 apply trap \u6765\u62e6\u622a\u5bf9 fetch \u7684\u8c03\u7528\u3002<\/p>\n<\/li>\n<li>\n<p>\u8bf7\u6c42\u65e5\u5fd7\u8bb0\u5f55\uff1a\u8bb0\u5f55\u8bf7\u6c42\u7684 url \u548c\u9009\u9879\u3002<br \/> \u54cd\u5e94\u5904\u7406\uff1a\u8bb0\u5f55\u54cd\u5e94\u72b6\u6001\u3001\u72b6\u6001\u6587\u672c\u548c url\u3002<br \/> \u514b\u9686\u54cd\u5e94\u4ee5\u786e\u4fdd\u6b63\u6587\u53ef\u4ee5\u88ab\u591a\u6b21\u8bfb\u53d6\u3002<\/p>\n<\/li>\n<li>\n<p>\u9519\u8bef\u5904\u7406\uff1a\u6355\u83b7\u5e76\u8bb0\u5f55\u63d0\u53d6\u8fc7\u7a0b\u4e2d\u9047\u5230\u7684\u4efb\u4f55\u9519\u8bef\u3002<\/p>\n<\/li>\n<li>\n<p>\u4f7f\u7528\u4ee3\u7406\uff1a\u60a8\u53ef\u4ee5\u901a\u8fc7\u5c06\u4ee3\u7406\u5206\u914d\u7ed9window.fetch\u6765\u5168\u5c40\u66ff\u6362fetch\u3002<\/p>\n<\/li>\n<\/ol>\n<pre>\/\/ create a logging wrapper for fetch using proxy\nconst fetchlogger = new proxy(fetch, {\n    apply: (target, thisarg, args) =&gt; {\n        \/\/ log the request details\n        const [url, options] = args;\n        console.log(\"fetch request:\");\n        console.log(\"url:\", url);\n        console.log(\"options:\", options);\n\n        \/\/ call the original fetch function\n        return reflect.apply(target, thisarg, args)\n            .then(response =&gt; {\n                \/\/ log response details\n                console.log(\"fetch response:\");\n                console.log(\"status:\", response.status);\n                console.log(\"status text:\", response.statustext);\n                console.log(\"url:\", response.url);\n\n                \/\/ return the response for further use\n                return response.clone(); \/\/ clone to allow response reuse\n            })\n            .catch(error =&gt; {\n                \/\/ log errors\n                console.error(\"fetch error:\", error);\n                throw error; \/\/ rethrow the error for caller handling\n            });\n    }\n});\n\n\/\/ example usage of the logging fetch\nfetchlogger(\"https:\/\/jsonplaceholder.typicode.com\/posts\", {\n    method: \"get\",\n    headers: {\n        \"content-type\": \"application\/json\"\n    }\n})\n    .then(response =&gt; response.json())\n    .then(data =&gt; console.log(\"data:\", data))\n    .catch(error =&gt; console.error(\"error in fetch:\", error));\n<\/pre>\n<h3> \u5c06\u5168\u5c40\u83b7\u53d6\u66ff\u6362\u4e3a\u65e5\u5fd7\u8bb0\u5f55 <\/h3>\n<pre>window.fetch = fetchLogger;\n<\/pre>\n<p>\u4ee5\u4e0a\u5c31\u662f\u300aLogging System with Proxy and Fetch\u300b\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u5173\u4e8e\u7684\u8d44\u6599\u8bf7\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff01<\/p>\n<p>      \u7248\u672c\u58f0\u660e \u672c\u6587\u8f6c\u8f7d\u4e8e\uff1adev.to \u5982\u6709\u4fb5\u72af\uff0c\u8bf7\u8054\u7cfb\u5220\u9664    <\/p>\n<dl>\n<dt><\/dt>\n<dd>\n   Vite \u4e2d\u5982\u4f55\u5f15\u5165\u9759\u6001 JS \u6587\u4ef6\uff1f\n <\/dd>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>Logging System with Proxy and Fetch \u6536\u85cf \u5728IT\u884c\u4e1a\u8fd9\u4e2a\u53d1\u5c55\u66f4\u65b0\u901f\u5ea6\u5f88\u5feb\u7684\u884c\u4e1a\uff0c\u53ea\u6709\u4e0d\u505c\u6b62\u7684\u5b66\u4e60\uff0c\u624d\u4e0d\u4f1a\u88ab\u884c\u4e1a\u6240\u6dd8\u6c70\u3002\u5982\u679c\u4f60\u662f\u6587\u7ae0\u5b66\u4e60\u8005\uff0c\u90a3\u4e48\u672c\u6587\u300aLogging System with Proxy and Fetch\u300b\u5c31\u5f88\u9002\u5408\u4f60\uff01\u672c\u7bc7\u5185\u5bb9\u4e3b\u8981\u5305\u62ec##content_title##\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u52a9\u529b\u5b9e\u6218\u5f00\u53d1\uff01 \u4ee3\u7406\u5bf9\u8c61\uff1afetchlogger \u5305\u88c5\u4e86 fetch \u51fd\u6570\u3002 \u5b83\u4f7f\u7528 apply trap \u6765\u62e6\u622a\u5bf9 fetch \u7684\u8c03\u7528\u3002 \u8bf7\u6c42\u65e5\u5fd7\u8bb0\u5f55\uff1a\u8bb0\u5f55\u8bf7\u6c42\u7684 url \u548c\u9009\u9879\u3002 \u54cd\u5e94\u5904\u7406\uff1a\u8bb0\u5f55\u54cd\u5e94\u72b6\u6001\u3001\u72b6\u6001\u6587\u672c\u548c url\u3002 \u514b\u9686\u54cd\u5e94\u4ee5\u786e\u4fdd\u6b63\u6587\u53ef\u4ee5\u88ab\u591a\u6b21\u8bfb\u53d6\u3002 \u9519\u8bef\u5904\u7406\uff1a\u6355\u83b7\u5e76\u8bb0\u5f55\u63d0\u53d6\u8fc7\u7a0b\u4e2d\u9047\u5230\u7684\u4efb\u4f55\u9519\u8bef\u3002 \u4f7f\u7528\u4ee3\u7406\uff1a\u60a8\u53ef\u4ee5\u901a\u8fc7\u5c06\u4ee3\u7406\u5206\u914d\u7ed9window.fetch\u6765\u5168\u5c40\u66ff\u6362fetch\u3002 \/\/ create a logging wrapper for fetch using proxy const fetchlogger = new proxy(fetch, { apply: (target, thisarg, args) =&gt; { \/\/ log the [&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-53604","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/53604","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=53604"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/53604\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=53604"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=53604"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=53604"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}