Skip to content

Commit

Permalink
Keep AWS session
Browse files Browse the repository at this point in the history
  • Loading branch information
sndyuk committed Oct 13, 2019
1 parent 429ac5a commit 042874c
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 20 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Just add it to your dependency then you can use the slf4j module in your applica
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.7.0-JAVA9MODULE_SLF4J17</version>
<version>1.7.1-JAVA9MODULE_SLF4J17</version>
</dependency>
```

Expand All @@ -44,7 +44,7 @@ Just add it to your dependency then you can use the slf4j module in your applica

### Latest changes

##### Version 1.7.0
##### Version 1.7.1

* Fix multithreading problem on CloudWatch and KinesisStream LogbackAppender.

Expand Down Expand Up @@ -75,7 +75,7 @@ Configure your pom.xml:
<dependency>
<groupId>com.sndyuk</groupId>
<artifactId>logback-more-appenders</artifactId>
<version>1.7.0</version>
<version>1.7.1</version>
</dependency>

<!-- [Optional] If you use The CloudWatch appender, You need to add the dependency(aws-java-sdk-logs). -->
Expand Down
2 changes: 1 addition & 1 deletion pom-JAVA9MODULE_SLF4J17.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.sndyuk</groupId>
<artifactId>logback-more-appenders</artifactId>
<version>1.7.0-JAVA9MODULE_SLF4J17</version>
<version>1.7.1-JAVA9MODULE_SLF4J17</version>
<name>logback-more-appenders</name>
<description>logback appenders.</description>
<url>https://github.com/sndyuk/logback-more-appenders</url>
Expand Down
7 changes: 5 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.sndyuk</groupId>
<artifactId>logback-more-appenders</artifactId>
<version>1.7.0</version>
<version>1.7.1</version>
<name>logback-more-appenders</name>
<description>logback appenders.</description>
<url>https://github.com/sndyuk/logback-more-appenders</url>
Expand Down Expand Up @@ -59,7 +59,7 @@
<source>${jdk.version}</source>
<target>${jdk.version}</target>
<excludes>
<exclude>module-info.java</exclude>
<exclude>**/module-info.java</exclude>
</excludes>
</configuration>
</plugin>
Expand Down Expand Up @@ -90,6 +90,9 @@
</executions>
<configuration>
<additionalparam>-Xdoclint:none</additionalparam>
<sourceFileExcludes>
<sourceFileExclude>**/module-info.java</sourceFileExclude>
</sourceFileExcludes>
</configuration>
</plugin>
<plugin>
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/ch/qos/logback/more/appenders/AwsAppender.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import ch.qos.logback.core.UnsynchronizedAppenderBase;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.AWSCredentialsProvider;
import com.amazonaws.auth.DefaultAWSCredentialsProviderChain;
import com.amazonaws.auth.PropertiesCredentials;
import com.amazonaws.auth.profile.ProfileCredentialsProvider;
Expand All @@ -23,6 +24,7 @@
public abstract class AwsAppender<E> extends UnsynchronizedAppenderBase<E> {

protected AwsConfig config;
protected AWSCredentialsProvider credentialsProvider;
protected AWSCredentials credentials;

@Override
Expand All @@ -34,9 +36,9 @@ public void start() {
this.credentials = new PropertiesCredentials(getClass().getClassLoader()
.getResourceAsStream(config.getCredentialFilePath()));
} else if (config.getProfile() != null && config.getProfile().length() > 0) {
this.credentials = new ProfileCredentialsProvider(config.getProfile()).getCredentials();
this.credentialsProvider = new ProfileCredentialsProvider(config.getProfile());
} else {
this.credentials = DefaultAWSCredentialsProviderChain.getInstance().getCredentials();
this.credentialsProvider = DefaultAWSCredentialsProviderChain.getInstance();
}
} catch (Exception e) {
addWarn("Could not initialize " + AwsAppender.class.getCanonicalName()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,19 @@
*/
package ch.qos.logback.more.appenders;

import java.util.Collections;
import java.util.List;
import java.util.UUID;
import ch.qos.logback.core.encoder.EchoEncoder;
import ch.qos.logback.core.encoder.Encoder;
import ch.qos.logback.more.appenders.IntervalEmitter.EventMapper;
import ch.qos.logback.more.appenders.IntervalEmitter.IntervalAppender;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.client.builder.AwsClientBuilder;
import com.amazonaws.services.logs.AWSLogs;
import com.amazonaws.services.logs.AWSLogsClientBuilder;
import com.amazonaws.services.logs.model.*;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.encoder.EchoEncoder;
import ch.qos.logback.core.encoder.Encoder;
import ch.qos.logback.more.appenders.IntervalEmitter.EventMapper;
import ch.qos.logback.more.appenders.IntervalEmitter.IntervalAppender;

import java.util.Collections;
import java.util.List;
import java.util.UUID;

/**
* Appender for CloudWatch. It appends logs for every emitInterval.
Expand Down Expand Up @@ -107,10 +107,13 @@ protected void append(E eventObject) {

private void ensureLogGroup() {
if (this.awsLogs == null) {
this.awsLogs = AWSLogsClientBuilder.standard()
.withCredentials(new AWSStaticCredentialsProvider(credentials))
.withRegion(config.getRegion())
.build();
AWSLogsClientBuilder builder = AWSLogsClientBuilder.standard().withRegion(config.getRegion());
if (credentials != null) {
builder.withCredentials(new AWSStaticCredentialsProvider(credentials));
} else if (credentialsProvider != null) {
builder.withCredentials(credentialsProvider);
}
this.awsLogs = builder.build();
}
DescribeLogGroupsRequest request = new DescribeLogGroupsRequest().withLogGroupNamePrefix(logGroupName).withLimit(1);
DescribeLogGroupsResult result = awsLogs.describeLogGroups(request);
Expand Down

0 comments on commit 042874c

Please sign in to comment.