0

0

Logging System with Proxy and Fetch

霞舞

霞舞

发布时间:2024-11-30 17:09:31

|

1050人浏览过

|

来源于dev.to

转载

logging system with proxy and fetch

  1. 代理对象:fetchlogger 包装了 fetch 函数。
    它使用 apply trap 来拦截对 fetch 的调用。

  2. 请求日志记录:记录请求的 url 和选项。
    响应处理:记录响应状态、状态文本和 url。
    克隆响应以确保正文可以被多次读取。

  3. 错误处理:捕获并记录提取过程中遇到的任何错误。

  4. 使用代理:您可以通过将代理分配给window.fetch来全局替换fetch。

    Magician
    Magician

    Figma插件,AI生成图标、图片和UX文案

    下载
// create a logging wrapper for fetch using proxy
const fetchlogger = new proxy(fetch, {
    apply: (target, thisarg, args) => {
        // log the request details
        const [url, options] = args;
        console.log("fetch request:");
        console.log("url:", url);
        console.log("options:", options);

        // call the original fetch function
        return reflect.apply(target, thisarg, args)
            .then(response => {
                // log response details
                console.log("fetch response:");
                console.log("status:", response.status);
                console.log("status text:", response.statustext);
                console.log("url:", response.url);

                // return the response for further use
                return response.clone(); // clone to allow response reuse
            })
            .catch(error => {
                // log errors
                console.error("fetch error:", error);
                throw error; // rethrow the error for caller handling
            });
    }
});

// example usage of the logging fetch
fetchlogger("https://jsonplaceholder.typicode.com/posts", {
    method: "get",
    headers: {
        "content-type": "application/json"
    }
})
    .then(response => response.json())
    .then(data => console.log("data:", data))
    .catch(error => console.error("error in fetch:", error));

将全局获取替换为日志记录

window.fetch = fetchLogger;

相关专题

更多
Java 桌面应用开发(JavaFX 实战)
Java 桌面应用开发(JavaFX 实战)

本专题系统讲解 Java 在桌面应用开发领域的实战应用,重点围绕 JavaFX 框架,涵盖界面布局、控件使用、事件处理、FXML、样式美化(CSS)、多线程与UI响应优化,以及桌面应用的打包与发布。通过完整示例项目,帮助学习者掌握 使用 Java 构建现代化、跨平台桌面应用程序的核心能力。

37

2026.01.14

php与html混编教程大全
php与html混编教程大全

本专题整合了php和html混编相关教程,阅读专题下面的文章了解更多详细内容。

19

2026.01.13

PHP 高性能
PHP 高性能

本专题整合了PHP高性能相关教程大全,阅读专题下面的文章了解更多详细内容。

37

2026.01.13

MySQL数据库报错常见问题及解决方法大全
MySQL数据库报错常见问题及解决方法大全

本专题整合了MySQL数据库报错常见问题及解决方法,阅读专题下面的文章了解更多详细内容。

19

2026.01.13

PHP 文件上传
PHP 文件上传

本专题整合了PHP实现文件上传相关教程,阅读专题下面的文章了解更多详细内容。

16

2026.01.13

PHP缓存策略教程大全
PHP缓存策略教程大全

本专题整合了PHP缓存相关教程,阅读专题下面的文章了解更多详细内容。

6

2026.01.13

jQuery 正则表达式相关教程
jQuery 正则表达式相关教程

本专题整合了jQuery正则表达式相关教程大全,阅读专题下面的文章了解更多详细内容。

3

2026.01.13

交互式图表和动态图表教程汇总
交互式图表和动态图表教程汇总

本专题整合了交互式图表和动态图表的相关内容,阅读专题下面的文章了解更多详细内容。

45

2026.01.13

nginx配置文件详细教程
nginx配置文件详细教程

本专题整合了nginx配置文件相关教程详细汇总,阅读专题下面的文章了解更多详细内容。

9

2026.01.13

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
10分钟--Midjourney创作自己的漫画
10分钟--Midjourney创作自己的漫画

共1课时 | 0.1万人学习

Midjourney 关键词系列整合
Midjourney 关键词系列整合

共13课时 | 0.9万人学习

AI绘画教程
AI绘画教程

共2课时 | 0.2万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号