This is a custom writer for tinylog 2 logging framework to send log events to AWS CloudWatch Logs.
AWS SDK for Java 2.0 is used to send log events to AWS CloudWatch Logs
Two different types of message formats are supported:
<dependency>
<groupId>ch.eswitch.tinylog.writers</groupId>
<artifactId>tinylog-aws-cloudwatch-logs-writer</artifactId>
<version>1.0.19</version>
</dependency>
compile(group: 'ch.eswitch.tinylog.writers', name: 'tinylog-aws-cloudwatch-logs-writer', version: '1.0.19', ext: 'pom')
https://artifactory.e-switch.ch/artifactory/libs-release-public
(add this repository to repositories
section in your pom.xml
or build.gradle
)
to use Format Pattern, use the following writer name in tinylog writer configuration:
aws cloud watch logs
see Message Format - Format Pattern for configuration
to use JSON Format, use the following writer name in tinylog writer configuration:
aws cloud watch logs json
see Message Format - JSON Format for configuration
Property logGroupName
and streamName
are mandatory and must be specified in tinylog configuration writer
config.
see Working with log groups and log streams
Log Group must exist.
In case a Log Stream does not exist, it will be created (since 1.0.13).
Class software.amazon.awssdk.auth.credentials.DefaultCredentialsProvider is used for AWS authentication. see AWS Developer Guide how to configure credentials
Properties starting with aws.
in tinylog configuration writer config are read and forwarded
to software.amazon.awssdk.auth.credentials.SystemPropertyCredentialsProvider
Message Formatter (format
property) is supported and can be
used to format message.
Formatting is based on
class org.tinylog.writers.AbstractFormatPatternWriter
Example of tinylog.properties
:
writer_awscloudwatchlogs=aws cloud watch logs
writer_awscloudwatchlogs.level=trace
writer_awscloudwatchlogs.format={{level}|min-size=7} [{thread}] {class}.{method}()\t{context: prefix}{message}
writer_awscloudwatchlogs.logGroupName=myLogGroup
writer_awscloudwatchlogs.streamName=myStream
writer_awscloudwatchlogs.aws.region=eu-central-1
writer_awscloudwatchlogs.aws.accessKeyId=AKIAxxxxxxxxxxxxxxxx
writer_awscloudwatchlogs.aws.secretAccessKey=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
JSON Writer is used and can be used for JSON configuration.
Properties file
, charset
, append
and buffered
are NOT supported.
Formatting is done in the same way as in
class org.tinylog.writers.JsonWriter
Example of tinylog.properties
:
writer_awscloudwatchlogsjson=aws cloud watch logs json
writer_awscloudwatchlogsjson.level=trace
writer_awscloudwatchlogsjson.format=LDJSON
writer_awscloudwatchlogsjson.field.level=level
writer_awscloudwatchlogsjson.field.thread=thread
writer_awscloudwatchlogsjson.field.source={class}.{method}()
writer_awscloudwatchlogsjson.field.message=message
writer_awscloudwatchlogsjson.logGroupName=myLogGroup
writer_awscloudwatchlogsjson.streamName=myStream
writer_awscloudwatchlogsjson.aws.region=eu-central-1
writer_awscloudwatchlogsjson.aws.accessKeyId=AKIAxxxxxxxxxxxxxxxx
writer_awscloudwatchlogsjson.aws.secretAccessKey=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Log event size is limited to 256kB by AWS CloudWatch and can't be changed ( see CloudWatch Logs quotas).
Boolean property splitLargeMessages
controls how to handle larger messages (> 256kB).
If this property is not set (default behaviour), log message text is truncated.
If this property is set, log messages are split into several messages with same timestamp.
Each message is marked with context key part
and value [<part>/<total parts>]
.
HTTP
Servlet ch.eswitch.tinylog.writers.AwsCloudWatchLogsViewerServlet
provides web based access to AWS Log Event with various select options.
This Servlet merges split Log Events into one.
This Servlet is availble on URL "web context"/awslogviewer.