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

Commit 2cdca8f

Browse files
committed
Merge branch '1-aws-request-id-is-configurable' into 'main'
Resolve "AWS Request ID is configurable" Closes #2 and #1 See merge request bot-by/slf4j-aws-lambda!2
2 parents e80b767 + 5a02e3c commit 2cdca8f

22 files changed

+280
-149
lines changed

.gitlab-ci.yml

+5-9
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ variables:
44

55
image: maven:3-openjdk-11
66

7+
include:
8+
- template: Security/SAST.gitlab-ci.yml
9+
710
cache:
811
paths:
912
- .m2/repository
@@ -31,17 +34,10 @@ test:
3134
except:
3235
- tags
3336

34-
# You can override the included template(s) by including variable overrides
35-
# SAST customization: https://docs.gitlab.com/ee/user/application_security/sast/#customizing-the-sast-settings
36-
# Secret Detection customization: https://docs.gitlab.com/ee/user/application_security/secret_detection/#customizing-settings
37-
# Dependency Scanning customization: https://docs.gitlab.com/ee/user/application_security/dependency_scanning/#customizing-the-dependency-scanning-settings
38-
# Container Scanning customization: https://docs.gitlab.com/ee/user/application_security/container_scanning/#customizing-the-container-scanning-settings
39-
# Note that environment variables can be set in several places
40-
# See https://docs.gitlab.com/ee/ci/variables/#cicd-variable-precedence
4137
sast:
4238
stage: test
43-
include:
44-
- template: Security/SAST.gitlab-ci.yml
39+
variables:
40+
SAST_EXCLUDED_PATHS: jacoco-resources,prism.js
4541

4642
spotbugs-sast:
4743
variables:

.idea/misc.xml

-5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

changelog.md

+4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

77
## Unreleased
8+
### Added
9+
- Name of AWS request ID is configurable
10+
### Fixed
11+
- CRLF injection issue #2
812

913
## 1.0.1 - 2022-05-06
1014
### Changed

example/pom.xml

+33-2
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151

5252
<properties>
5353
<!-- version -->
54-
<revision>1.0.1</revision>
54+
<revision>1.1.0</revision>
5555
<changelist>-SNAPSHOT</changelist>
5656
<sha1/>
5757
<!-- java and maven -->
@@ -64,6 +64,8 @@
6464
<!-- plugins -->
6565
<enforcer-plugin.version>3.0.0</enforcer-plugin.version>
6666
<clean-plugin.version>3.2.0</clean-plugin.version>
67+
<templating-plugin.version>1.0.0</templating-plugin.version>
68+
<resources-plugin.version>3.2.0</resources-plugin.version>
6769
<compiler-plugin.version>3.10.1</compiler-plugin.version>
6870
<surefire-plugin.version>2.22.2</surefire-plugin.version>
6971
<shade-plugin.version>3.3.0</shade-plugin.version>
@@ -72,7 +74,9 @@
7274
<lambda-core.version>1.2.1</lambda-core.version>
7375
<lambda-events.version>3.11.0</lambda-events.version>
7476
<slf4j.version>1.7.36</slf4j.version>
75-
<slf4j-aws-lambda.version>1.0.1</slf4j-aws-lambda.version>
77+
<slf4j-aws-lambda.version>1.1.0</slf4j-aws-lambda.version>
78+
<!-- other -->
79+
<aws-request-id>request#</aws-request-id>
7680
</properties>
7781

7882
<dependencies>
@@ -105,6 +109,12 @@
105109

106110
<build>
107111
<defaultGoal>clean verify</defaultGoal>
112+
<resources>
113+
<resource>
114+
<directory>src/main/resources</directory>
115+
<filtering>true</filtering>
116+
</resource>
117+
</resources>
108118
<plugins>
109119
<plugin>
110120
<groupId>org.apache.maven.plugins</groupId>
@@ -141,6 +151,27 @@
141151
<artifactId>maven-clean-plugin</artifactId>
142152
<version>${clean-plugin.version}</version>
143153
</plugin>
154+
<plugin>
155+
<groupId>org.codehaus.mojo</groupId>
156+
<artifactId>templating-maven-plugin</artifactId>
157+
<version>${templating-plugin.version}</version>
158+
<executions>
159+
<execution>
160+
<id>filter-sources</id>
161+
<goals>
162+
<goal>filter-sources</goal>
163+
</goals>
164+
</execution>
165+
</executions>
166+
</plugin>
167+
<plugin>
168+
<groupId>org.apache.maven.plugins</groupId>
169+
<artifactId>maven-resources-plugin</artifactId>
170+
<version>${resources-plugin.version}</version>
171+
<configuration>
172+
<propertiesEncoding>${propertiesEncoding}</propertiesEncoding>
173+
</configuration>
174+
</plugin>
144175
<plugin>
145176
<groupId>org.apache.maven.plugins</groupId>
146177
<artifactId>maven-compiler-plugin</artifactId>

example/src/main/java/uk/bot_by/bot/slf4j_demo/BotHandler.java renamed to example/src/main/java-templates/uk/bot_by/bot/slf4j_demo/BotHandler.java

+10-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
package uk.bot_by.bot.slf4j_demo;
22

3-
import static uk.bot_by.aws_lambda.slf4j.LambdaLogger.AWS_REQUEST_ID;
4-
53
import com.amazonaws.services.lambda.runtime.Context;
64
import com.amazonaws.services.lambda.runtime.RequestHandler;
75
import java.util.Map;
6+
import java.util.stream.Stream;
87
import org.slf4j.Logger;
98
import org.slf4j.LoggerFactory;
109
import org.slf4j.MDC;
@@ -13,14 +12,22 @@ public class BotHandler implements RequestHandler<Map<String, Object>, String> {
1312

1413
private final Logger logger = LoggerFactory.getLogger(getClass());
1514

15+
{
16+
logger.info("Load version: @project.version@");
17+
}
18+
1619
@Override
1720
public String handleRequest(Map<String, Object> input, Context context) {
18-
MDC.put(AWS_REQUEST_ID, context.getAwsRequestId());
21+
MDC.put("@aws-request-id@", context.getAwsRequestId());
1922
logger.trace("trace message");
2023
logger.debug("debug message");
2124
logger.info("info message");
2225
logger.warn("warning message");
2326
logger.error("error message");
27+
Stream.of("\n", "\r\n", "\r").forEach(injection -> {
28+
logger.info("CRLF{}injection", injection);
29+
});
30+
logger.warn("printable stacktrace", new Throwable("Printable Stacktrace Demo"));
2431
return "done";
2532
}
2633

Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
requestId=${aws-request-id}

pom.xml

+13-3
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666

6767
<properties>
6868
<!-- version -->
69-
<revision>1.0.2</revision>
69+
<revision>1.1.0</revision>
7070
<changelist>-SNAPSHOT</changelist>
7171
<sha1/>
7272
<!-- java and maven -->
@@ -97,11 +97,13 @@
9797
<mockito.version>4.6.1</mockito.version>
9898
<hamcrest.version>2.2</hamcrest.version>
9999
<system-stubs.version>2.0.1</system-stubs.version>
100+
<commons-text.version>1.9</commons-text.version>
100101
<!-- dependencies -->
101102
<jetbrains-annotations.version>23.0.0</jetbrains-annotations.version>
102103
<slf4j.version>1.7.36</slf4j.version>
103104
<!-- other -->
104-
<package.registry.url>https://gitlab.com/bot-by/slf4j-aws-lambda/-/packages</package.registry.url>
105+
<package.registry.url>https://gitlab.com/bot-by/slf4j-aws-lambda/-/packages
106+
</package.registry.url>
105107
</properties>
106108

107109
<dependencies>
@@ -164,6 +166,12 @@
164166
<version>${system-stubs.version}</version>
165167
<scope>test</scope>
166168
</dependency>
169+
<dependency>
170+
<groupId>org.apache.commons</groupId>
171+
<artifactId>commons-text</artifactId>
172+
<version>${commons-text.version}</version>
173+
<scope>test</scope>
174+
</dependency>
167175
</dependencies>
168176

169177
<build>
@@ -457,7 +465,9 @@
457465
<script type="text/javascript" src="{@docRoot}/resources/prism.js"></script>
458466
]]></bottom>
459467
<additionalJOptions>
460-
<additionalJOption>-J-Dhttp.agent=maven-javadoc-plugin-${project.artifactId}</additionalJOption>
468+
<additionalJOption>
469+
-J-Dhttp.agent=maven-javadoc-plugin_${project.groupId}:${project.artifactId}
470+
</additionalJOption>
461471
</additionalJOptions>
462472
<additionalOptions>
463473
<additionalOption>--allow-script-in-comments</additionalOption>

readme.md

+17-27
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ An [SLF4J][] Logger implementation for [AWS Lambda][lambda].
44

55
[![Codacy Grade](https://app.codacy.com/project/badge/Grade/dda626a02daf464c94aa10955a6b8f6b)](https://www.codacy.com/gl/bot-by/slf4j-aws-lambda/dashboard?utm_source=gitlab.com&utm_medium=referral&utm_content=bot-by/slf4j-aws-lambda&utm_campaign=Badge_Grade)
66
[![Codacy Coverage](https://app.codacy.com/project/badge/Coverage/dda626a02daf464c94aa10955a6b8f6b)](https://www.codacy.com/gl/bot-by/slf4j-aws-lambda/dashboard?utm_source=gitlab.com&utm_medium=referral&utm_content=bot-by/slf4j-aws-lambda&utm_campaign=Badge_Coverage)
7+
[![Maven Central](https://img.shields.io/maven-central/v/uk.bot-by/slf4j-aws-lambda)](https://search.maven.org/artifact/uk.bot-by/slf4j-aws-lambda)
78

89
## Getting started
910

@@ -16,14 +17,14 @@ The sample code, see the folder **[example](example)** :
1617

1718
```java
1819
@Override
19-
public String handleRequest(Map<String, Object> input,Context context){
20-
MDC.put(LambdaLogger.AWS_REQUEST_ID, context.getAwsRequestId());
21-
logger.trace("trace message");
22-
logger.debug("debug message");
23-
logger.info("info message");
24-
logger.warn("warning message");
25-
logger.error("error message");
26-
return"done";
20+
public String handleRequest(Map<String, Object> input,Context context) {
21+
MDC.put("AWS_REQUEST_ID", context.getAwsRequestId());
22+
logger.trace("trace message");
23+
logger.debug("debug message");
24+
logger.info("info message");
25+
logger.warn("warning message");
26+
logger.error("error message");
27+
return"done";
2728
}
2829
```
2930

@@ -40,22 +41,6 @@ END RequestId: cc4eb5aa-66b4-42fc-b27a-138bd672b38a
4041
The footprint of **slf4j-aws-lambda** (68K) is same size as **slf4j-simple** (64K) and much smaller
4142
than **logback** (716K).
4243

43-
## Acquire
44-
45-
![Maven Central](https://img.shields.io/maven-central/v/uk.bot-by/slf4j-aws-lambda)
46-
47-
Please add dependency to your project:
48-
49-
```xml
50-
<dependency>
51-
<groupId>uk.bot-by</groupId>
52-
<artifactId>slf4j-aws-lambda</artifactId>
53-
<version>1.0.1</version>
54-
</dependency>
55-
```
56-
57-
## Usage
58-
5944
There is a great original [manual][manual].
6045

6146
The configuration is similar to [SLF4J Simple][slf4j-simple].
@@ -69,6 +54,7 @@ It looks for the `lambda-logger.properties` resource and read properties:
6954
Must be one of (_trace_, _debug_, _info_, _warn_, _error_), a value is case-insensitive.
7055
If not specified, defaults to _info_.
7156
* **levelInBrackets** - Should the level string be output in brackets? Defaults to `false`.
57+
* **requestId** - Set the context name of AWS request ID. Defaults to `AWS_REQUEST_ID`.
7258
* **showDateTime** - Set to `true` if you want the current date and time to be included in output
7359
messages. Defaults to `false`.
7460
* **showLogName** - Set to `true` if you want the Logger instance name to be included in output
@@ -80,9 +66,13 @@ It looks for the `lambda-logger.properties` resource and read properties:
8066
* **showThreadName** - Set to `true` if you want to output the current thread name.
8167
Defaults to `false`.
8268

83-
The environment variables overrides the properties: **LOG_DATE_TIME_FORMAT**, **LOG_DEFAULT_LEVEL**,
84-
**LOG_LEVEL_IN_BRACKETS**, **LOG_SHOW_DATE_TIME**, **LOG_SHOW_NAME**, **LOG_SHOW_SHORT_NAME**,
85-
**LOG_SHOW_THREAD_ID**, **LOG_SHOW_THREAD_NAME**.
69+
The environment variables overrides the properties: **LOG_AWS_REQUEST_ID**,
70+
**LOG_DATE_TIME_FORMAT**, **LOG_DEFAULT_LEVEL**, **LOG_LEVEL_IN_BRACKETS**, **LOG_SHOW_DATE_TIME**,
71+
**LOG_SHOW_NAME**, **LOG_SHOW_SHORT_NAME**, **LOG_SHOW_THREAD_ID**, **LOG_SHOW_THREAD_NAME**.
72+
73+
More information you can find on site:
74+
75+
https://slf4j-aws-lambda.bot-by.uk/
8676

8777
## Contributing
8878

src/main/java/uk/bot_by/aws_lambda/slf4j/LambdaLogger.java

+1-11
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
* <pre><code class="language-java">
3333
* {@literal @}Override
3434
* public String handleRequest({@literal Map<String, Object>} input, Context context) {
35-
* MDC.put(LambdaLogger.AWS_REQUEST_ID, context.getAwsRequestId());
35+
* MDC.put("AWS_REQUEST_ID", context.getAwsRequestId());
3636
* ...
3737
* logger.info("info message");
3838
* ...
@@ -50,16 +50,6 @@
5050
*/
5151
public class LambdaLogger extends MarkerIgnoringBase {
5252

53-
/**
54-
* AWS request ID.
55-
* <p>
56-
* Use to put the request ID to MDC:
57-
* <pre><code class="language-java">
58-
* MDC.put(LambdaLogger.AWS_REQUEST_ID, context.getAwsRequestId());
59-
* </code></pre>
60-
*/
61-
public static final String AWS_REQUEST_ID = "AWS_REQUEST_ID";
62-
6353
private final LambdaLoggerConfiguration configuration;
6454
private final PrintStream printStream;
6555

src/main/java/uk/bot_by/aws_lambda/slf4j/LambdaLoggerConfiguration.java

+26-5
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939
* a value is case-insensitive. If not specified, defaults to <em>info</em>.</li>
4040
* <li><strong>levelInBrackets</strong> - Should the level string be output in brackets?
4141
* Defaults to {@code false}.</li>
42+
* <li><strong>requestId</strong> - Set the context name of <strong>AWS request ID</strong>.
43+
* Defaults to {@code AWS_REQUEST_ID}.</li>
4244
* <li><strong>showDateTime</strong> - Set to {@code true} if you want the current date and time
4345
* to be included in output messages. Defaults to {@code false}.</li>
4446
* <li><strong>showLogName</strong> - Set to {@code true} if you want the Logger instance name
@@ -51,11 +53,11 @@
5153
* the current thread name. Defaults to {@code false}.</li>
5254
* </ul>
5355
* <p>
54-
* The environment variables overrides the properties: <strong>LOG_DATE_TIME_FORMAT</strong>,
55-
* <strong>LOG_DEFAULT_LEVEL</strong>, <strong>LOG_LEVEL_IN_BRACKETS</strong>,
56-
* <strong>LOG_SHOW_DATE_TIME</strong>, <strong>LOG_SHOW_NAME</strong>,
57-
* <strong>LOG_SHOW_SHORT_NAME</strong>, <strong>LOG_SHOW_THREAD_ID</strong>,
58-
* <strong>LOG_SHOW_THREAD_NAME</strong>.
56+
* The environment variables overrides the properties: <strong>LOG_AWS_REQUEST_ID</strong>,
57+
* <strong>LOG_DATE_TIME_FORMAT</strong>, <strong>LOG_DEFAULT_LEVEL</strong>,
58+
* <strong>LOG_LEVEL_IN_BRACKETS</strong>, <strong>LOG_SHOW_DATE_TIME</strong>,
59+
* <strong>LOG_SHOW_NAME</strong>, <strong>LOG_SHOW_SHORT_NAME</strong>,
60+
* <strong>LOG_SHOW_THREAD_ID</strong>, <strong>LOG_SHOW_THREAD_NAME</strong>.
5961
*/
6062
public class LambdaLoggerConfiguration {
6163

@@ -66,6 +68,7 @@ public class LambdaLoggerConfiguration {
6668
private final Level loggerLevel;
6769
private final String logName;
6870
private final String name;
71+
private final String requestId;
6972
private final boolean showDateTime;
7073
private final boolean showThreadId;
7174
private final boolean showThreadName;
@@ -82,6 +85,7 @@ private LambdaLoggerConfiguration(Builder builder) {
8285
} else {
8386
logName = null;
8487
}
88+
requestId = builder.requestId;
8589
showDateTime = builder.showDateTime;
8690
showThreadId = builder.showThreadId;
8791
showThreadName = builder.showThreadName;
@@ -111,6 +115,10 @@ public String name() {
111115
return name;
112116
}
113117

118+
public String requestId() {
119+
return requestId;
120+
}
121+
114122
public boolean showDateTime() {
115123
return showDateTime;
116124
}
@@ -132,6 +140,7 @@ public static class Builder {
132140
private boolean levelInBrackets;
133141
private Level loggerLevel;
134142
private String name;
143+
private String requestId;
135144
private boolean showDateTime;
136145
private boolean showLogName;
137146
private boolean showShortLogName;
@@ -149,6 +158,7 @@ private Builder() {
149158
public LambdaLoggerConfiguration build() {
150159
requireNonNull(loggerLevel, "Logger level is null");
151160
requireNonNull(name, "Logger name is null");
161+
requireNonNull(requestId, "AWS request ID is null");
152162
return new LambdaLoggerConfiguration(this);
153163
}
154164

@@ -205,6 +215,17 @@ public Builder name(@NotNull String name) {
205215
return this;
206216
}
207217

218+
/**
219+
* Context name of AWS request ID.
220+
*
221+
* @param requestId context name of AWS request ID
222+
* @return a builder
223+
*/
224+
public Builder requestId(@NotNull String requestId) {
225+
this.requestId = requestId;
226+
return this;
227+
}
228+
208229
/**
209230
* Set to {@code true} if you want the current date and time to be included in output messages.
210231
* <p>

0 commit comments

Comments
 (0)