Logging
How to access logs for your Edge Functions.
Logs are provided for each function invocation, locally and in hosted environments.
How to access logs
Hosted
You can access both tools from the Functions section of the Dashboard. Select your function from the list, and click Invocations
or Logs
:
- Invocations: shows the Request and Response for each execution. You can see the headers, body, status code, and duration of each invocation. You can also filter the invocations by date, time, or status code.
- Logs: shows any platform events, uncaught exceptions, and custom log events. You can see the timestamp, level, and message of each log event. You can also filter the log events by date, time, or level.
Local
When developing locally you will see error messages and console log statements printed to your local terminal window.
Events that get logged
- Uncaught exceptions: Uncaught exceptions thrown by a function during execution are automatically logged. You can see the error message and stack trace in the Logs tool.
- Custom log events: You can use
console.log
,console.error
, andconsole.warn
in your code to emit custom log events. These events also appear in the Logs tool. - Boot and Shutdown Logs: The Logs tool extends its coverage to include logs for the boot and shutdown of functions.
A custom log message can contain up to 10,000 characters. A function can log up to 100 events within a 10 second period.
Here is an example of how to use custom logs events in your function:
_23Deno.serve(async (req) => {_23 try {_23 const { name } = await req.json()_23_23 if (!name) {_23 console.warn('Empty name provided')_23 }_23_23 const data = {_23 message: `Hello ${name || 'Guest'}!`, // Provide a default value if name is empty_23 }_23_23 console.log(`Name: ${name}`)_23_23 return new Response(JSON.stringify(data), { headers: { 'Content-Type': 'application/json' } })_23 } catch (error) {_23 console.error(`Error processing request: ${error.message}`)_23 return new Response(JSON.stringify({ error: 'Internal Server Error' }), {_23 status: 500,_23 headers: { 'Content-Type': 'application/json' },_23 })_23 }_23})
Logging Tips
Logging Request Headers
When debugging Edge Functions, a common mistake is to try to log headers to the developer console via code like this:
Both attempts will give as output the string "{}"
, even though retrieving the value using request.headers.get("Your-Header-Name")
will indeed give you the correct value. This behavior mirrors that of browsers.
The reason behind this behavior is that Headers objects don't store headers in JavaScript properties that can be enumerated. As a result, neither the developer console nor the JSON stringifier can properly interpret the names and values of the headers. Essentially, it's not an empty object, but rather an opaque one.
However, Headers
objects are iterable. You can utilize this feature to craft a couple of succinct one-liners for debugging and printing headers.
Convert headers into an object with Object.fromEntries:
You can use Object.fromEntries
which is a call to convert the headers into an object:
This results in something like: