Skip to content

Commit 6232ac5

Browse files
committed
Add a AwsLambdaAppender + default component and environment for ENV variables
The AwsLambdaAppender can be used in place of org.jlib:jlib-awslambda-logback for the AWS Lambda case
1 parent e64be45 commit 6232ac5

File tree

5 files changed

+68
-2
lines changed

5 files changed

+68
-2
lines changed

.editorconfig

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# editorconfig.org
2+
3+
root = true
4+
5+
[*]
6+
charset = utf-8
7+
end_of_line = lf
8+
indent_size = 2
9+
indent_style = space
10+
insert_final_newline = true
11+
trim_trailing_whitespace = true
12+
spaces_around_operators = true

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ To ensure `jlink` correctly determines the runtime modules required, add the fol
2424

2525
```java
2626
module my.module {
27-
requires io.avaje.logback.encoder;
27+
requires io.avaje.logback.encoder;
2828
}
2929
```
3030

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package io.avaje.logback.encoder;
2+
3+
import ch.qos.logback.classic.spi.ILoggingEvent;
4+
import ch.qos.logback.core.UnsynchronizedAppenderBase;
5+
import ch.qos.logback.core.encoder.Encoder;
6+
7+
import java.io.IOException;
8+
9+
/**
10+
* Appender to be used for AWS Lambda that defaults to using JsonEncoder.
11+
*/
12+
public final class AwsLambdaAppender extends UnsynchronizedAppenderBase<ILoggingEvent> {
13+
14+
private Encoder<ILoggingEvent> encoder;
15+
16+
public AwsLambdaAppender() {
17+
this.encoder = new JsonEncoder();
18+
}
19+
20+
@Override
21+
protected void append(ILoggingEvent event) {
22+
try {
23+
System.out.write(encoder.encode(event));
24+
} catch (IOException e) {
25+
// NOTE: When actually running on AWS Lambda, an IOException would never happen
26+
e.printStackTrace();
27+
}
28+
}
29+
30+
@Override
31+
public void start() {
32+
encoder.start();
33+
super.start();
34+
}
35+
36+
/**
37+
* Change the encoder from the default JsonEncoder.
38+
*/
39+
public void setEncoder(Encoder<ILoggingEvent> encoder) {
40+
this.encoder = encoder;
41+
}
42+
}

src/main/java/io/avaje/logback/encoder/JsonEncoder.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ public final class JsonEncoder extends EncoderBase<ILoggingEvent> {
3333
public JsonEncoder() {
3434
this.json = JsonStream.builder().build();
3535
this.properties = json.properties("component", "env", "timestamp", "level", "logger", "message", "thread", "stacktrace");
36+
this.component = System.getenv("COMPONENT");
37+
this.environment = System.getenv("ENVIRONMENT");
3638
}
3739

3840
@Override

src/test/java/io/avaje/logback/encoder/JsonEncoderTest.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,16 @@ void throwable_usingConverter() {
9999
assertThat((String)asMap.get("stacktrace")).startsWith("j.l.NullPointerException: ");
100100
}
101101

102+
@Test
103+
void awsAppender() {
104+
AwsLambdaAppender appender = new AwsLambdaAppender();
105+
appender.start();
106+
107+
appender.append(createLogEvent());
108+
appender.append(createLogEvent(createThrowable()));
109+
appender.append(createLogEvent());
110+
}
111+
102112
Throwable createThrowable() {
103113
try {
104114
System.getProperty("doNotExist").toUpperCase();
@@ -107,4 +117,4 @@ Throwable createThrowable() {
107117
return e;
108118
}
109119
}
110-
}
120+
}

0 commit comments

Comments
 (0)