---
name: azion-query-function-logs-with-graphql-api
description: >-
  Retrieve the console and runtime log lines of your functions for a time range, and filter them by message content.
---

# Query function logs with GraphQL API

You can retrieve the log lines a function writes with [GraphQL API](/en/documentation/devtools/graphql/overview/). Run the query from an API platform such as Postman, or from the GraphiQL Playground in Azion Console. The `functionConsoleEvents` dataset returns the messages your code writes with `console.log`, and the lines Azion Runtime produces. For compute time and invocation counts, refer to [Query usage data from Functions](/en/documentation/guides/platform/observability/query-functions-usage-data-with-graphql/). To match an entry to its cause, refer to [Troubleshoot function execution and logs](/en/documentation/platform/functions/troubleshooting/).

---

## Prerequisites

- A personal token. To create one, refer to [Personal Tokens](/en/documentation/fundamentals/personal-tokens/).
- A function that has run at least once. Creating a function does not execute it, and a function that never runs writes no entries.

---

## Build the query

The `functionConsoleEvents` dataset holds one entry per log message. This query returns 10 entries from a time range, ordered from the oldest to the most recent:

```graphql
query ConsoleLog {
  functionConsoleEvents(
    limit: 10,
    filter: {
      tsRange: {begin:"2023-02-01T10:10:10", end:"2023-06-15T10:10:10"}
    }
    orderBy: [ts_ASC]
  )
  {
    ts
    solutionId
    configurationId
    functionId
    id
    lineSource
    level
    line
  }
}
```

Set `begin` and `end` to the time range you want to read. The `limit` field accepts up to 10,000 rows per query, and a query selects at most 37 fields. For both limits, refer to [Limits GraphQL API](/en/documentation/devtools/graphql/limits/).

---

## Send the query

The Real-Time Events endpoint, `https://api.azion.com/v4/events/graphql`, answers the query. To send it from Postman or another API platform:

1. **Create a request**

   For the complete Postman setup, refer to [How to query GraphQL requests on Postman](/en/documentation/guides/platform/observability/query-graphql-postman/).

2. **Add your personal token to the Authorization header**

   Enter `Authorization: Token [TOKEN VALUE]`, where `[TOKEN VALUE]` is the token you created.

3. **Set the method to POST**

4. **Enter the Real-Time Events endpoint as the URL**

   Enter `https://api.azion.com/v4/events/graphql`.

5. **Add the ConsoleLog query to the request body**

6. **Send the request**

The response carries the matching entries as JSON:

```json
{
    "data": {
        "functionConsoleEvents": [
            {
                "ts": "2023-06-12T17:09:54Z",
                "solutionId": "1234567890",
                "configurationId": "1234567891",
                "functionId": "9483",
                "id": "fa498cd9-cbep-4382-8bbb-32b029fe7411",
                "lineSource": "RUNTIME",
                "level": "ERROR",
                "line": "TypeError: Object not found"
            },
            {
                "ts": "2023-06-12T17:09:54Z",
                "solutionId": "1234567890",
                "configurationId": "1234567891",
                "functionId": "9483",
                "id": "fa498cd9-cbep-4382-8bbb-32b029fe7411",
                "lineSource": "RUNTIME",
                "level": "ERROR",
                "line": "TypeError: Object not found"
            },
            {
                "ts": "2023-06-12T17:09:54Z",
                "solutionId": "1234567890",
                "configurationId": "1234567891",
                "functionId": "9483",
                "id": "fa498cd9-cbep-4382-8bbb-32b029fe7411",
                "lineSource": "RUNTIME",
                "level": "ERROR",
                "line": "    at async mainFetch (ext:cells_fetch/26_fetch.js:266:12)"
            }
        ]
    }
}
```

Each entry carries the level of the message in `level`, its category in `lineSource`, and the message itself in `line`. `CONSOLE` marks a line your own code wrote with `console.log`. A line Azion Runtime produced, such as an exception, carries `RUNTIME`. The `id` field is the request identifier, and it aggregates every message of a single request. For every field of the dataset, refer to [Real-Time Events GraphQL API Fields](/en/documentation/devtools/graphql/gql-real-time-events-fields/).

You now have the log entries of the time range, each with its level and its source.

> **Tip**
>
> To run the query without an API platform, log in to Azion Console and open `https://api.azion.com/v4/events/graphql`. Use the GraphiQL Playground to write, validate, and test queries in the browser. Refer to [First Steps GraphQL API](/en/documentation/devtools/graphql/first-steps/).

---

## Filter the entries by message

Every filter key joins a field name to an operator, as in `tsRange` and `lineLike`. To return only the entries whose message matches a pattern, add `lineLike` to the filter:

```graphql
query ConsoleLogErrors {
  functionConsoleEvents(
    limit: 10,
    filter: {
      tsRange: {begin:"2023-02-01T10:10:10", end:"2023-06-15T10:10:10"},
      lineLike: "%TypeError%"
    }
    orderBy: [ts_ASC]
  )
  {
    ts
    functionId
    id
    lineSource
    level
    line
  }
}
```

The `%` character stands for any sequence of characters, so `"%TypeError%"` matches every line that contains `TypeError`. `Like` is case-sensitive, and `Ilike` is its case-insensitive form. For every operator, refer to [Queries GraphQL API](/en/documentation/devtools/graphql/features/#operators).

---

## Next steps

- [Troubleshoot function execution and logs](/en/documentation/platform/functions/troubleshooting.md): Match a log entry to the failure that produced it, and apply the fix.
- [Real-Time Events fields](/en/documentation/devtools/graphql/gql-real-time-events-fields.md): Every field of the functionConsoleEvents dataset, with an example value.
- [GraphQL API quickstart](/en/documentation/devtools/graphql/first-steps.md): The personal token, the endpoint of each dataset, and the GraphiQL Playground.
- [Debug functions on Data Stream](/en/documentation/guides/platform/observability/debugging-functions-data-stream.md): Send the same log output to an endpoint you own.
