JavaScript logging

JavaScript logging captures log messages generated during the current request.

In addition to logging static strings, you can also use variable substitution to include dynamic data 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.

Add the JavaScript built-in module

For information about how to retrieve logs for the responseProvider event handler see, Enable JavaScript logging for responseProvider

  1. To view the JavaScript logging results you need to set up your property for enhanced debug headers.

  2. Import the built-in log module.

import {logger} from 'log' // Import the logger module export function
  1. 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.

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');
}
  1. Activate your EdgeWorkers code bundle that now includes the built-in log module.

  2. 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 -i 'http://www.example.com/ -H 'Pragma: akamai-x-ew-debug' -H 'Akamai-EW-Trace: exp=1600166650~acl=/*~hmac=db4018160fab828da87d3fd8685bfbb9e0f1020885a9cce9403bcf3f5182477d'

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.

"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"

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 and added the built-in log 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 -i 'http://www.example.com/ -H 'Pragma: akamai-x-ew-debug-rp' -H 'Akamai-EW-Trace: exp=1600166650~acl=/*~hmac=db4018160fab828da87d3fd8685bfbb9e0f1020885a9cce9403bcf3f5182477d'

In the multi-part response output below, the original body is separated from the trailing debug information by the j50cx0rLZHkfMieMHdE7HM randomized boundary string.

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--

Add dynamic data to log messages

You can also add dynamic data to your logging output. For example, you can include information such as JavaScript variables or information about the specific request.

ūüďė

Make sure that you do not include sensitive information in the log messages.
When adding data to your JavaScript log use variable substitution instead of manual string concatenation or string templates. Variable substitution is recommended for performance reasons.

  1. JavaScript logging supports the WhatWG open specifications to enable variable substitution.

    You can use these format specifiers when using variable substitution to add data.

SpecifierResult
%sConverts an argument into a string and inserts it into the JavaScript log.
%d or %iConverts an argument into an integer and inserts it into the JavaScript log.
%fConverts an argument into float and inserts it into the JavaScript log.
%o or %OConverts an argument into JSON and inserts it into the JavaScript log.
%%Inserts a verbatim "%" into the JavaScript log.
  1. Here’s an example of how to import the built-in log module and include information about the host, method, and path used during the EdgeWorkers onClientRequest event handler:
import {logger} from 'log';
export function onClientRequest(request) {
   logger.log("The request's host is: %s", request.host);
   logger.log("The request's method is: %s", request.method)
   logger.log("The request's path is: %s", request.path)
   if (request.scheme == 'https') {
       logger.log('This is an https request')
   } else {
       logger.log('This is an http request')
   }
}
  1. Here’s a curl request that passes the Pragma akamai-x-ew-debug and the Akamai-EW-Trace token to the web site https://www.example.com:
curl -i 'https://www.example.com' -H 'Pragma:akamai-x-ew-debug' -H 'Akamai-EW-Trace: exp=1600166650~acl=/*~hmac=db4018160fab828da87d3fd8685bfbb9e0f1020885a9cce9403bcf3f5182477d'

This example shows that the log provides information about the host, method, and path used during the EdgeWorkers onClientRequest event handler.

X-Akamai-EdgeWorker-onClientRequest-Log: D:main.js:4 The request's host is : www.example.com |D::5 The request's method is : GET|D::6 The request's path is : /reflect.html|D::11 This is an http request

Did this page help you?