# SLS Logger

The `sls-logger` plugin sends gateway request and response logs to [Alibaba Cloud Simple Log Service](https://www.alibabacloud.com/help/en/sls/use-the-syslog-protocol-to-upload-logs) as RFC 5424 events over TCP and TLS.

## Examples[​](#examples "Direct link to Examples")

The examples show how to send default access logs and replace them with a focused custom format.

[Alibaba Cloud SLS](https://www.alibabacloud.com/help/en/sls/resource-management-overview) is a hosted service, so no local SLS server is required. Activate SLS, create a Project and Logstore, then create a RAM user restricted to `log:PostLogStoreLogs` for that Project and Logstore.

Choose the project-qualified public or internal regional Syslog endpoint that every gateway can reach over TCP port `10009`. SLS requires TLS 1.2. Export the host and RAM credentials:

```
export SLS_HOST="your-project.cn-hangzhou.log.aliyuncs.com"

export SLS_PROJECT="your-project"

export SLS_LOGSTORE="your-logstore"

export SLS_ACCESS_KEY_ID="replace-with-access-key-id"

export SLS_ACCESS_KEY_SECRET="replace-with-access-key-secret"
```

Keep each RFC 5424 log entry no larger than 64 KB.

### Send Default Access Logs[​](#send-default-access-logs "Direct link to Send Default Access Logs")

Configure a route to send each record immediately to the SLS Syslog endpoint.

* Admin API
* ADC
* Ingress Controller

```
curl "http://127.0.0.1:9180/apisix/admin/routes/sls-logger-route" -X PUT \

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

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

  -d @- <<EOF

{

  "uri": "/anything/sls-logger",

  "plugins": {

    "sls-logger": {

      "host": "${SLS_HOST}",

      "port": 10009,

      "project": "${SLS_PROJECT}",

      "logstore": "${SLS_LOGSTORE}",

      "access_key_id": "${SLS_ACCESS_KEY_ID}",

      "access_key_secret": "${SLS_ACCESS_KEY_SECRET}",

      "batch_max_size": 1

    }

  },

  "upstream": {

    "type": "roundrobin",

    "nodes": {

      "httpbin.org:80": 1

    }

  }

}

EOF
```

adc.yaml

```
services:

  - name: sls-logger-service

    labels:

      docs-example: sls-logger

    routes:

      - name: sls-logger-route

        uris:

          - /anything/sls-logger

        plugins:

          sls-logger:

            host: "${SLS_HOST}"

            port: 10009

            project: "${SLS_PROJECT}"

            logstore: "${SLS_LOGSTORE}"

            access_key_id: "${SLS_ACCESS_KEY_ID}"

            access_key_secret: "${SLS_ACCESS_KEY_SECRET}"

            batch_max_size: 1

    upstream:

      type: roundrobin

      nodes:

        - host: httpbin.org

          port: 80

          weight: 1
```

Preview the scoped change:

```
adc diff -f adc.yaml \

  --include-resource-type service \

  --label-selector docs-example=sls-logger
```

Review `diff.yaml`, then synchronize the labeled service:

```
adc sync -f adc.yaml \

  --include-resource-type service \

  --label-selector docs-example=sls-logger
```

[Set up the Ingress Controller and gateway](https://docs.api7.ai/ingress-controller/set-up-ingress-controller-and-gateway.md) in the `aic` namespace, then replace the SLS values.

* Gateway API
* APISIX CRD

sls-logger-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: sls-logger-route

spec:

  plugins:

    - name: sls-logger

      config:

        host: your-project.cn-hangzhou.log.aliyuncs.com

        port: 10009

        project: your-project

        logstore: your-logstore

        access_key_id: replace-with-access-key-id

        access_key_secret: replace-with-access-key-secret

        batch_max_size: 1

---

apiVersion: gateway.networking.k8s.io/v1

kind: HTTPRoute

metadata:

  namespace: aic

  name: sls-logger-route

spec:

  parentRefs:

    - name: apisix

  rules:

    - matches:

        - path:

            type: Exact

            value: /anything/sls-logger

      filters:

        - type: ExtensionRef

          extensionRef:

            group: apisix.apache.org

            kind: PluginConfig

            name: sls-logger-route

      backendRefs:

        - name: httpbin-external-domain

          port: 80
```

sls-logger-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: sls-logger-route

spec:

  ingressClassName: apisix

  plugins:

    - name: sls-logger

      enable: true

      config:

        host: your-project.cn-hangzhou.log.aliyuncs.com

        port: 10009

        project: your-project

        logstore: your-logstore

        access_key_id: replace-with-access-key-id

        access_key_secret: replace-with-access-key-secret

        batch_max_size: 1

---

apiVersion: apisix.apache.org/v2

kind: ApisixRoute

metadata:

  namespace: aic

  name: sls-logger-route

spec:

  ingressClassName: apisix

  http:

    - name: sls-logger-route

      match:

        paths:

          - /anything/sls-logger

      backends:

        - serviceName: httpbin-external-domain

          servicePort: 80

      plugin_config_name: sls-logger-route
```

Apply the manifest:

```
kubectl apply -f sls-logger-ic.yaml
```

Send a request:

```
curl "http://127.0.0.1:9080/anything/sls-logger?source=docs"
```

A successful gateway send does not prove that SLS accepted the Project, Logstore, or credentials. Confirm the event in the SLS console: open the Project and Logstore, choose **Search & Analyze**, set a time range that includes the request, and search the `content` field. Create an index for `content` first if the field is not indexed.

```
content: "sls-logger-route"
```

The JSON stored in `content` should include:

```
{

  "request": {

    "method": "GET",

    "uri": "/anything/sls-logger?source=docs"

  },

  "response": {

    "status": 200

  }

}
```

### Send a Focused Custom Format[​](#send-a-focused-custom-format "Direct link to Send a Focused Custom Format")

Update the existing plugin to replace the default entry with selected fields.

* Admin API
* ADC
* Ingress Controller

```
curl "http://127.0.0.1:9180/apisix/admin/routes/sls-logger-route/plugins/sls-logger" -X PATCH \

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

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

  -d @- <<EOF

{

  "host": "${SLS_HOST}",

  "port": 10009,

  "project": "${SLS_PROJECT}",

  "logstore": "${SLS_LOGSTORE}",

  "access_key_id": "${SLS_ACCESS_KEY_ID}",

  "access_key_secret": "${SLS_ACCESS_KEY_SECRET}",

  "batch_max_size": 1,

  "log_format": {

    "method": "\$request_method",

    "uri": "\$request_uri",

    "status": "\$status"

  }

}

EOF
```

In `adc.yaml`, replace the plugin configuration on the existing route:

adc.yaml (route plugin)

```
plugins:

  sls-logger:

    host: "${SLS_HOST}"

    port: 10009

    project: "${SLS_PROJECT}"

    logstore: "${SLS_LOGSTORE}"

    access_key_id: "${SLS_ACCESS_KEY_ID}"

    access_key_secret: "${SLS_ACCESS_KEY_SECRET}"

    batch_max_size: 1

    log_format:

      method: $request_method

      uri: $request_uri

      status: $status
```

Preview the updated service:

```
adc diff -f adc.yaml \

  --include-resource-type service \

  --label-selector docs-example=sls-logger
```

Review `diff.yaml`, then synchronize the labeled service:

```
adc sync -f adc.yaml \

  --include-resource-type service \

  --label-selector docs-example=sls-logger
```

Update the existing plugin configuration with the Kubernetes API used by the deployment:

* Gateway API
* APISIX CRD

sls-logger-ic.yaml

```
apiVersion: apisix.apache.org/v1alpha1

kind: PluginConfig

metadata:

  namespace: aic

  name: sls-logger-route

spec:

  plugins:

    - name: sls-logger

      config:

        host: your-project.cn-hangzhou.log.aliyuncs.com

        port: 10009

        project: your-project

        logstore: your-logstore

        access_key_id: replace-with-access-key-id

        access_key_secret: replace-with-access-key-secret

        batch_max_size: 1

        log_format:

          method: $request_method

          uri: $request_uri

          status: $status
```

sls-logger-ic.yaml

```
apiVersion: apisix.apache.org/v2

kind: ApisixPluginConfig

metadata:

  namespace: aic

  name: sls-logger-route

spec:

  ingressClassName: apisix

  plugins:

    - name: sls-logger

      enable: true

      config:

        host: your-project.cn-hangzhou.log.aliyuncs.com

        port: 10009

        project: your-project

        logstore: your-logstore

        access_key_id: replace-with-access-key-id

        access_key_secret: replace-with-access-key-secret

        batch_max_size: 1

        log_format:

          method: $request_method

          uri: $request_uri

          status: $status
```

Apply the updated plugin configuration:

```
kubectl apply -f sls-logger-ic.yaml
```

Send another request:

```
curl "http://127.0.0.1:9080/anything/sls-logger?source=docs"
```

In **Search & Analyze**, use the same time range and content query to find the latest event:

```
content: "sls-logger-route"
```

The JSON stored in `content` should contain the selected fields:

```
{

  "method": "GET",

  "uri": "/anything/sls-logger?source=docs",

  "status": 200,

  "route_id": "sls-logger-route"

}
```
