sls-logger
Sends request logs to Alibaba Cloud Log Service.
sls-loggerPriority 406What it does
sls-logger writes the records of the requests that reach the route or other object it is added to, into a logstore of Alibaba Cloud Log Service. It reaches the service at Host and Port, writes into the Logstore of the Project, and signs with the Access key ID and its Access key secret.
The records are collected on the gateway node and sent in batches over a connection that is always TLS. SSL verify decides only whether the service's certificate is checked against the Host.
When to use it
- The company runs its workloads on Alibaba Cloud and searches its logs in Log Service, and the request records of the orders API should join them there.
- The gateway's records belong in a logstore of their own, for example
accessin the projectapi-gateway. - The access key ID stays out of the stored configuration and is read on each gateway node, as
$env://SLS_ACCESS_KEY_IDin the example. - Every route should write the same few fields, the client, method, path, status and time, set once in the plugin metadata.
Not for records sent to an HTTP collector of your own — see http-logger (Sends request logs to an HTTP endpoint in batches), records kept in Elasticsearch — see elasticsearch-logger (Sends request logs to an Elasticsearch index) or records sent to a Kafka topic — see kafka-logger (Sends request logs to a Kafka topic).
Set it up
Write request records to a logstore in Alibaba Cloud Log Service
SLS_ACCESS_KEY_ID is read from the environment of every gateway node when the configuration loads.
Under Basics set Host to cn-hangzhou.log.aliyuncs.com, set Port to 10009, set Project to api-gateway, set Logstore to access, set Access key ID to $env://SLS_ACCESS_KEY_ID and set Access key secret to <secret>.
{
"host": "cn-hangzhou.log.aliyuncs.com",
"port": 10009,
"project": "api-gateway",
"logstore": "access",
"access_key_id": "$env://SLS_ACCESS_KEY_ID",
"access_key_secret": "<secret>"
}
More scenarios
Shape the record every route writes to the logstoreenvironment-wide settings
Fill Log format as in the JSON below.
{
"log_format": {
"client": "$remote_addr",
"method": "$request_method",
"path": "$uri",
"status": "$status",
"at": "$time_iso8601"
}
}
Things to know
- Port — A port of
0or above65535is refused when you save. - Access key ID — Without an access key ID the plugin is refused when you save.
- Access key secret — Without the secret the plugin is refused when you save.
- Timeout — A whole number of milliseconds: a fraction,
1.5for example, is refused when you save. - SSL verify — The connection is always TLS. This switch only decides whether the collector's certificate is checked against the host above.
- Log format extra — Added to the default record only: while a log format is set, on this object or in the plugin's metadata, the engine writes that format alone and reads nothing here.
- Include request body when — Not checked at save: an expression the engine cannot read is stored, and the body is then never logged. Only the gateway's error log says so.
- Include response body when — Not checked at save: an expression the engine cannot read is stored, and the body is then never logged. Only the gateway's error log says so.
- Batch size — A batch goes out when it holds Batch size records, when its first record has waited Buffer duration, or when nothing new arrived for Inactive timeout.
- In the plugin metadata, Pending entry limit caps the backlog. While more entries than this wait to be sent, new ones are dropped rather than queued.
- AdviceKeep SSL verify on.
- AdviceTurn on the request and response bodies only on routes whose logstore is meant to hold what clients and the backend send.
Settings
The fields as the console shows them, section by section.
Basics
hoststringRequiredportintegerRequiredprojectstringRequiredlogstorestringRequiredaccess_key_idstringRequiredaccess_key_secretstringRequiredStored on the engine and shown only to roles that manage this object.
Behavior
timeoutintegerdefault 5000ssl_verifybooleandefault truelog_formatobject{"host":"$host","client":"$remote_addr","status":"$status","at":"$time_iso8601"}log_format_extraobject{"zone":"eu-west","gateway":"$hostname"}include_req_bodybooleandefault falseinclude_req_body_exprarrayinclude_resp_bodybooleandefault falseinclude_resp_body_exprarraymax_req_body_bytesintegerdefault 524288max_resp_body_bytesintegerdefault 524288Advanced
batch_max_sizeintegerdefault 1000buffer_durationintegerdefault 60inactive_timeoutintegerdefault 5max_retry_countintegerdefault 0retry_delayintegerdefault 1namestringdefault sls-loggerEnvironment-wide settings
Set once per environment, under Plugins › sls-logger. Every sls-logger in the environment uses these.
log_formatobject{"host":"$host","client":"$remote_addr","status":"$status","at":"$time_iso8601"}log_format_extraobject{"zone":"eu-west","gateway":"$hostname"}max_pending_entriesintegerdefault 8192How many log entries may wait to be sent. While the backlog is above this number, new entries are dropped rather than queued.