# AccessLogFormat

factory methods for access log format.

Obtainable from: AccessLogFormat.clf, AccessLogFormat.custom, AccessLogFormat.jsonWithStandardFields
Returned by: [HttpStage.defaultAccessLogFormat](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_apigatewayv2.HttpStage.html#defaultaccesslogformat), WebSocketStage.defaultAccessLogFormat

- Source: [`aws-apigateway/lib/access-log.ts`](https://github.com/aws/aws-cdk/blob/v2.278.0/packages/aws-cdk-lib/aws-apigateway/lib/access-log.ts#L728)
- Type last changed: v2.222.0 · 4 Nov 2025

## Example

TypeScript:

```ts
const destinationBucket = new s3.Bucket(this, 'Bucket');
const deliveryStreamRole = new iam.Role(this, 'Role', {
  assumedBy: new iam.ServicePrincipal('firehose.amazonaws.com'),
});

const stream = new firehose.CfnDeliveryStream(this, 'MyStream', {
  deliveryStreamName: 'amazon-apigateway-delivery-stream',
  s3DestinationConfiguration: {
    bucketArn: destinationBucket.bucketArn,
    roleArn: deliveryStreamRole.roleArn,
  },
});

const api = new apigateway.RestApi(this, 'books', {
  deployOptions: {
    accessLogDestination: new apigateway.FirehoseLogDestination(stream),
    accessLogFormat: apigateway.AccessLogFormat.jsonWithStandardFields(),
  },
});
```

Python:

```python
destination_bucket = s3.Bucket(self, "Bucket")
delivery_stream_role = iam.Role(self, "Role",
    assumed_by=iam.ServicePrincipal("firehose.amazonaws.com")
)

stream = firehose.CfnDeliveryStream(self, "MyStream",
    delivery_stream_name="amazon-apigateway-delivery-stream",
    s3_destination_configuration=firehose.CfnDeliveryStream.S3DestinationConfigurationProperty(
        bucket_arn=destination_bucket.bucket_arn,
        role_arn=delivery_stream_role.role_arn
    )
)

api = apigateway.RestApi(self, "books",
    deploy_options=apigateway.StageOptions(
        access_log_destination=apigateway.FirehoseLogDestination(stream),
        access_log_format=apigateway.AccessLogFormat.json_with_standard_fields()
    )
)
```

Java:

```java
Bucket destinationBucket = new Bucket(this, "Bucket");
Role deliveryStreamRole = Role.Builder.create(this, "Role")
        .assumedBy(new ServicePrincipal("firehose.amazonaws.com"))
        .build();

CfnDeliveryStream stream = CfnDeliveryStream.Builder.create(this, "MyStream")
        .deliveryStreamName("amazon-apigateway-delivery-stream")
        .s3DestinationConfiguration(S3DestinationConfigurationProperty.builder()
                .bucketArn(destinationBucket.getBucketArn())
                .roleArn(deliveryStreamRole.getRoleArn())
                .build())
        .build();

RestApi api = RestApi.Builder.create(this, "books")
        .deployOptions(StageOptions.builder()
                .accessLogDestination(new FirehoseLogDestination(stream))
                .accessLogFormat(AccessLogFormat.jsonWithStandardFields())
                .build())
        .build();
```

C# / .NET:

```csharp
// Install: dotnet add package Amazon.CDK.Lib
var destinationBucket = new Bucket(this, "Bucket");
var deliveryStreamRole = new Role(this, "Role", new RoleProps {
    AssumedBy = new ServicePrincipal("firehose.amazonaws.com")
});

var stream = new CfnDeliveryStream(this, "MyStream", new CfnDeliveryStreamProps {
    DeliveryStreamName = "amazon-apigateway-delivery-stream",
    S3DestinationConfiguration = new S3DestinationConfigurationProperty {
        BucketArn = destinationBucket.BucketArn,
        RoleArn = deliveryStreamRole.RoleArn
    }
});

var api = new RestApi(this, "books", new RestApiProps {
    DeployOptions = new StageOptions {
        AccessLogDestination = new FirehoseLogDestination(stream),
        AccessLogFormat = AccessLogFormat.JsonWithStandardFields()
    }
});
```

Golang:

```go
// Install: go get github.com/aws/aws-cdk-go/awscdk/v2
destinationBucket := s3.NewBucket(this, jsii.String("Bucket"))
deliveryStreamRole := iam.NewRole(this, jsii.String("Role"), &RoleProps{
	AssumedBy: iam.NewServicePrincipal(jsii.String("firehose.amazonaws.com")),
})

stream := firehose.NewCfnDeliveryStream(this, jsii.String("MyStream"), &CfnDeliveryStreamProps{
	DeliveryStreamName: jsii.String("amazon-apigateway-delivery-stream"),
	S3DestinationConfiguration: &S3DestinationConfigurationProperty{
		BucketArn: destinationBucket.bucketArn,
		RoleArn: deliveryStreamRole.roleArn,
	},
})

api := apigateway.NewRestApi(this, jsii.String("books"), &RestApiProps{
	DeployOptions: &StageOptions{
		AccessLogDestination: apigateway.NewFirehoseLogDestination(stream),
		AccessLogFormat: apigateway.AccessLogFormat_JsonWithStandardFields(),
	},
})
```

## Initializer

TypeScript:

```ts
new AccessLogFormat(format: string)
```

Python:

```python
AccessLogFormat(format: str)
```

Java:

```java
new AccessLogFormat(String format)
```

C# / .NET:

```csharp
new AccessLogFormat(string format)
```

Golang:

```go
NewAccessLogFormat(Format *string)
```

## Methods

- `clf(): AccessLogFormat // chainable` (static) — Generate Common Log Format.

- `custom(format: string): AccessLogFormat // chainable` (static) — Custom log format.

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

  ```ts
   apigateway.AccessLogFormat.custom(JSON.stringify({
       requestId: apigateway.AccessLogField.contextRequestId(),
       sourceIp: apigateway.AccessLogField.contextIdentitySourceIp(),
       method: apigateway.AccessLogField.contextHttpMethod(),
       userContext: {
         sub: apigateway.AccessLogField.contextAuthorizerClaims('sub'),
         email: apigateway.AccessLogField.contextAuthorizerClaims('email')
       }
    }))
  ```

- `jsonWithStandardFields(fields?: JsonWithStandardFieldProps): AccessLogFormat // chainable` (static) — 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.

- `toString(): string` — Output a format string to be used with CloudFormation.
