A standalone Java app that sends logs to CloudWatch Logs with slf4j / logback

Make a note before you forget it. I was making a sample to understand how logs are sent to CloudWatch Logs, but I couldn't find a reference site.

Premise

  1. I don't want to make it as much as possible → Use OSS on Github
  2. I want to send with the minimum code
  3. Gradle compatible

result

Now this.

https://github.com/kojiisd/cloudwatch-logs-java-standalone

OSS used

cloudwatch-logback-appender https://github.com/j256/cloudwatch-logback-appender

How to use

If you edit the contents of logback.xml, you can freely flow the log to some extent.

    <appender name="CLOUDWATCH" class="com.j256.cloudwatchlogbackappender.CloudWatchAppender">
        <region>us-east-1</region>
        <accessKeyId>XXXXXXX</accessKeyId>
        <secretKey>XXXXXXX</secretKey>
        <logGroup>test-loggroup</logGroup>
        <logStream>test-logstream</logStream>
        <layout>
            <pattern>[%d{HH:mm:ss.SSS} [%thread] %-5level %logger{20} - %msg%n</pattern>
        </layout>
        <maxBatchSize>32</maxBatchSize>
    </appender>

accesskeyidOrsecretkeySeems to go to the awsconfig of the execution machine if the item itself is deleted (although I have not tried it)

Execution result

The log was successfully output to CloudWatch Logs. (If the log group or log stream does not exist, it will be created)

スクリーンショット 2019-02-17 19.18.35.png

important point

Obviously, in order to send data to CloudWatch Logs in the library, we will do various initialization and data sending. Since that process is executed in a thread, when the logging process of the caller ends and the application ends, the child thread side also ends automatically, and the log is not sent to CloudWatch Logs. (Actually, I don't think it's possible to send logs with such a single application, so I don't think it's something to worry about) I noticed while looking at the source on the OSS side.

To avoid that, I intentionally put Sleep processing in the implementation for about 20 seconds.

Summary

It was very easy to achieve if I didn't get into the points of caution.

Recommended Posts

A standalone Java app that sends logs to CloudWatch Logs with slf4j / logback
A story that I struggled to challenge a competition professional with Java
How to interact with a server that does not crash the app
How to make an app with a plugin mechanism [C # and Java]
Deploy Java web app to Azure with maven
I tried to break a block with java (1)
Create a docker image that runs a simple Java app
Submit a job to AWS Batch with Java (Eclipse)
Output log to external file with slf4j + logback with Maven
How to get started with creating a Rails app
[Java] How to start a new line with StringBuilder
[Java] logback slf4j sample
I want to write a loop that references an index with Java 8's Stream API
I tried to create a java8 development environment with Chocolatey
Java program to resize a photo into a square with margins
I tried to modernize a Java EE application with OpenShift.
[Rails] I tried to create a mini app with FullCalendar
How to deploy a simple Java Servlet app on Heroku
[Beginner] Try to make a simple RPG game with Java ①
[Beginner] android app that rolls a ball using a sensor [Java]
I want to make a list with kotlin and java!
I want to make a function with kotlin and java!
How to deploy a kotlin (java) app on AWS fargate
Even in Java, I want to output true with a == 1 && a == 2 && a == 3
How to develop and register a Sota app in Java
A memo to start Java programming with VS Code (2020-04 version)
How to deal with the type that I thought about writing a Java program for 2 years
Java to play with Function
Connect to DB with Java
Connect to MySQL 8 with Java
A clever way to unify loggers to SLF4J in Java / build.gradle entry / 2019
How to implement a job that uses Java API in JobScheduler
How to deploy an app that references a local jar to heroku
Connecting to a database with Java (Part 1) Maybe the basic method
Replace with a value according to the match with a Java regular expression
Introduction to Java that can be understood even with Krillin (Part 1)
A story that I wanted to write a process equivalent to a while statement with the Stream API of Java8
When calling sshpass from Java with shell etc., it seems that it is necessary to have a path.