> For clean Markdown of any page, append .md to the page URL. > For a complete documentation index, see https://akamai.ferndocs.com/edge-workers/enable-javascript-logging/llms.txt. > For AI client integration (Claude Code, Cursor, etc.), connect to the MCP server at https://akamai.ferndocs.com/_mcp/server. # JavaScript logging JavaScript logging captures log messages generated during the current request. You can deliver the JavaScript logs via [Enhanced debug headers](enable-enhanced-debug-headers.md) or a [DataStream 2 stream](ds2-javascript-logging.md). In addition to logging static strings, you can also use variable substitution to include [dynamic data](/edge-workers/enable-javascript-logging#add-dynamic-data-to-log-messages) in the EdgeWorkers logs. > π§ When adding JavaScript logging to your EdgeWorkers make sure that sensitive data is not included in the log output. The following restrictions apply to JavaScript logging headers: - Logging headers are not cached. - The logging header includes all messages logged for the specified event handler. - The maximum log size per event handler is 1024 bytes. - If the log contents exceed 1024 bytes the results are truncated. # Use enhanced debug headers to view JavaScript logs For information about how to retrieve logs for the `responseProvider` event handler see, [Enable JavaScript logging for responseProvider](/edge-workers/enable-javascript-logging#enable-javascript-logging-for-responseprovider). 1. To view the JavaScript logging results you need to set up your property for [enhanced debug headers](enable-enhanced-debug-headers.md). 2. Import the built-in [log](log.md) module. ```javascript import {logger} from 'log' // Import the logger module export function ``` 3. Add the logging messages into the event handlers of your choice. You can add EdgeWorkers log messages to all event handlers. > π You can also use format specifiers to add dynamic data to the log messages. ```javascript import { logger } from 'log'; // Import the logger module export function export function onClientRequest (request) { logger.log('gold'); } export function onOriginRequest (request) { logger.log('silver'); } export function onOriginResponse (request, response) { logger.log('bronze'); } export function onClientResponse (request, response) { logger.log('iron'); } export function responseProvider (request) { logger.log('steel'); } ``` 4. Activate your EdgeWorkers code bundle that now includes the built-in log module. 5. To view the log data you can use this curl request that adds the `Pragma: Akamai-X-Ew-Debug` and the `Akamai-EW-Trace` headers. These headers retrieve the logging information. ```curl curl -i 'http://www.example.com/ -H 'Pragma: akamai-x-ew-debug' -H 'Akamai-EW-Trace: eyJ0eXAiOiJKV1Qi ... iu1XOS9eJOl-54Ywd' ``` This example lists the corresponding logging response header for each event handler. A response header is only returned when logging information is available for the event. ```text "X-Akamai-EdgeWorker-onClientRequest-Log: D:main.js:4 gold\r\n" "X-Akamai-EdgeWorker-onOriginRequest-Log: D:main.js:8 silver\r\n" "X-Akamai-EdgeWorker-onOriginResponse-Log: D:main.js:12 bronze\r\n" "X-Akamai-EdgeWorker-onClientResponse-Log: D:main.js:16 iron\r\n" ``` 6. You can also use the `akamai-x-ew-log` header to specify the [log](log.md) level. The available log levels, in ascending order of severity, are `trace`, `debug`, `info`, `warn`, and `error`. In the request header below, `akamai-x-ew-log-level: error`, specifies that **error** level messages should be included in the logs. ```curl curl "http://www.example.com" -H 'Pragma: akamai-x-ew-debug' -H 'akamai-x-ew-log' -H 'akamai-x-ew-log-level: error' -H 'Akamai-EW-Trace: eyJ0eXAiOiJKV1Qi ... iu1XOS9eJOl-54Yw ``` ## Enable JavaScript logging for responseProvider JavaScript logging for the `responseProvider` event handler includes a multi-part response. It consists of the expected response body followed by an additional section that contains the `responseProvider` debug fields. With streamed responses, this information is not available until the event handler ends. > π For `responseProvider` you need to add the Pragma `akamai-x-ew-debug-rp` header that enables the multi-part response header. If the `responseProvider` event handler is not implemented, a status of **UnimplementedEventHandler** will appear in the standard trace header. 1. Make sure that you've [enabled enhanced debug headers](enable-enhanced-debug-headers.md) and added the [built-in log](log.md) module to your EdgeWorkers function. 2. Hereβs a curl request that adds the Pragma `akamai-x-ew-debug-rp` multi-part response header and the `Akamai-EW-Trace` header to retrieve the JavaScript logging information: ```curl curl -i 'http://www.example.com/ -H 'Pragma: akamai-x-ew-debug-rp' -H 'Akamai-EW-Trace: eyJ0eXAiOiJKV1Qi ... iu1XOS9eJOl-54Yw' ``` In the multi-part response output below, the original body is separated from the trailing debug information by the `j50cx0rLZHkfMieMHdE7HM` randomized boundary string. ```shell HTTP/1.1 200 OK Content-Type: multipart/form-data; boundary=j50cx0rLZHkfMieMHdE7HM content-disposition: attachment Date: Thu, 22 Oct 2020 19:08:18 GMT Content-Length: 1871 Connection: keep-alive X-Akamai-EdgeWorker-onClientResponse-Info: ew=[EdgeWorker ID] v1.0:Example; status=Success; status_msg=-; wall_time=0.037; cpu_time=0.037; sc_ver=2.15.14; pearl_ver=2.0.1; ew_exe_ip=198.18.231.68; heap_usage=0 X-Akamai-EdgeWorker-onClientRequest-Info: ew=[EdgeWorker ID] v:Example; status=UnimplementedEventHandler; sc_ver=; pearl_ver=2.0.1; ew_exe_ip=198.18.231.68 AK_EIP_FORWARDER_IP: X-Powered-By: Akamai EdgeWorkers --j50cx0rLZHkfMieMHdE7HM content-type: application/json content-disposition: form-data; name="response-provider-body" {"endPoint1":{"manifest1":{ ... }},"endPoint2":{"manifest2":{ ... }},"endPoint3":{"manifest3":{ ... }}} --j50cx0rLZHkfMieMHdE7HM content-type: text/plain;charset=UTF-8 content-disposition: form-data; name="stream-trace" X-Akamai-EdgeWorker-ResponseProvider-Info: ew=[EdgeWorker ID] v1.0:Example; status=Success; status_msg=-; wall_time=18.921; cpu_time=5.892 X-Akamai-EdgeWorker-responseProvider-Log: D:main.js:40 SubRequests Completed - Resulting JSON {"endPoint1":{"manifest1":{ ... }},"endPoint2":{"manifest2":{ ... }},"endPoint3":{"manifest3":{ ... }}} --j50cx0rLZHkfMieMHdE7HM-- ``` 3. You can also use the `akamai-x-ew-log` header to specify the [log](log.md) level. The available log levels, in ascending order of severity, are `trace`, `debug`, `info`, `warn`, and `error`. In the request header below, `akamai-x-ew-log-level: error`, specifies that **error** level messages should be included in the logs. ```curl curl -i 'http://www.example.com/ -H 'Pragma: akamai-x-ew-debug-rp'-H 'akamai-x-ew-log' -H 'akamai-x-ew-log-level: error' -H 'Akamai-EW-Trace: eyJ0eXAiOiJKV1Qi ... iu1XOS9eJOl-54Yw' ``` # Use DataStream 2 to deliver the JavaScript logs For complete instructions that include how to set up a DataStream 2 stream and a log destination, refer to the [Use DataStream 2 to deliver JavaScript logs](javascript-logging.md) tutorial. 1. Import the [log](log.md) built-in module in the `main.js` file. The log built-in module logs messages generated during the current request. ```javascript // Import logging module import { logger } from 'log'; export function onClientRequest (request) { logger.trace("onClientRequest test trace level"); logger.debug("onClientRequest test debug level"); logger.info("onClientRequest test info level"); logger.warn("onClientRequest test warn level"); logger.error("onClientRequest test error level"); request.respondWith( 200, {}, '