Skip to content

Commit 7af12b0

Browse files
committed
logging module independent from implementation, based on slf4j
1 parent bef38d7 commit 7af12b0

34 files changed

+1149
-1653
lines changed

powertools-logging/pom.xml

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,15 @@
33
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
44
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
55
<modelVersion>4.0.0</modelVersion>
6-
7-
<artifactId>powertools-logging</artifactId>
8-
<packaging>jar</packaging>
9-
106
<parent>
117
<artifactId>powertools-parent</artifactId>
128
<groupId>software.amazon.lambda</groupId>
139
<version>1.12.3</version>
1410
</parent>
1511

12+
<artifactId>powertools-logging</artifactId>
13+
<packaging>jar</packaging>
14+
1615
<name>AWS Lambda Powertools for Java library Logging</name>
1716
<description>
1817
A suite of utilities for AWS Lambda Functions that makes tracing with AWS X-Ray, structured logging and creating custom metrics asynchronously easier.
@@ -54,24 +53,8 @@
5453
<artifactId>jackson-databind</artifactId>
5554
</dependency>
5655
<dependency>
57-
<groupId>org.apache.logging.log4j</groupId>
58-
<artifactId>log4j-layout-template-json</artifactId>
59-
</dependency>
60-
<dependency>
61-
<groupId>org.apache.logging.log4j</groupId>
62-
<artifactId>log4j-core</artifactId>
63-
</dependency>
64-
<dependency>
65-
<groupId>org.apache.logging.log4j</groupId>
66-
<artifactId>log4j-slf4j-impl</artifactId>
67-
</dependency>
68-
<dependency>
69-
<groupId>org.apache.logging.log4j</groupId>
70-
<artifactId>log4j-api</artifactId>
71-
</dependency>
72-
<dependency>
73-
<groupId>org.aspectj</groupId>
74-
<artifactId>aspectjrt</artifactId>
56+
<groupId>org.slf4j</groupId>
57+
<artifactId>slf4j-api</artifactId>
7558
</dependency>
7659

7760
<!-- Test dependencies -->
@@ -126,5 +109,4 @@
126109
<scope>test</scope>
127110
</dependency>
128111
</dependencies>
129-
130112
</project>

powertools-logging/src/main/java/software/amazon/lambda/powertools/logging/Logging.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@
4242
* <p>By default {@code Logging} will also create keys for:</p>
4343
*
4444
* <ul>
45-
* <li>coldStart - True if this is the first invocation of this Lambda execution environment; else False</li>
45+
* <li>cold_start - True if this is the first invocation of this Lambda execution environment; else False</li>
4646
* <li>service - The value of the 'POWER_TOOLS_SERVICE_NAME' environment variable or 'service_undefined'</li>
47-
* <li>samplingRate - The value of the 'POWERTOOLS_LOGGER_SAMPLE_RATE' environment variable or value of samplingRate field or 0.
47+
* <li>sampling_rate - The value of the 'POWERTOOLS_LOGGER_SAMPLE_RATE' environment variable or value of sampling_rate field or 0.
4848
* Valid value is from 0.0 to 1.0. Value outside this range is silently ignored.</li>
4949
* </ul>
5050
*

powertools-logging/src/main/java/software/amazon/lambda/powertools/logging/LoggingUtils.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,11 @@
1313
*/
1414
package software.amazon.lambda.powertools.logging;
1515

16-
import java.util.Map;
17-
1816
import com.fasterxml.jackson.databind.ObjectMapper;
19-
import org.apache.logging.log4j.ThreadContext;
17+
import org.slf4j.MDC;
2018

21-
import static java.util.Arrays.asList;
19+
import java.util.Arrays;
20+
import java.util.Map;
2221

2322
/**
2423
* A class of helper functions to add additional functionality to Logging.
@@ -39,7 +38,7 @@ private LoggingUtils() {
3938
* @param value The value to be logged
4039
*/
4140
public static void appendKey(String key, String value) {
42-
ThreadContext.put(key, value);
41+
MDC.put(key, value);
4342
}
4443

4544

@@ -51,7 +50,7 @@ public static void appendKey(String key, String value) {
5150
* @param customKeys Map of custom keys values to be appended to logs
5251
*/
5352
public static void appendKeys(Map<String, String> customKeys) {
54-
ThreadContext.putAll(customKeys);
53+
customKeys.forEach(MDC::put);
5554
}
5655

5756
/**
@@ -60,7 +59,7 @@ public static void appendKeys(Map<String, String> customKeys) {
6059
* @param customKey The name of the key to be logged
6160
*/
6261
public static void removeKey(String customKey) {
63-
ThreadContext.remove(customKey);
62+
MDC.remove(customKey);
6463
}
6564

6665

@@ -70,7 +69,7 @@ public static void removeKey(String customKey) {
7069
* @param keys Map of custom keys values to be appended to logs
7170
*/
7271
public static void removeKeys(String... keys) {
73-
ThreadContext.removeAll(asList(keys));
72+
Arrays.stream(keys).forEach(MDC::remove);
7473
}
7574

7675
/**
@@ -79,7 +78,7 @@ public static void removeKeys(String... keys) {
7978
* @param value The value of the correlation id
8079
*/
8180
public static void setCorrelationId(String value) {
82-
ThreadContext.put("correlation_id", value);
81+
MDC.put("correlation_id", value);
8382
}
8483

8584
/**

0 commit comments

Comments
 (0)