class LogGroup (construct)
Language | Type name |
---|---|
.NET | Amazon.CDK.AWS.Logs.LogGroup |
Go | github.com/aws/aws-cdk-go/awscdk/v2/awslogs#LogGroup |
Java | software.amazon.awscdk.services.logs.LogGroup |
Python | aws_cdk.aws_logs.LogGroup |
TypeScript (source) | aws-cdk-lib » aws_logs » LogGroup |
Implements
IConstruct
, IDependable
, IResource
, ILog
, IResource
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
Log
Group Props
Construct Props
Name | Type | Description |
---|---|---|
data | Data | Data Protection Policy for this log group. |
encryption | IKey | The KMS customer managed key to encrypt the log group with. |
log | Log | The class of the log group. Possible values are: STANDARD and INFREQUENT_ACCESS. |
log | string | Name of the log group. |
removal | Removal | Determine the removal policy of this log group. |
retention? | Retention | How long, in days, the log contents will be retained. |
dataProtectionPolicy?
Type:
Data
(optional, default: no data protection policy)
Data Protection Policy for this log group.
encryptionKey?
Type:
IKey
(optional, default: Server-side encryption managed by the CloudWatch Logs service)
The KMS customer managed key to encrypt the log group with.
logGroupClass?
Type:
Log
(optional, default: LogGroupClass.STANDARD)
The class of the log group. Possible values are: STANDARD and INFREQUENT_ACCESS.
INFREQUENT_ACCESS class provides customers a cost-effective way to consolidate logs which supports querying using Logs Insights. The logGroupClass property cannot be changed once the log group is created.
logGroupName?
Type:
string
(optional, default: Automatically generated)
Name of the log group.
removalPolicy?
Type:
Removal
(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:
Retention
(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
Name | Type | Description |
---|---|---|
env | Resource | The environment this resource belongs to. |
log | string | The ARN of this log group. |
log | string | The name of this log group. |
node | Node | The tree node. |
stack | Stack | The stack in which this resource is defined. |
env
Type:
Resource
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
Name | Description |
---|---|
add | Create a new Metric Filter on this Log Group. |
add | Create a new Log Stream for this Log Group. |
add | Create a new Subscription Filter on this Log Group. |
add | Adds a statement to the resource policy associated with this log group. |
apply | Apply the given removal policy to this resource. |
extract | Extract a metric from structured log events in the LogGroup. |
grant(grantee, ...actions) | Give the indicated permissions on this log group and all streams. |
grant | Give permissions to read and filter events from this log group. |
grant | Give permissions to create and write to streams in this log group. |
log | Public method to get the physical name of this log group. |
metric(metricName, props?) | Creates a CloudWatch metric for this log group. |
metric | Creates a CloudWatch metric for the volume of incoming log data in bytes to this log group. |
metric | Creates a CloudWatch metric for the number of incoming log events to this log group. |
to | Returns a string representation of this construct. |
static from | Import an existing LogGroup given its ARN. |
static from | 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
Metric
— Properties for creating the MetricFilter.Filter Options
Returns
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
Stream
— Properties for creating the LogStream.Options
Returns
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
Subscription
— Properties for creating the SubscriptionFilter.Filter Options
Returns
Create a new Subscription Filter on this Log Group.
addToResourcePolicy(statement)
public addToResourcePolicy(statement: PolicyStatement): AddToResourcePolicyResult
Parameters
- statement
Policy
— The policy statement to add.Statement
Returns
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
Removal
Policy
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
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
Give the indicated permissions on this log group and all streams.
grantRead(grantee)
public grantRead(grantee: IGrantable): Grant
Parameters
- grantee
IGrantable
Returns
Give permissions to read and filter events from this log group.
grantWrite(grantee)
public grantWrite(grantee: IGrantable): Grant
Parameters
- grantee
IGrantable
Returns
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.
metric(metricName, props?)
public metric(metricName: string, props?: MetricOptions): Metric
Parameters
- metricName
string
— - The name of the metric to create. - props
Metric
— - Optional.Options
Returns
Creates a CloudWatch metric for this log group.
metricIncomingBytes(props?)
public metricIncomingBytes(props?: MetricOptions): Metric
Parameters
- props
Metric
— - Optional.Options
Returns
Creates a CloudWatch metric for the volume of incoming log data in bytes to this log group.
metricIncomingLogEvents(props?)
public metricIncomingLogEvents(props?: MetricOptions): Metric
Parameters
- props
Metric
— - Optional.Options
Returns
Creates a CloudWatch metric for the number of incoming log events to 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
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
Import an existing LogGroup given its name.