Skip to content
This repository was archived by the owner on Jun 4, 2024. It is now read-only.

Commit b9e5101

Browse files
committed
Merge branch '18-lambdalogger' into 'main'
Replace the custom output to stdout with AWS LambdaLogger Closes #18 See merge request bot-by/slf4j-aws-lambda!15
2 parents 3240044 + 46e0404 commit b9e5101

17 files changed

+439
-473
lines changed

example-lambda/pom.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -227,11 +227,11 @@
227227
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
228228
<propertiesEncoding>ISO-8859-1</propertiesEncoding>
229229
<resources-plugin.version>3.3.0</resources-plugin.version>
230-
<revision>1.3.1</revision>
230+
<revision>1.4.0</revision>
231231
<sha1/>
232232
<shade-plugin.version>3.4.1</shade-plugin.version>
233-
<slf4j-aws-lambda.version>3.0.6</slf4j-aws-lambda.version>
234-
<slf4j.version>2.0.5</slf4j.version>
233+
<slf4j-aws-lambda.version>3.1.0-SNAPSHOT</slf4j-aws-lambda.version>
234+
<slf4j.version>2.0.7</slf4j.version>
235235
<surefire-plugin.version>2.22.2</surefire-plugin.version>
236236
<templating-plugin.version>1.0.0</templating-plugin.version>
237237
</properties>

example-lambda/src/main/resources/lambda-logger.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
requestId=${aws-request-id}
2-
log.uk.bot_by.slf4j_demo=warn trace@important
2+
log.uk.bot_by.slf4j_demo=info trace@important
33
# multi-space
44
logLevelSeparator=\\s+
55
# single pipe symbol

pom.xml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,12 @@
249249
<artifactId>annotations</artifactId>
250250
<groupId>org.jetbrains</groupId>
251251
<version>${jetbrains-annotations.version}</version>
252+
<scope>provided</scope>
253+
</dependency>
254+
<dependency>
255+
<artifactId>aws-lambda-java-core</artifactId>
256+
<groupId>com.amazonaws</groupId>
257+
<version>${lambda-core.version}</version>
252258
</dependency>
253259
<dependency>
254260
<artifactId>slf4j-api</artifactId>
@@ -453,6 +459,7 @@
453459
<javadoc-plugin.version>3.5.0</javadoc-plugin.version>
454460
<jetbrains-annotations.version>24.0.1</jetbrains-annotations.version>
455461
<junit-jupiter.version>5.9.2</junit-jupiter.version>
462+
<lambda-core.version>1.2.2</lambda-core.version>
456463
<maven.compiler.release>11</maven.compiler.release>
457464
<minimal-version.java>11</minimal-version.java>
458465
<minimal-version.maven>3.8.1</minimal-version.maven>
@@ -461,7 +468,7 @@
461468
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
462469
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
463470
<propertiesEncoding>ISO-8859-1</propertiesEncoding>
464-
<revision>3.0.6</revision>
471+
<revision>3.1.0</revision>
465472
<sha1/>
466473
<sign-plugin.version>1.0.1</sign-plugin.version>
467474
<site-plugin.version>3.12.1</site-plugin.version>

src/main/java/uk/bot_by/aws_lambda/slf4j/LambdaLogger.java renamed to src/main/java/uk/bot_by/aws_lambda/slf4j/AWSLambdaLogger.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
*/
1616
package uk.bot_by.aws_lambda.slf4j;
1717

18+
import com.amazonaws.services.lambda.runtime.LambdaLogger;
1819
import java.io.ObjectStreamException;
19-
import java.io.PrintStream;
2020
import java.io.Serializable;
2121
import org.jetbrains.annotations.NotNull;
2222
import org.jetbrains.annotations.VisibleForTesting;
@@ -50,19 +50,19 @@
5050
* END RequestId: cc4eb5aa-66b4-42fc-b27a-138bd672b38a
5151
* </code></pre>
5252
*
53-
* @see LoggerConfiguration LambdaLogger's configuration
53+
* @see AWSLambdaLoggerConfiguration LambdaLogger's configuration
5454
*/
55-
public class LambdaLogger implements Logger, Serializable {
55+
public class AWSLambdaLogger implements Logger, Serializable {
5656

5757
private static final long serialVersionUID = 7893093825483346807L;
5858

59-
private final LoggerConfiguration configuration;
60-
private final PrintStream printStream;
59+
private final AWSLambdaLoggerConfiguration configuration;
60+
private final LambdaLogger lambdaLogger;
6161

62-
public LambdaLogger(@NotNull LoggerConfiguration configuration,
63-
@NotNull PrintStream printStream) {
62+
public AWSLambdaLogger(@NotNull AWSLambdaLoggerConfiguration configuration,
63+
@NotNull LambdaLogger lambdaLogger) {
6464
this.configuration = configuration;
65-
this.printStream = printStream;
65+
this.lambdaLogger = lambdaLogger;
6666
}
6767

6868
public String getName() {
@@ -374,15 +374,15 @@ void log(Level level, String message, Throwable throwable) {
374374
if (!isLevelEnabled(level)) {
375375
return;
376376
}
377-
LoggerUtil.log(configuration, printStream, level, message, throwable);
377+
AWSLambdaLoggerUtil.log(configuration, lambdaLogger, level, message, throwable);
378378
}
379379

380380
@VisibleForTesting
381381
void log(Level level, Marker marker, String message, Throwable throwable) {
382382
if (!isLevelEnabled(level, marker)) {
383383
return;
384384
}
385-
LoggerUtil.log(configuration, printStream, level, message, throwable);
385+
AWSLambdaLoggerUtil.log(configuration, lambdaLogger, level, message, throwable);
386386
}
387387

388388
private void formatAndLog(Level level, String format, Object... arguments) {

src/main/java/uk/bot_by/aws_lambda/slf4j/LoggerConfiguration.java renamed to src/main/java/uk/bot_by/aws_lambda/slf4j/AWSLambdaLoggerConfiguration.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
import org.slf4j.Marker;
3030
import org.slf4j.event.Level;
3131

32-
class LoggerConfiguration {
32+
class AWSLambdaLoggerConfiguration {
3333

3434
private static final String DOT = ".";
3535

@@ -43,7 +43,7 @@ class LoggerConfiguration {
4343
private final boolean showThreadId;
4444
private final boolean showThreadName;
4545

46-
private LoggerConfiguration(Builder builder) {
46+
private AWSLambdaLoggerConfiguration(Builder builder) {
4747
dateTimeFormat = builder.dateTimeFormat;
4848
levelInBrackets = builder.levelInBrackets;
4949
loggerPredicates = List.copyOf(builder.loggerPredicates);
@@ -122,11 +122,11 @@ static class Builder {
122122
private Builder() {
123123
}
124124

125-
LoggerConfiguration build() {
125+
AWSLambdaLoggerConfiguration build() {
126126
requireNonNull(loggerPredicates, "Logger level is null");
127127
requireNonNull(name, "Logger name is null");
128128
requireNonNull(requestId, "AWS request ID is null");
129-
return new LoggerConfiguration(this);
129+
return new AWSLambdaLoggerConfiguration(this);
130130
}
131131

132132
Builder dateTimeFormat(@Nullable DateFormat dateTimeFormat) {

src/main/java/uk/bot_by/aws_lambda/slf4j/LambdaLoggerFactory.java renamed to src/main/java/uk/bot_by/aws_lambda/slf4j/AWSLambdaLoggerFactory.java

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
import static java.util.Objects.isNull;
1919
import static java.util.Objects.nonNull;
2020

21+
import com.amazonaws.services.lambda.runtime.LambdaLogger;
22+
import com.amazonaws.services.lambda.runtime.LambdaRuntime;
2123
import java.io.IOException;
2224
import java.io.InputStream;
2325
import java.io.PrintStream;
@@ -36,7 +38,7 @@
3638
import org.slf4j.helpers.Util;
3739

3840
/**
39-
* Responsible for building {@link Logger} using the {@link LambdaLogger} implementation.
41+
* Responsible for building {@link Logger} using the {@link AWSLambdaLogger} implementation.
4042
* <p>
4143
* The configuration is similar to <a
4244
* href="https://www.slf4j.org/api/org/slf4j/simple/SimpleLogger.html">SLF4J Simple</a>.
@@ -100,7 +102,7 @@
100102
* markerSeparator=\\|
101103
* </code></pre>
102104
*/
103-
public class LambdaLoggerFactory implements ILoggerFactory {
105+
public class AWSLambdaLoggerFactory implements ILoggerFactory {
104106

105107
private static final String AT = "@";
106108
private static final String CONFIGURATION_FILE = "lambda-logger.properties";
@@ -112,7 +114,7 @@ public class LambdaLoggerFactory implements ILoggerFactory {
112114

113115
private final ConcurrentMap<String, Logger> loggers;
114116
private final DateFormat dateTimeFormat;
115-
private final List<LoggerLevel> defaultLoggerLevel;
117+
private final List<AWSLambdaLoggerLevel> defaultLoggerLevel;
116118
private final boolean levelInBrackets;
117119
private final String logLevelSeparator;
118120
private final String markerSeparator;
@@ -124,12 +126,12 @@ public class LambdaLoggerFactory implements ILoggerFactory {
124126
private final boolean showThreadId;
125127
private final boolean showThreadName;
126128

127-
public LambdaLoggerFactory() {
129+
public AWSLambdaLoggerFactory() {
128130
this(CONFIGURATION_FILE);
129131
}
130132

131133
@VisibleForTesting
132-
LambdaLoggerFactory(String configurationFile) {
134+
AWSLambdaLoggerFactory(String configurationFile) {
133135
loggers = new ConcurrentHashMap<>();
134136
properties = loadProperties(configurationFile);
135137
dateTimeFormat = getDateTimeFormat(ConfigurationProperty.DateTimeFormat);
@@ -149,19 +151,24 @@ public LambdaLoggerFactory() {
149151
@Override
150152
public Logger getLogger(@NotNull String name) {
151153
return loggers.computeIfAbsent(name, loggerName -> {
152-
var configuration = LoggerConfiguration.builder().name(loggerName)
154+
var configuration = AWSLambdaLoggerConfiguration.builder().name(loggerName)
153155
.dateTimeFormat(dateTimeFormat).levelInBrackets(levelInBrackets).requestId(requestId)
154156
.showDateTime(showDateTime).showLogName(showLogName).showShortLogName(showShortLogName)
155157
.showThreadId(showThreadId).showThreadName(showThreadName);
156158

157-
for (LoggerLevel loggerLevel : getLoggerLevels(name)) {
159+
for (AWSLambdaLoggerLevel loggerLevel : getLoggerLevels(name)) {
158160
configuration.loggerLevel(loggerLevel.getLevel(), loggerLevel.getMarkers());
159161
}
160162

161-
return new LambdaLogger(configuration.build(), getPrintStream());
163+
return new AWSLambdaLogger(configuration.build(), getLambdaLogger());
162164
});
163165
}
164166

167+
@VisibleForTesting
168+
LambdaLogger getLambdaLogger() {
169+
return LambdaRuntime.getLogger();
170+
}
171+
165172
@VisibleForTesting
166173
PrintStream getPrintStream() {
167174
return System.out;
@@ -186,7 +193,8 @@ private DateFormat getDateTimeFormat(ConfigurationProperty configurationProperty
186193
return null;
187194
}
188195

189-
private List<LoggerLevel> getLoggerLevelProperty(ConfigurationProperty configurationProperty) {
196+
private List<AWSLambdaLoggerLevel> getLoggerLevelProperty(
197+
ConfigurationProperty configurationProperty) {
190198
String value = System.getenv(configurationProperty.variableName);
191199

192200
if (nonNull(value)) {
@@ -209,10 +217,11 @@ private List<LoggerLevel> getLoggerLevelProperty(ConfigurationProperty configura
209217
}
210218

211219
return List.of(
212-
LoggerLevel.builder().level(Level.valueOf(configurationProperty.defaultValue)).build());
220+
AWSLambdaLoggerLevel.builder().level(Level.valueOf(configurationProperty.defaultValue))
221+
.build());
213222
}
214223

215-
private List<LoggerLevel> getLoggerLevels(String loggerName) {
224+
private List<AWSLambdaLoggerLevel> getLoggerLevels(String loggerName) {
216225
var name = loggerName;
217226
int indexOfLastDot = name.length();
218227
String loggerLevelString = null;
@@ -223,7 +232,7 @@ private List<LoggerLevel> getLoggerLevels(String loggerName) {
223232
indexOfLastDot = name.lastIndexOf(DOT);
224233
}
225234

226-
List<LoggerLevel> loggerLevels = null;
235+
List<AWSLambdaLoggerLevel> loggerLevels = null;
227236

228237
if (nonNull(loggerLevelString)) {
229238
try {
@@ -286,12 +295,12 @@ private Properties loadProperties(String configurationFile) {
286295
return properties;
287296
}
288297

289-
private List<LoggerLevel> parseLoggerLevelString(String loggerLevelString)
298+
private List<AWSLambdaLoggerLevel> parseLoggerLevelString(String loggerLevelString)
290299
throws IllegalArgumentException {
291-
var loggerLevels = new ArrayList<LoggerLevel>();
300+
var loggerLevels = new ArrayList<AWSLambdaLoggerLevel>();
292301

293302
for (String loggerLevel : loggerLevelString.split(logLevelSeparator)) {
294-
var loggerLevelBuilder = LoggerLevel.builder();
303+
var loggerLevelBuilder = AWSLambdaLoggerLevel.builder();
295304
var loggerLevelWithMarkers = loggerLevel.split(AT);
296305

297306
loggerLevelBuilder.level(Level.valueOf(loggerLevelWithMarkers[0].toUpperCase()));

src/main/java/uk/bot_by/aws_lambda/slf4j/LoggerLevel.java renamed to src/main/java/uk/bot_by/aws_lambda/slf4j/AWSLambdaLoggerLevel.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@
2323
import org.slf4j.event.Level;
2424
import org.slf4j.helpers.BasicMarkerFactory;
2525

26-
class LoggerLevel {
26+
class AWSLambdaLoggerLevel {
2727

2828
private final Level level;
2929
private final Marker[] markers;
3030

31-
private LoggerLevel(Builder builder) {
31+
private AWSLambdaLoggerLevel(Builder builder) {
3232
level = builder.level;
3333
markers = builder.markers.toArray(new Marker[0]);
3434
}
@@ -55,9 +55,9 @@ private Builder() {
5555
markers = new ArrayList<>();
5656
}
5757

58-
LoggerLevel build() {
58+
AWSLambdaLoggerLevel build() {
5959
requireNonNull(level, "Logger level is null");
60-
return new LoggerLevel(this);
60+
return new AWSLambdaLoggerLevel(this);
6161
}
6262

6363
Builder level(Level level) {

0 commit comments

Comments
 (0)