11---
22title : " CloudWatch Logs"
3- linkTitle : " CloudWatch Logs"
43description : Get started with AWS CloudWatch Logs on LocalStack
54tags : ["Free"]
65persistence : supported
76---
87
8+ ## Introduction
9+
910[ CloudWatch Logs] ( https://docs.aws.amazon.com/cloudwatch/index.html ) allows to store and retrieve logs.
1011While some services automatically create and write logs (e.g. Lambda), logs can also be added manually.
1112CloudWatch Logs is available in the Community version.
@@ -23,37 +24,40 @@ In the following we setup a little example on how to use subscription filters wi
2324First, we setup the required resources.
2425Therefore, we create a kinesis stream, a log group and log stream.
2526Then we can configure the subscription filter.
26- {{< command >}}
27- $ awslocal kinesis create-stream --stream-name "logtest" --shard-count 1
28- $ kinesis_arn=$(awslocal kinesis describe-stream --stream-name "logtest" | jq -r .StreamDescription.StreamARN)
29- $ awslocal logs create-log-group --log-group-name test
27+ ``` bash
28+ awslocal kinesis create-stream --stream-name " logtest" --shard-count 1
29+ kinesis_arn=$( awslocal kinesis describe-stream --stream-name " logtest" | jq -r .StreamDescription.StreamARN)
30+
31+ awslocal logs create-log-group --log-group-name test
3032
31- $ awslocal logs create-log-stream \
32- --log-group-name test \
33- --log-stream-name test
33+ awslocal logs create-log-stream \
34+ --log-group-name test \
35+ --log-stream-name test
3436
35- $ awslocal logs put-subscription-filter \
37+ awslocal logs put-subscription-filter \
3638 --log-group-name " test" \
3739 --filter-name " kinesis_test" \
3840 --filter-pattern " " \
3941 --destination-arn $kinesis_arn \
4042 --role-arn " arn:aws:iam::000000000000:role/kinesis_role"
41- {{< / command >}}
43+ ```
44+
45+ Next, we can add a log event, that will be forwarded to Kinesis.
4246
43- Next, we can add a log event, that will be forwarded to kinesis.
44- {{< command >}}
45- $ timestamp=$(($(date +'%s * 1000 + %-N / 1000000')))
46- $ awslocal logs put-log-events --log-group-name test --log-stream-name test --log-events "[ {\" timestamp\" : ${timestamp} , \" message\" : \" hello from cloudwatch\" }] "
47- {{< / command >}}
47+ ``` bash
48+ timestamp=$(( $(date + '% s * 1000 + %- N / 1000000 ')) )
49+ awslocal logs put-log-events --log-group-name test --log-stream-name test --log-events " [{\" timestamp\" : ${timestamp} , \" message\" : \" hello from cloudwatch\" }]"
50+ ```
4851
4952Now we can retrieve the data.
5053In our example, there will only be one record.
5154The data record is base64 encoded and compressed in gzip format:
52- {{< command >}}
53- $ shard_iterator=$(awslocal kinesis get-shard-iterator --stream-name logtest --shard-id shardId-000000000000 --shard-iterator-type TRIM_HORIZON | jq -r .ShardIterator)
54- $ record=$(awslocal kinesis get-records --limit 10 --shard-iterator $shard_iterator | jq -r '.Records[ 0] .Data')
55- $ echo $record | base64 -d | zcat
56- {{< / command >}}
55+
56+ ``` bash
57+ shard_iterator=$( awslocal kinesis get-shard-iterator --stream-name logtest --shard-id shardId-000000000000 --shard-iterator-type TRIM_HORIZON | jq -r .ShardIterator)
58+ record=$( awslocal kinesis get-records --limit 10 --shard-iterator $shard_iterator | jq -r ' .Records[0].Data' )
59+ echo $record | base64 -d | zcat
60+ ```
5761
5862## Filter Pattern (Pro only)
5963
@@ -66,39 +70,42 @@ LocalStack currently supports simple json-property filter.
6670Metric filters can be used to automatically create CloudWatch metrics.
6771
6872In the following example we are interested in logs that include a key-value pair ` "foo": "bar" ` and create a metric filter.
69- {{< command >}}
70- $ awslocal logs create-log-group --log-group-name test-filter
7173
72- $ awslocal logs create-log-stream \
73- --log-group-name test-filter \
74- --log-stream-name test-filter-stream
74+ ``` bash
75+ awslocal logs create-log-group --log-group-name test-filter
76+
77+ awslocal logs create-log-stream \
78+ --log-group-name test-filter \
79+ --log-stream-name test-filter-stream
7580
76- $ awslocal logs put-metric-filter \
81+ awslocal logs put-metric-filter \
7782 --log-group-name test-filter \
7883 --filter-name my-filter \
7984 --filter-pattern " {$.foo = \" bar\" }" \
8085 --metric-transformations \
8186 metricName=MyMetric,metricNamespace=MyNamespace,metricValue=1,defaultValue=0
82- {{< / command >}}
87+ ```
8388
8489Next, we can insert some values:
85- {{< command >}}
86- $ timestamp=$(($(date +'%s * 1000 + %-N / 1000000')))
87- $ awslocal logs put-log-events --log-group-name test-filter \
90+
91+ ``` bash
92+ timestamp=$(( $(date + '% s * 1000 + %- N / 1000000 ')) )
93+ awslocal logs put-log-events --log-group-name test-filter \
8894 --log-stream-name test-filter-stream \
8995 --log-events \
9096 timestamp=$timestamp ,message=' "{\"foo\":\"bar\", \"hello\": \"world\"}"' \
9197 timestamp=$timestamp ,message=" my test event" \
9298 timestamp=$timestamp ,message=' "{\"foo\":\"nomatch\"}"'
93- {{< / command >}}
99+ ```
94100
95101Now we can check that the metric was indeed created:
96- {{< command >}}
102+
103+ ``` bash
97104end=$( date +%s)
98105awslocal cloudwatch get-metric-statistics --namespace MyNamespace \
99106 --metric-name MyMetric --statistics Sum --period 3600 \
100107 --start-time 1659621274 --end-time $end
101- {{< / command >}}
108+ ```
102109
103110### Filter Log Events
104111
@@ -108,37 +115,39 @@ Similarly, you can use filter-pattern to filter logs with different kinds of pat
108115
109116For purely JSON structured log messages, you can use JSON filter patterns to traverse the JSON object.
110117Enclose your pattern in curly braces, like this:
111- {{< command >}}
112- $ awslocal logs filter-log-events --log-group-name test-filter --filter-pattern "{$.foo = \" bar\" }"
113- {{< / command >}}
118+
119+ ``` bash
120+ awslocal logs filter-log-events --log-group-name test-filter --filter-pattern " {$.foo = \" bar\" }"
121+ ```
114122
115123This returns all events whose top level "foo" key has the "bar" value.
116124
117125#### Regular Expression Filter Pattern
118126
119127You can use a simplified regex syntax for regular expression matching.
120128Enclose your pattern in percentage signs like this:
121- {{< command >}}
122- $ awslocal logs filter-log-events --log-group-name test-filter --filter-pattern "\% [ fF] oo\% "
123- {{< / command >}}
129+
130+ ``` bash
131+ awslocal logs filter-log-events --log-group-name test-filter --filter-pattern " \%[fF]oo\%"
132+ ```
133+
124134This returns all events containing "Foo" or "foo".
125135For a complete set of the supported syntax, check [ the official AWS documentation] ( https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/FilterAndPatternSyntax.html#regex-expressions )
126136
127137#### Unstructured Filter Pattern
128138
129139If not specified otherwise in the pattern, we look for a match in the whole event message:
130- {{< command >}}
131- $ awslocal logs filter-log-events --log-group-name test-filter --filter-pattern "foo"
132- {{< / command >}}
140+
141+ ``` bash
142+ awslocal logs filter-log-events --log-group-name test-filter --filter-pattern " foo"
143+ ```
133144
134145## Resource Browser
135146
136147The LocalStack Web Application provides a Resource Browser for exploring CloudWatch Logs.
137148You can access the Resource Browser by opening the LocalStack Web Application in your browser, navigating to the ** Resources** section, and then clicking on ** CloudWatch Logs** under the ** Management/Governance** section.
138149
139- <img src =" logs-resource-browser.png " alt =" CloudWatch Logs Resource Browser " title =" CloudWatch Logs Resource Browser " width =" 900 " />
140- <br >
141- <br >
150+ ![ CloudWatch Logs Resource Browser] ( /images/aws/logs-resource-browser.png )
142151
143152The Resource Browser allows you to perform the following actions:
144153
0 commit comments