I'm trying to send Logs directly to Cloudwatch from my Spring Boot Application.
The Logback Appender I'm using, of course needs AWS Credentials.
Since most developers doesn't have AWS Credentials on their local machines and just want to log into a file according to the the logback-spring.xml configuration.
Most tests fail locally, because of missing aws credentials.
Is there a way to initialize the logback appender only for a specific profile?
Here's a snapshot of the logback-spring.xml:
<!-- Configuration for your local environment -->
<springProfile name="${user.name}">
<root level="DEBUG">
<appender-ref ref="ROLLING_FILE" />
</root>
</springProfile>
<appender name="ROLLING_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
...
</appender>
<!-- Configuration for Development Environment -->
<springProfile name="dev">
<root level="DEBUG">
<appender-ref ref="AWS_LOGS_DEV" />
<appender-ref ref="ROLLING_FILE" />
</root>
</springProfile>
<appender name="AWS_LOGS_DEV" class="ca.pjer.logback.AwsLogsAppender">
<layout>
<pattern>${FILE_LOG_PATTERN}</pattern>
</layout>
<logGroupName>/dev</logGroupName>
<logStreamName>log_${date}</logStreamName>
</appender>
This package provides a logback appender that writes its log events to Cloudwatch. Before you say it, there seem to be many projects like this out there but I could find none of them that were self-contained and that were published to the central Maven repo. Code available from the git repository.
The Logback Appender I'm using, of course needs AWS Credentials. Since most developers doesn't have AWS Credentials on their local machines and just want to log into a file according to the the logback-spring.xml configuration. Most tests fail locally, because of missing aws credentials.
The Spring Boot team however recommends using the -spring variant for your logging configuration, logback-spring.xml is preferred over logback.xml. If you use the standard logback.xml configuration, Spring Boot may not be able to completely control log initialization. Here is the code of the logback-spring.xml file.
To include the CloudCaptain Java log appender for AWS CloudWatch Logs in your application all you need to do is include the dependency in your build file. Start by adding the CloudCaptain Maven repository to your list of repositories in your pom.xml:
Already solved. You just need to extract the appender to another logback-file:
logback-spring.xml
<!-- Configuration for your local environment -->
<springProfile name="${user.name}">
<root level="DEBUG">
<appender-ref ref="ROLLING_FILE" />
</root>
</springProfile>
<appender name="ROLLING_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
...
</appender>
<!-- Configuration for Development Environment -->
<springProfile name="dev">
<include resource="logback-prod.xml" />
</springProfile>
logback-prod.xml
<included>
<root level="DEBUG">
<appender-ref ref="AWS_LOGS_DEV" />
<appender-ref ref="ROLLING_FILE" />
</root>
<shutdownHook class="ch.qos.logback.core.hook.DelayingShutdownHook"/>
<appender name="AWS_LOGS_DEV" class="ca.pjer.logback.AwsLogsAppender">
<layout>
<pattern>${FILE_LOG_PATTERN}</pattern>
</layout>
<logGroupName>/prod</logGroupName>
<logStreamName>log_${date}</logStreamName>
<maxBatchLogEvents>200</maxBatchLogEvents>
<maxFlushTimeMillis>30000</maxFlushTimeMillis>
<maxBlockTimeMillis>5000</maxBlockTimeMillis>
</appender>
</included>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With