Debugging logs

Send debugging information in an errored response to a logging service.

Last updated Oct 13, 2025

// Replace with your actual logging service endpoint
const loggingEndpoint = "https://your-logging-endpoint";

export default {
    async fetch(request) {
        try {
            // Attempt to fetch the request from the origin server
            const response = await fetch(request.clone());

            // Check if the response status indicates an error (for example, 4xx or 5xx)
            if (!response.ok) {
                // Prepare error details and context for logging
                const errorDetails = {
                    status: response.status,
                    statusText: response.statusText,
                    url: request.url,
                    method: request.method,
                    headers: Object.fromEntries(request.headers),
                };

                // Log error details to your logging service
                await logError(errorDetails);

                // Return the original response with status and statusText intact
                return new Response(response.body, {
                    status: response.status,
                    statusText: response.statusText,
                    headers: response.headers,
                });
            }

            // Return the successful response from the origin server
            return response;
        } catch (error) {
            // Handle any exceptions that occur during fetch
            const errorDetails = {
                message: error.message,
                stack: error.stack,
                url: request.url,
                method: request.method,
                headers: Object.fromEntries(request.headers),
            };

            // Log error details to your logging service
            await logError(errorDetails);

            // Return a generic error response
            return new Response("Internal Server Error", {
                status: 500,
            });
        }
    },
};

// Function to log error details to your logging service
async function logError(details) {
    try {
        const response = await fetch(loggingEndpoint, {
            method: "POST",
            headers: {
                "Content-Type": "application/json",
            },
            body: JSON.stringify(details),
        });

        if (!response.ok) {
            console.error("Failed to log error:", response.statusText);
        }
    } catch (error) {
        console.error("Error logging error:", error.message);
    }
}

Was this helpful?

YesNo

On this page

Debugging logs - 图1Docs

{"@context":"https://schema.org","@type":"TechArticle","@id":"https://developers.cloudflare.com/rules/snippets/examples/debugging-logs/#page","headline":"Debugging logs","description":"Send debugging information in an errored response to a logging service.","url":"https://developers.cloudflare.com/rules/snippets/examples/debugging-logs/","inLanguage":"en","image":"https://developers.cloudflare.com/rules/snippets/examples/debugging-logs/og.png?v=5763ec0f092af796","dateModified":"2025-10-13","publisher":{"@type":"Organization","name":"Cloudflare","description":"One platform for your apps, agents, and workforce. Build, secure, and scale without managing infrastructure","url":"https://www.cloudflare.com/","sameAs":["https://github.com/cloudflare","https://www.linkedin.com/company/cloudflare","https://x.com/cloudflare"],"logo":{"@type":"ImageObject","url":"https://developers.cloudflare.com/logo.svg"},"address":{"@type":"PostalAddress","streetAddress":"101 Townsend St","addressLocality":"San Francisco","addressRegion":"CA","postalCode":"94107","addressCountry":"US"},"contactPoint":[{"@type":"ContactPoint","contactType":"Customer Support","url":"https://support.cloudflare.com/","availableLanguage":["English"]},{"@type":"ContactPoint","contactType":"Sales","url":"https://www.cloudflare.com/contact/","availableLanguage":["English"]}]},"isPartOf":{"@type":"WebSite","@id":"https://developers.cloudflare.com/#website","name":"Cloudflare Docs","url":"https://developers.cloudflare.com/"},"keywords":["Logging","Response modification"]}


description: Adjust Cross-Origin Resource Sharing (CORS) headers and handle preflight requests. title: Define CORS headers image: https://developers.cloudflare.com/rules/snippets/examples/define-cors-headers/og.png?v=31dd9760a4d0407c


Skip to content

Documentation Index
Fetch the complete documentation index at: https://developers.cloudflare.com/rules/llms.txt
Use this file to discover all available pages before exploring further.