AwsLogDriver

class aws_cdk.aws_ecs.AwsLogDriver(*, stream_prefix, datetime_format=None, log_group=None, log_retention=None, mode=None, multiline_pattern=None)

Bases: LogDriver

A log driver that sends log information to CloudWatch Logs.

ExampleMetadata:

infused

Example:

# cluster: ecs.Cluster

# Create a Task Definition for the container to start
task_definition = ecs.Ec2TaskDefinition(self, "TaskDef")
task_definition.add_container("TheContainer",
    image=ecs.ContainerImage.from_asset(path.resolve(__dirname, "..", "eventhandler-image")),
    memory_limit_mi_b=256,
    logging=ecs.AwsLogDriver(stream_prefix="EventDemo", mode=ecs.AwsLogDriverMode.NON_BLOCKING)
)

# An Rule that describes the event trigger (in this case a scheduled run)
rule = events.Rule(self, "Rule",
    schedule=events.Schedule.expression("rate(1 min)")
)

# Pass an environment variable to the container 'TheContainer' in the task
rule.add_target(targets.EcsTask(
    cluster=cluster,
    task_definition=task_definition,
    task_count=1,
    container_overrides=[targets.ContainerOverride(
        container_name="TheContainer",
        environment=[targets.TaskEnvironmentVariable(
            name="I_WAS_TRIGGERED",
            value="From CloudWatch Events"
        )]
    )]
))

Constructs a new instance of the AwsLogDriver class.

Parameters:
  • stream_prefix (str) – Prefix for the log streams. The awslogs-stream-prefix option allows you to associate a log stream with the specified prefix, the container name, and the ID of the Amazon ECS task to which the container belongs. If you specify a prefix with this option, then the log stream takes the following format:: prefix-name/container-name/ecs-task-id

  • datetime_format (Optional[str]) – This option defines a multiline start pattern in Python strftime format. A log message consists of a line that matches the pattern and any following lines that don’t match the pattern. Thus the matched line is the delimiter between log messages. Default: - No multiline matching.

  • log_group (Optional[ILogGroup]) – The log group to log to. Default: - A log group is automatically created.

  • log_retention (Optional[RetentionDays]) – The number of days log events are kept in CloudWatch Logs when the log group is automatically created by this construct. Default: - Logs never expire.

  • mode (Optional[AwsLogDriverMode]) – The delivery mode of log messages from the container to awslogs. Default: - AwsLogDriverMode.BLOCKING

  • multiline_pattern (Optional[str]) – This option defines a multiline start pattern using a regular expression. A log message consists of a line that matches the pattern and any following lines that don’t match the pattern. Thus the matched line is the delimiter between log messages. This option is ignored if datetimeFormat is also configured. Default: - No multiline matching.

Methods

bind(scope, container_definition)

Called when the log driver is configured on a container.

Parameters:
Return type:

LogDriverConfig

Attributes

log_group

The log group to send log streams to.

Only available after the LogDriver has been bound to a ContainerDefinition.

Static Methods

classmethod aws_logs(*, stream_prefix, datetime_format=None, log_group=None, log_retention=None, mode=None, multiline_pattern=None)

Creates a log driver configuration that sends log information to CloudWatch Logs.

Parameters:
  • stream_prefix (str) – Prefix for the log streams. The awslogs-stream-prefix option allows you to associate a log stream with the specified prefix, the container name, and the ID of the Amazon ECS task to which the container belongs. If you specify a prefix with this option, then the log stream takes the following format:: prefix-name/container-name/ecs-task-id

  • datetime_format (Optional[str]) – This option defines a multiline start pattern in Python strftime format. A log message consists of a line that matches the pattern and any following lines that don’t match the pattern. Thus the matched line is the delimiter between log messages. Default: - No multiline matching.

  • log_group (Optional[ILogGroup]) – The log group to log to. Default: - A log group is automatically created.

  • log_retention (Optional[RetentionDays]) – The number of days log events are kept in CloudWatch Logs when the log group is automatically created by this construct. Default: - Logs never expire.

  • mode (Optional[AwsLogDriverMode]) – The delivery mode of log messages from the container to awslogs. Default: - AwsLogDriverMode.BLOCKING

  • multiline_pattern (Optional[str]) – This option defines a multiline start pattern using a regular expression. A log message consists of a line that matches the pattern and any following lines that don’t match the pattern. Thus the matched line is the delimiter between log messages. This option is ignored if datetimeFormat is also configured. Default: - No multiline matching.

Return type:

LogDriver