How to log AWS calls details

After a post about request handlers, we considered it’s time to see a good use case where this feature could be used. What if we create a library that logs in a separate file details about a call to AWS, like latency, response code and so on? Maybe these information are useless in 99.99% of cases, but when you have to debug something, any clue is more than welcome. And sooner or later we have to do investigations.

To this purpose, we created a library very simple to integrate in your application that does exactly what we described above: log details found in a file. Let’s see what are the steps:

  1. Declare the dependency in your pom.xml file
<dependency>
 <groupId>cloud.cirrusup</groupId>
 <artifactId>aws-latency-request-log-handler</artifactId>
 <version>1.0.0</version>
</dependency>
  1. Add the request handler to the AWS client.
AwsLatencyRequestLogHandler handler = new AwsLatencyRequestLogHandler();
amazonS3Client.addRequestHandler(handler);
  1. Create a new log appender.
<appender name="AWS-APPENDER-LOG" class="ch.qos.logback.core.rolling.RollingFileAppender">
   <file>/var/log/awsLatency.log</file>
   <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
     <!-- hourly rollover -->
     <fileNamePattern>/var/log/awsLatency/awsLatency.%d{yyyy-MM-dd-HH}.log</fileNamePattern>
     <!-- keep 2 days' worth of history capped at 500MB total size -->
     <maxHistory>2</maxHistory>
     <totalSizeCap>500MB</totalSizeCap>
   </rollingPolicy>
   <encoder>
     <pattern>[%thread] %-5level - %d{yyyy-MM-dd HH:mm:ss} - %logger{36} - %msg%n</pattern>
   </encoder>
 </appender>

 <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
   <encoder>
     <pattern>[%thread] %-5level - %d{yyyy-MM-dd HH:mm:ss} - %logger{36} - %msg%n</pattern>
   </encoder>
 </appender>

  1. Make sure logs are backed in S3 in order to have them later.

The code for this library is available here.

Please add your thoughts in a comment below and if you notice any issue let us know!