# Loggly

`loggly` 插件将网关请求和响应日志发送至 [SolarWinds Loggly](https://www.solarwinds.com/loggly)，以便搜索和分析。HTTPS 批量投递提供经过身份认证且有确认机制的传输方式，建议在生产环境中使用。

可选的 Syslog 方式使用 UDP，无法验证服务提供商是否收到数据，也无法重传本地发送成功后丢失的数据包。

## 示例[​](#示例 "示例的直接链接")

以下示例通过 HTTPS 发送默认日志并添加指定字段。

本示例无需部署 Loggly。网关将事件发送至 SolarWinds Loggly 的[托管 HTTPS 批量端点](https://documentation.solarwinds.com/en/success_center/loggly/content/admin/http-bulk-endpoint.htm)。请确保每个网关都能通过 HTTPS 的 `443` 端口访问 `logs-01.loggly.com`。登录 Loggly，在 **Source Setup → Customer Tokens** 中复制客户令牌：

```
export LOGGLY_CUSTOMER_TOKEN="replace-with-customer-token"
```

请将客户令牌作为机密信息处理，并限制对令牌和 Loggly 数据的访问。

### 通过 HTTPS 发送日志[​](#通过-https-发送日志 "通过 HTTPS 发送日志的直接链接")

配置 Loggly 插件元数据以通过 HTTPS 投递，然后配置路由日志记录器。

* Admin API
* ADC
* Ingress Controller

```
curl "http://127.0.0.1:9180/apisix/admin/plugin_metadata/loggly" -X PUT \

  -H "X-API-KEY: ${ADMIN_API_KEY}" \

  -H "Content-Type: application/json" \

  -d '{

    "host": "logs-01.loggly.com",

    "protocol": "https"

  }'



curl "http://127.0.0.1:9180/apisix/admin/routes/loggly-route" -X PUT \

  -H "X-API-KEY: ${ADMIN_API_KEY}" \

  -H "Content-Type: application/json" \

  -d @- <<EOF

{

  "uri": "/anything/loggly",

  "plugins": {

    "loggly": {

      "customer_token": "${LOGGLY_CUSTOMER_TOKEN}",

      "tags": [

        "api7-docs"

      ],

      "batch_max_size": 1

    }

  },

  "upstream": {

    "type": "roundrobin",

    "nodes": {

      "httpbin.org:80": 1

    }

  }

}

EOF
```

修改 Loggly 条目前，先导出完整的插件元数据集合：

```
adc dump -o adc-metadata.yaml --with-id \

  --include-resource-type plugin_metadata
```

在 `adc-metadata.yaml` 中添加或更新 Loggly 条目，同时保留所有其他元数据条目：

adc-metadata.yaml (plugin\_metadata 片段)

```
plugin_metadata:

  # 保留导出文件中的所有其他插件元数据条目。

  loggly:

    host: logs-01.loggly.com

    protocol: https
```

单独创建带标签的服务：

adc.yaml

```
services:

  - name: loggly-service

    labels:

      docs-example: loggly

    routes:

      - name: loggly-route

        uris:

          - /anything/loggly

        plugins:

          loggly:

            customer_token: "${LOGGLY_CUSTOMER_TOKEN}"

            tags:

              - api7-docs

            batch_max_size: 1

    upstream:

      type: roundrobin

      nodes:

        - host: httpbin.org

          port: 80

          weight: 1
```

预览完整元数据集合，确认差异中没有意外更新或删除：

```
adc diff -f adc-metadata.yaml \

  --include-resource-type plugin_metadata
```

检查 `diff.yaml`，然后同步插件元数据集合：

```
adc sync -f adc-metadata.yaml \

  --include-resource-type plugin_metadata
```

预览带标签的服务：

```
adc diff -f adc.yaml \

  --include-resource-type service \

  --label-selector docs-example=loggly
```

检查 `diff.yaml`，然后同步服务：

```
adc sync -f adc.yaml \

  --include-resource-type service \

  --label-selector docs-example=loggly
```

在部署使用的完整 `GatewayProxy` 清单中，将 HTTPS Loggly 条目添加到 `spec.pluginMetadata` 下：

GatewayProxy pluginMetadata 片段

```
pluginMetadata:

  # 保留所有其他插件元数据条目。

  loggly:

    host: logs-01.loggly.com

    protocol: https
```

通过部署常用的 Kubernetes 或 GitOps 工作流应用更新后的完整 `GatewayProxy` 清单。

接下来，使用部署所用的 Kubernetes API 将插件绑定到路由：

* Gateway API
* APISIX CRD

loggly-ic.yaml

```
apiVersion: v1

kind: Service

metadata:

  namespace: aic

  name: httpbin-external-domain

spec:

  type: ExternalName

  externalName: httpbin.org

  ports:

    - port: 80

---

apiVersion: apisix.apache.org/v1alpha1

kind: PluginConfig

metadata:

  namespace: aic

  name: loggly-route

spec:

  plugins:

    - name: loggly

      config:

        customer_token: replace-with-customer-token

        tags:

          - api7-docs

        batch_max_size: 1

---

apiVersion: gateway.networking.k8s.io/v1

kind: HTTPRoute

metadata:

  namespace: aic

  name: loggly-route

spec:

  parentRefs:

    - name: apisix

  rules:

    - matches:

        - path:

            type: Exact

            value: /anything/loggly

      filters:

        - type: ExtensionRef

          extensionRef:

            group: apisix.apache.org

            kind: PluginConfig

            name: loggly-route

      backendRefs:

        - name: httpbin-external-domain

          port: 80
```

loggly-ic.yaml

```
apiVersion: v1

kind: Service

metadata:

  namespace: aic

  name: httpbin-external-domain

spec:

  type: ExternalName

  externalName: httpbin.org

  ports:

    - port: 80

---

apiVersion: apisix.apache.org/v2

kind: ApisixPluginConfig

metadata:

  namespace: aic

  name: loggly-route

spec:

  ingressClassName: apisix

  plugins:

    - name: loggly

      enable: true

      config:

        customer_token: replace-with-customer-token

        tags:

          - api7-docs

        batch_max_size: 1

---

apiVersion: apisix.apache.org/v2

kind: ApisixRoute

metadata:

  namespace: aic

  name: loggly-route

spec:

  ingressClassName: apisix

  http:

    - name: loggly-route

      match:

        paths:

          - /anything/loggly

      backends:

        - serviceName: httpbin-external-domain

          servicePort: 80

      plugin_config_name: loggly-route
```

应用路由配置：

```
kubectl apply -f loggly-ic.yaml
```

发送请求，然后在 Loggly 中搜索 `tag:api7-docs`。索引可能需要几分钟。

```
curl "http://127.0.0.1:9080/anything/loggly"
```

事件应包含：

```
{

  "route_id": "loggly-route",

  "request": {

    "method": "GET",

    "uri": "/anything/loggly"

  },

  "response": {

    "status": 200

  }

}
```

### 添加指定字段[​](#添加指定字段 "添加指定字段的直接链接")

更新 Loggly 插件元数据，将指定字段添加到默认日志条目中。`log_format_extra` 对象用于扩充默认条目；若要用指定字段替换默认条目，请改用 `log_format`。

避免向 Loggly 发送敏感值。

* Admin API
* ADC
* Ingress Controller

用投递设置和所需字段替换现有元数据：

```
curl "http://127.0.0.1:9180/apisix/admin/plugin_metadata/loggly" -X PUT \

  -H "X-API-KEY: ${ADMIN_API_KEY}" \

  -H "Content-Type: application/json" \

  -d '{

    "host": "logs-01.loggly.com",

    "protocol": "https",

    "log_format_extra": {

      "request_method": "$request_method",

      "request_uri": "$request_uri",

      "response_status": "$status"

    }

  }'
```

在部署使用的完整插件元数据集合中替换 Loggly 条目：

adc-metadata.yaml (plugin\_metadata 片段)

```
plugin_metadata:

  # 保留所有其他插件元数据条目。

  loggly:

    host: logs-01.loggly.com

    protocol: https

    log_format_extra:

      request_method: $request_method

      request_uri: $request_uri

      response_status: $status
```

预览完整元数据变更，确认没有意外更新或删除：

```
adc diff -f adc-metadata.yaml \

  --include-resource-type plugin_metadata
```

检查 `diff.yaml`，然后同步插件元数据集合：

```
adc sync -f adc-metadata.yaml \

  --include-resource-type plugin_metadata
```

在部署使用的完整 `GatewayProxy` 清单中替换 `spec.pluginMetadata` 下的 Loggly 条目：

GatewayProxy pluginMetadata 片段

```
pluginMetadata:

  # 保留所有其他插件元数据条目。

  loggly:

    host: logs-01.loggly.com

    protocol: https

    log_format_extra:

      request_method: $request_method

      request_uri: $request_uri

      response_status: $status
```

通过部署常用的 Kubernetes 或 GitOps 工作流应用更新后的完整清单。

再次发送请求，并在 Loggly 中搜索 `tag:api7-docs`：

```
curl "http://127.0.0.1:9080/anything/loggly"
```

新事件应在默认条目之外包含指定字段：

```
{

  "request_method": "GET",

  "request_uri": "/anything/loggly",

  "response_status": 200

}
```
