aws-cdk-lib.aws_logs.LogGroup

class LogGroup (construct)

LanguageType name
.NETAmazon.CDK.AWS.Logs.LogGroup
Gogithub.com/aws/aws-cdk-go/awscdk/v2/awslogs#LogGroup
Javasoftware.amazon.awscdk.services.logs.LogGroup
Pythonaws_cdk.aws_logs.LogGroup
TypeScript (source)aws-cdk-lib » aws_logs » LogGroup

Implements IConstruct, IDependable, IResource, ILogGroup, IResourceWithPolicy

Define a CloudWatch Log Group.

Example

import * as logs from 'aws-cdk-lib/aws-logs';

const logGroup = new logs.LogGroup(this, 'Log Group');
const logBucket = new s3.Bucket(this, 'S3 Bucket')

new tasks.EmrContainersStartJobRun(this, 'EMR Containers Start Job Run', {
  virtualCluster: tasks.VirtualClusterInput.fromVirtualClusterId('de92jdei2910fwedz'),
  releaseLabel: tasks.ReleaseLabel.EMR_6_2_0,
  jobDriver: {
    sparkSubmitJobDriver: {
      entryPoint: sfn.TaskInput.fromText('local:///usr/lib/spark/examples/src/main/python/pi.py'),
      sparkSubmitParameters: '--conf spark.executor.instances=2 --conf spark.executor.memory=2G --conf spark.executor.cores=2 --conf spark.driver.cores=1',
    },
  },
  monitoring: {
    logGroup: logGroup,
    logBucket: logBucket,
  },
});

Initializer

new LogGroup(scope: Construct, id: string, props?: LogGroupProps)

Parameters

  • scope Construct
  • id string
  • props LogGroupProps

Construct Props

NameTypeDescription
dataProtectionPolicy?DataProtectionPolicyData Protection Policy for this log group.
encryptionKey?IKeyThe KMS customer managed key to encrypt the log group with.
logGroupName?stringName of the log group.
removalPolicy?RemovalPolicyDetermine the removal policy of this log group.
retention?RetentionDaysHow long, in days, the log contents will be retained.

dataProtectionPolicy?

Type: DataProtectionPolicy (optional, default: no data protection policy)

Data Protection Policy for this log group.


encryptionKey?

Type: IKey (optional, default: Server-side encrpytion managed by the CloudWatch Logs service)

The KMS customer managed key to encrypt the log group with.


logGroupName?

Type: string (optional, default: Automatically generated)

Name of the log group.


removalPolicy?

Type: RemovalPolicy (optional, default: RemovalPolicy.Retain)

Determine the removal policy of this log group.

Normally you want to retain the log group so you can diagnose issues from logs even after a deployment that no longer includes the log group. In that case, use the normal date-based retention policy to age out your logs.


retention?

Type: RetentionDays (optional, default: RetentionDays.TWO_YEARS)

How long, in days, the log contents will be retained.

To retain all logs, set this value to RetentionDays.INFINITE.

Properties

NameTypeDescription
envResourceEnvironmentThe environment this resource belongs to.
logGroupArnstringThe ARN of this log group.
logGroupNamestringThe name of this log group.
nodeNodeThe tree node.
stackStackThe stack in which this resource is defined.

env

Type: ResourceEnvironment

The environment this resource belongs to.

For resources that are created and managed by the CDK (generally, those created by creating new class instances like Role, Bucket, etc.), this is always the same as the environment of the stack they belong to; however, for imported resources (those obtained from static methods like fromRoleArn, fromBucketName, etc.), that might be different than the stack they were imported into.


logGroupArn

Type: string

The ARN of this log group.


logGroupName

Type: string

The name of this log group.


node

Type: Node

The tree node.


stack

Type: Stack

The stack in which this resource is defined.

Methods

NameDescription
addMetricFilter(id, props)Create a new Metric Filter on this Log Group.
addStream(id, props?)Create a new Log Stream for this Log Group.
addSubscriptionFilter(id, props)Create a new Subscription Filter on this Log Group.
addToResourcePolicy(statement)Adds a statement to the resource policy associated with this log group.
applyRemovalPolicy(policy)Apply the given removal policy to this resource.
extractMetric(jsonField, metricNamespace, metricName)Extract a metric from structured log events in the LogGroup.
grant(grantee, ...actions)Give the indicated permissions on this log group and all streams.
grantRead(grantee)Give permissions to read and filter events from this log group.
grantWrite(grantee)Give permissions to create and write to streams in this log group.
logGroupPhysicalName()Public method to get the physical name of this log group.
toString()Returns a string representation of this construct.
static fromLogGroupArn(scope, id, logGroupArn)Import an existing LogGroup given its ARN.
static fromLogGroupName(scope, id, logGroupName)Import an existing LogGroup given its name.

addMetricFilter(id, props)

public addMetricFilter(id: string, props: MetricFilterOptions): MetricFilter

Parameters

  • id string — Unique identifier for the construct in its parent.
  • props MetricFilterOptions — Properties for creating the MetricFilter.

Returns

  • MetricFilter

Create a new Metric Filter on this Log Group.


addStream(id, props?)

public addStream(id: string, props?: StreamOptions): LogStream

Parameters

  • id string — Unique identifier for the construct in its parent.
  • props StreamOptions — Properties for creating the LogStream.

Returns

  • LogStream

Create a new Log Stream for this Log Group.


addSubscriptionFilter(id, props)

public addSubscriptionFilter(id: string, props: SubscriptionFilterOptions): SubscriptionFilter

Parameters

  • id string — Unique identifier for the construct in its parent.
  • props SubscriptionFilterOptions — Properties for creating the SubscriptionFilter.

Returns

  • SubscriptionFilter

Create a new Subscription Filter on this Log Group.


addToResourcePolicy(statement)

public addToResourcePolicy(statement: PolicyStatement): AddToResourcePolicyResult

Parameters

  • statement PolicyStatement — The policy statement to add.

Returns

  • AddToResourcePolicyResult

Adds a statement to the resource policy associated with this log group.

A resource policy will be automatically created upon the first call to addToResourcePolicy.

Any ARN Principals inside of the statement will be converted into AWS Account ID strings because CloudWatch Logs Resource Policies do not accept ARN principals.


applyRemovalPolicy(policy)

public applyRemovalPolicy(policy: RemovalPolicy): void

Parameters

  • policy RemovalPolicy

Apply the given removal policy to this resource.

The Removal Policy controls what happens to this resource when it stops being managed by CloudFormation, either because you've removed it from the CDK application or because you've made a change that requires the resource to be replaced.

The resource can be deleted (RemovalPolicy.DESTROY), or left in your AWS account for data recovery and cleanup later (RemovalPolicy.RETAIN).


extractMetric(jsonField, metricNamespace, metricName)

public extractMetric(jsonField: string, metricNamespace: string, metricName: string): Metric

Parameters

  • jsonField string — JSON field to extract (example: '$.myfield').
  • metricNamespace string — Namespace to emit the metric under.
  • metricName string — Name to emit the metric under.

Returns

  • Metric

Extract a metric from structured log events in the LogGroup.

Creates a MetricFilter on this LogGroup that will extract the value of the indicated JSON field in all records where it occurs.

The metric will be available in CloudWatch Metrics under the indicated namespace and name.


grant(grantee, ...actions)

public grant(grantee: IGrantable, ...actions: string[]): Grant

Parameters

  • grantee IGrantable
  • actions string

Returns

  • Grant

Give the indicated permissions on this log group and all streams.


grantRead(grantee)

public grantRead(grantee: IGrantable): Grant

Parameters

  • grantee IGrantable

Returns

  • Grant

Give permissions to read and filter events from this log group.


grantWrite(grantee)

public grantWrite(grantee: IGrantable): Grant

Parameters

  • grantee IGrantable

Returns

  • Grant

Give permissions to create and write to streams in this log group.


logGroupPhysicalName()

public logGroupPhysicalName(): string

Returns

  • string

Public method to get the physical name of this log group.


toString()

public toString(): string

Returns

  • string

Returns a string representation of this construct.


static fromLogGroupArn(scope, id, logGroupArn)

public static fromLogGroupArn(scope: Construct, id: string, logGroupArn: string): ILogGroup

Parameters

  • scope Construct
  • id string
  • logGroupArn string

Returns

  • ILogGroup

Import an existing LogGroup given its ARN.


static fromLogGroupName(scope, id, logGroupName)

public static fromLogGroupName(scope: Construct, id: string, logGroupName: string): ILogGroup

Parameters

  • scope Construct
  • id string
  • logGroupName string

Returns

  • ILogGroup

Import an existing LogGroup given its name.