Skip to main content

SLS Logger

The sls-logger plugin sends gateway request and response logs to Alibaba Cloud Simple Log Service as RFC 5424 events over TCP and TLS.

Examples​

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

Alibaba Cloud SLS 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​

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

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

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​

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

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

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"
}