# How to Use Integration Logs with IFX (API only)

Source: https://dev.arcxp.com/integrations/logs-metrics/how-to-use-integration-logs-with-ifx-api-only/

> **Compliance Notice:**
> 
> Customers are responsible for [ensuring their own compliance with GDPR](https://dev.arcxp.com/integrations/ifx-data-processing/) and other applicable data protection laws in their local jurisdiction when using IFX services.

> **Note:**
> 
> While the API is still available, we have created an improved experience for viewing integration logs in a user-friendly UI. Simplifying this process reduces the time required for developers to view necessary logs and provides a better Developer XP. You can find that documentation [here](https://dev.arcxp.com/integrations/logs-metrics/integration-logs-ui).

IFX provides logs for integrations. These logs are helpful for debugging integrations or logging important details. This guide walks you through how to use the logs API.

## Logging Policies

Arc XP maintains high security standards with regular auditing. IFX integrations should always consider security risks such as outputting sensitive data in logs, especially in a production environment. Additionally, integration logs will be automatically purged after 30 days.

> Arc XP reserves the right to disable integrations which do not adhere to the policies.

### Integrations should not:

- Leave DEBUG mode enabled in a production environment outside of active troubleshooting. Be sure to disable it immediately afterward.
- Expose sensitive data in logs, especially a production environment.
- Send or receive unencrypted payment information such as credit card numbers.
- Send or receive any unencrypted sensitive data.

> **Compliance Notice:**
> 
> Customers are responsible for ensuring their own compliance with GDPR and other applicable data protection laws in their local jurisdiction when using IFX services.

Security and reliability are always concerns when integrating with third-parties. It’s common for third-party libraries to have [Security Vulnerabilities](https://snyk.io/vuln), or even for the packages [To Be Removed Entirely From NPM](https://github.com/stevemao/left-pad/issues/4). When integrating third-party libraries, make sure the code is from a reputable source, check it as thoroughly as possible, and keep your versions up-to-date!

## Add Logs to Integration

Simply use `console.log`. If you would like a more comprehensive guide, you can read [Node.js v18.14.2 documentation](https://nodejs.org/docs/latest-v18.x/api/console.html) or [Mozilla’s console.log() documentation](https://developer.mozilla.org/en-US/docs/Web/API/Console/log).

## IFX Log API

[IFX Admin API](https://dev.arcxp.com/api/integrations/integrations-production/) 

On Sandbox and Prod, IFX Log API can be used to view integration logs.

> Replace $ORG with your org id and $PAT with the PAT you got from the Developer Center.

### 1\. Request a queryId

```
curl --location --request GET 'https://api.sandbox.$ORG.arcpublishing.com/ifx/api/v1/admin/logs/integration/:integrationName' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer $PAT' \
--data-raw ''
```

You should get a response with a `queryId`:

```
{
  "queryId": "abcdef12-b766-4e71-868a-a58715f55ebb"
}
```

The default time is 300s (5m). If your logs are older you can use the `start` parameter to change the time in seconds:

```
/ifx/api/v1/admin/logs/integration/:integrationName?start=10000
```

### 2\. View the Logs with the queryId

Use the `queryId` to call the logs API and view the results.

```
curl --location --request GET 'https://api.sandbox.$ORG.arcpublishing.com/ifx/api/v1/admin/logs/results?queryId=abcdefg-b766-4e71-868a-a58715f55ebb' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer $PAT' \
--data-raw ''
```

You should get a response like below where the messages under the `events` object are your integration’s logs:

```json
{
    "statistics": {
        "recordsScanned": 19,
        "bytesScanned": 1738,
        "recordsMatched": 3
    },
    "events": [
        {
            "timestamp": "2022-03-10 16:45:01.733",
            "eventId": "Cl9y8wpdvgpfcvOBNAyg1IsBNQgQwQERgB",
            "message": "END RequestId: 81d3d7b6-4fb0-44eb-a9be-b026e3d03dbd\n"
        },
        {
            "timestamp": "2022-03-10 16:45:01.733",
            "eventId": "+Cl9y8wpdvgpfcvOBNAyg1IsBNQgQwQEhgB",
            "message": "REPORT RequestId: 81d3d7b6-4fb0-44eb-a9be-b026e3d03dbd\tDuration: 4.36 ms\tBilled Duration: 5 ms\tMemory Size: 512 MB\tMax Memory Used: 216 MB\t\nXRAY TraceId: 1-622a2b0d-18725169385f2b6c49dc6713\tSegmentId: 03a858531d7454c4\tSampled: true\t\n"
        },
        {
            "timestamp": "2022-03-10 16:45:01.731",
            "eventId": "+Cl9y8wpdvgpfcvOBNAyg1IsBNQgQwQDhgB",
            "message": "Received event key SCHEDULE_EVENT\n"
        }
    ],
    "status": "Complete"
}
```

If the status is Scheduled or Running, you should wait a few seconds and call the API again to view complete results.

---
  
Have a feature request or suggestion? Let us know on [Our Ideas Portal](https://ideas.arcxp.com/?project=IFX).
