Skip to main content

The CDK Construct Library for AWS::Logs

Project description

Amazon CloudWatch Logs Construct Library


Stability: Experimental

This API is still under active development and subject to non-backward compatible changes or removal in any future version. Use of the API is not recommended in production environments. Experimental APIs are not subject to the Semantic Versioning model.


This library supplies constructs for working with CloudWatch Logs.

Log Groups/Streams

The basic unit of CloudWatch is a Log Group. Every log group typically has the same kind of data logged to it, in the same format. If there are multiple applications or services logging into the Log Group, each of them creates a new Log Stream.

Every log operation creates a "log event", which can consist of a simple string or a single-line JSON object. JSON objects have the advantage that they afford more filtering abilities (see below).

The only configurable attribute for log streams is the retention period, which configures after how much time the events in the log stream expire and are deleted.

The default retention period if not supplied is 2 years, but it can be set to any amount of days, or Infinity to keep the data in the log group forever.

// Configure log group for short retention
const logGroup = new LogGroup(stack, 'LogGroup', {
  retentionDays: RetentionDays.OneWeek
});
// Configure log group for infinite retention
const logGroup = new LogGroup(stack, 'LogGroup', {
  retentionDays: Infinity
});

Subscriptions and Destinations

Log events matching a particular filter can be sent to either a Lambda function or a Kinesis stream.

If the Kinesis stream lives in a different account, a CrossAccountDestination object needs to be added in the destination account which will act as a proxy for the remote Kinesis stream. This object is automatically created for you if you use the CDK Kinesis library.

Create a SubscriptionFilter, initialize it with an appropriate Pattern (see below) and supply the intended destination:

const fn = new lambda.Function(this, 'Lambda', { ... });
const logGroup = new LogGroup(this, 'LogGroup', { ... });

new SubscriptionFilter(this, 'Subscription', {
    logGroup,
    destination: fn,
    filterPattern: FilterPattern.allTerms("ERROR", "MainThread")
});

Metric Filters

CloudWatch Logs can extract and emit metrics based on a textual log stream. Depending on your needs, this may be a more convenient way of generating metrics for you application than making calls to CloudWatch Metrics yourself.

A MetricFilter either emits a fixed number every time it sees a log event matching a particular pattern (see below), or extracts a number from the log event and uses that as the metric value.

Example:

new MetricFilter(this, 'MetricFilter', {
  logGroup,
  metricNamespace: 'MyApp',
  metricName: 'Latency',
  filterPattern: FilterPattern.exists('$.latency'),
  metricValue: '$.latency'
});

Remember that if you want to use a value from the log event as the metric value, you must mention it in your pattern somewhere.

A very simple MetricFilter can be created by using the logGroup.extractMetric() helper function:

logGroup.extractMetric('$.jsonField', 'Namespace', 'MetricName');

Will extract the value of jsonField wherever it occurs in JSON-structed log records in the LogGroup, and emit them to CloudWatch Metrics under the name Namespace/MetricName.

Patterns

Patterns describe which log events match a subscription or metric filter. There are three types of patterns:

  • Text patterns
  • JSON patterns
  • Space-delimited table patterns

All patterns are constructed by using static functions on the FilterPattern class.

In addition to the patterns above, the following special patterns exist:

  • FilterPattern.allEvents(): matches all log events.
  • FilterPattern.literal(string): if you already know what pattern expression to use, this function takes a string and will use that as the log pattern. For more information, see the Filter and Pattern Syntax.

Text Patterns

Text patterns match if the literal strings appear in the text form of the log line.

  • FilterPattern.allTerms(term, term, ...): matches if all of the given terms (substrings) appear in the log event.
  • FilterPattern.anyTerm(term, term, ...): matches if all of the given terms (substrings) appear in the log event.
  • FilterPattern.anyGroup([term, term, ...], [term, term, ...], ...): matches if all of the terms in any of the groups (specified as arrays) matches. This is an OR match.

Examples:

// Search for lines that contain both "ERROR" and "MainThread"
const pattern1 = FilterPattern.allTerms('ERROR', 'MainThread');

// Search for lines that either contain both "ERROR" and "MainThread", or
// both "WARN" and "Deadlock".
const pattern2 = FilterPattern.anyGroup(
    ['ERROR', 'MainThread'],
    ['WARN', 'Deadlock'],
    );

JSON Patterns

JSON patterns apply if the log event is the JSON representation of an object (without any other characters, so it cannot include a prefix such as timestamp or log level). JSON patterns can make comparisons on the values inside the fields.

  • Strings: the comparison operators allowed for strings are = and !=. String values can start or end with a * wildcard.
  • Numbers: the comparison operators allowed for numbers are =, !=, <, <=, >, >=.

Fields in the JSON structure are identified by identifier the complete object as $ and then descending into it, such as $.field or $.list[0].field.

  • FilterPattern.stringValue(field, comparison, string): matches if the given field compares as indicated with the given string value.
  • FilterPattern.numberValue(field, comparison, number): matches if the given field compares as indicated with the given numerical value.
  • FilterPattern.isNull(field): matches if the given field exists and has the value null.
  • FilterPattern.notExists(field): matches if the given field is not in the JSON structure.
  • FilterPattern.exists(field): matches if the given field is in the JSON structure.
  • FilterPattern.booleanValue(field, boolean): matches if the given field is exactly the given boolean value.
  • FilterPattern.all(jsonPattern, jsonPattern, ...): matches if all of the given JSON patterns match. This makes an AND combination of the given patterns.
  • FilterPattern.any(jsonPattern, jsonPattern, ...): matches if any of the given JSON patterns match. This makes an OR combination of the given patterns.

Example:

// Search for all events where the component field is equal to
// "HttpServer" and either error is true or the latency is higher
// than 1000.
const pattern = FilterPattern.all(
    FilterPattern.stringValue('$.component', '=', 'HttpServer'),
    FilterPattern.any(
        FilterPattern.booleanValue('$.error', true),
        FilterPattern.numberValue('$.latency', '>', 1000)
    ));

Space-delimited table patterns

If the log events are rows of a space-delimited table, this pattern can be used to identify the columns in that structure and add conditions on any of them. The canonical example where you would apply this type of pattern is Apache server logs.

Text that is surrounded by "..." quotes or [...] square brackets will be treated as one column.

  • FilterPattern.spaceDelimited(column, column, ...): construct a SpaceDelimitedTextPattern object with the indicated columns. The columns map one-by-one the columns found in the log event. The string "..." may be used to specify an arbitrary number of unnamed columns anywhere in the name list (but may only be specified once).

After constructing a SpaceDelimitedTextPattern, you can use the following two members to add restrictions:

  • pattern.whereString(field, comparison, string): add a string condition. The rules are the same as for JSON patterns.
  • pattern.whereNumber(field, comparison, number): add a numerical condition. The rules are the same as for JSON patterns.

Multiple restrictions can be added on the same column; they must all apply.

Example:

// Search for all events where the component is "HttpServer" and the
// result code is not equal to 200.
const pattern = FilterPattern.spaceDelimited('time', 'component', '...', 'result_code', 'latency')
    .whereString('component', '=', 'HttpServer')
    .whereNumber('result_code', '!=', 200);

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

aws-cdk.aws-logs-0.34.0.tar.gz (95.6 kB view details)

Uploaded Source

Built Distribution

aws_cdk.aws_logs-0.34.0-py3-none-any.whl (90.3 kB view details)

Uploaded Python 3

File details

Details for the file aws-cdk.aws-logs-0.34.0.tar.gz.

File metadata

  • Download URL: aws-cdk.aws-logs-0.34.0.tar.gz
  • Upload date:
  • Size: 95.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/39.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.6.5

File hashes

Hashes for aws-cdk.aws-logs-0.34.0.tar.gz
Algorithm Hash digest
SHA256 9c3a98e72d9206dab7a6478a2a4b14cf007364db343ed4416a42457cbea21383
MD5 394a3d2fc58bd40ae2a4a610b940afce
BLAKE2b-256 21d97e86f882814a73cfda58933e7b2ac7b2e1efcf11add8edbef659e7e8afc0

See more details on using hashes here.

File details

Details for the file aws_cdk.aws_logs-0.34.0-py3-none-any.whl.

File metadata

  • Download URL: aws_cdk.aws_logs-0.34.0-py3-none-any.whl
  • Upload date:
  • Size: 90.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/39.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.6.5

File hashes

Hashes for aws_cdk.aws_logs-0.34.0-py3-none-any.whl
Algorithm Hash digest
SHA256 99a848facfc72715474f9f43eeaf4de126fb69eef3e63780557dcbd8e0b45e33
MD5 7f6aa1ea730b3549b069eccc955265ba
BLAKE2b-256 80e37e004388454a7ae521dfb5a2351ba407a0425f3895f5e533719b158aebe3

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page