Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS Cloudwatch Logback Appender for Spring Boot Applications

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>
like image 486
ndueck Avatar asked Sep 18 '17 14:09

ndueck


People also ask

Is there a Logback Appender for CloudWatch?

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.

Does the Logback Appender need AWS credentials?

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.

What is the recommended Logback configuration for Spring Boot?

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.

How do I integrate cloudcaptain Java with AWS Cloudwatch Logs?

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:


1 Answers

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>
like image 162
ndueck Avatar answered Oct 14 '22 15:10

ndueck