AccessLogFormat

class aws_cdk.aws_apigateway.AccessLogFormat(*args: Any, **kwargs)

Bases: object

factory methods for access log format.

ExampleMetadata:

infused

Example:

log_group = logs.LogGroup(self, "ApiGatewayAccessLogs")
apigateway.RestApi(self, "books",
    deploy_options=apigateway.StageOptions(
        access_log_destination=apigateway.LogGroupLogDestination(log_group),
        access_log_format=apigateway.AccessLogFormat.custom(f"{apigateway.AccessLogField.contextRequestId()} {apigateway.AccessLogField.contextErrorMessage()} {apigateway.AccessLogField.contextErrorMessageString()}")
    )
)

Methods

to_string()

Output a format string to be used with CloudFormation.

Return type:

str

Static Methods

classmethod clf()

Generate Common Log Format.

Return type:

AccessLogFormat

classmethod custom(format)

Custom log format.

You can create any log format string. You can easily get the $ context variable by using the methods of AccessLogField.

Parameters:

format (str) –

Return type:

AccessLogFormat

Example:

apigateway.AccessLogFormat.custom(JSON.stringify({
    "request_id": apigateway.AccessLogField.context_request_id(),
    "source_ip": apigateway.AccessLogField.context_identity_source_ip(),
    "method": apigateway.AccessLogField.context_http_method(),
    "user_context": {
        "sub": apigateway.AccessLogField.context_authorizer_claims("sub"),
        "email": apigateway.AccessLogField.context_authorizer_claims("email")
    }
}))
classmethod json_with_standard_fields(*, caller, http_method, ip, protocol, request_time, resource_path, response_length, status, user)

Access log will be produced in the JSON format with a set of fields most useful in the access log.

All fields are turned on by default with the option to turn off specific fields.

Parameters:
  • caller (bool) – If this flag is enabled, the principal identifier of the caller will be output to the log.

  • http_method (bool) – If this flag is enabled, the http method will be output to the log.

  • ip (bool) – If this flag is enabled, the source IP of request will be output to the log.

  • protocol (bool) – If this flag is enabled, the request protocol will be output to the log.

  • request_time (bool) – If this flag is enabled, the CLF-formatted request time((dd/MMM/yyyy:HH:mm:ss +-hhmm) will be output to the log.

  • resource_path (bool) – If this flag is enabled, the path to your resource will be output to the log.

  • response_length (bool) – If this flag is enabled, the response payload length will be output to the log.

  • status (bool) – If this flag is enabled, the method response status will be output to the log.

  • user (bool) – If this flag is enabled, the principal identifier of the user will be output to the log.

Return type:

AccessLogFormat