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

Commit 1409aa2

Browse files
committed
Update example, custom level and marker separators
Closes #8 and #9
2 parents 426d6a7 + dd0f84f commit 1409aa2

File tree

10 files changed

+94
-25
lines changed

10 files changed

+94
-25
lines changed

example/cloudwatch-screenshot.png

62.6 KB
Loading

example/pom.xml

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

5252
<properties>
5353
<!-- version -->
54-
<revision>1.1.0</revision>
54+
<revision>1.2.0</revision>
5555
<changelist>-SNAPSHOT</changelist>
5656
<sha1/>
5757
<!-- java and maven -->
@@ -74,7 +74,7 @@
7474
<lambda-core.version>1.2.1</lambda-core.version>
7575
<lambda-events.version>3.11.0</lambda-events.version>
7676
<slf4j.version>1.7.36</slf4j.version>
77-
<slf4j-aws-lambda.version>1.1.0</slf4j-aws-lambda.version>
77+
<slf4j-aws-lambda.version>2.1.0</slf4j-aws-lambda.version>
7878
<!-- other -->
7979
<aws-request-id>request#</aws-request-id>
8080
</properties>

example/readme.md

+8-10
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
The expected log lines
22

33
```log
4-
INFO uk.bot_by.bot.slf4j_demo.BotHandler - Load version: 1.1.0
5-
START RequestId: 1cfba6fe-5030-4b00-9d0b-5406b30cbf8c Version: $LATEST
6-
1cfba6fe-5030-4b00-9d0b-5406b30cbf8c INFO uk.bot_by.bot.slf4j_demo.BotHandler - info message
7-
1cfba6fe-5030-4b00-9d0b-5406b30cbf8c WARN uk.bot_by.bot.slf4j_demo.BotHandler - warning message
8-
1cfba6fe-5030-4b00-9d0b-5406b30cbf8c ERROR uk.bot_by.bot.slf4j_demo.BotHandler - error message
9-
1cfba6fe-5030-4b00-9d0b-5406b30cbf8c INFO uk.bot_by.bot.slf4j_demo.BotHandler - CRLF injection
10-
1cfba6fe-5030-4b00-9d0b-5406b30cbf8c INFO uk.bot_by.bot.slf4j_demo.BotHandler - CRLF injection
11-
1cfba6fe-5030-4b00-9d0b-5406b30cbf8c INFO uk.bot_by.bot.slf4j_demo.BotHandler - CRLF injection
12-
1cfba6fe-5030-4b00-9d0b-5406b30cbf8c WARN uk.bot_by.bot.slf4j_demo.BotHandler - printable stacktrace java.lang.Throwable: Printable Stacktrace Demo at ...
13-
END RequestId: 1cfba6fe-5030-4b00-9d0b-5406b30cbf8c
4+
START RequestId: d78368cd-de47-43d5-b690-02586951ec84 Version: $LATEST
5+
d78368cd-de47-43d5-b690-02586951ec84 WARN uk.bot_by.bot.slf4j_demo.BotHandler - warning message
6+
d78368cd-de47-43d5-b690-02586951ec84 ERROR uk.bot_by.bot.slf4j_demo.BotHandler - error message
7+
d78368cd-de47-43d5-b690-02586951ec84 TRACE uk.bot_by.bot.slf4j_demo.BotHandler - CRLF injection
8+
d78368cd-de47-43d5-b690-02586951ec84 TRACE uk.bot_by.bot.slf4j_demo.BotHandler - CRLF injection
9+
d78368cd-de47-43d5-b690-02586951ec84 TRACE uk.bot_by.bot.slf4j_demo.BotHandler - CRLF injection
10+
d78368cd-de47-43d5-b690-02586951ec84 WARN uk.bot_by.bot.slf4j_demo.BotHandler - printable stacktrace java.lang.Throwable: Printable Stacktrace Demo at uk.bot_by.bot.slf4j_demo.BotHandler.handleRequest(BotHandler.java:35) at uk.bot_by.bot.slf4j_demo.BotHandler.handleRequest(BotHandler.java:13) at lambdainternal.EventHandlerLoader$PojoHandlerAsStreamHandler.handleRequest(EventHandlerLoader.java:199) at lambdainternal.EventHandlerLoader$2.call(EventHandlerLoader.java:899) at lambdainternal.AWSLambda.startRuntime(AWSLambda.java:268) at lambdainternal.AWSLambda.startRuntime(AWSLambda.java:206) at lambdainternal.AWSLambda.main(AWSLambda.java:200)
11+
END RequestId: d78368cd-de47-43d5-b690-02586951ec84
1412
```
1513

1614
The screenshot of CloudWatch log

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

+6-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import org.slf4j.Logger;
88
import org.slf4j.LoggerFactory;
99
import org.slf4j.MDC;
10+
import org.slf4j.Marker;
11+
import org.slf4j.helpers.BasicMarkerFactory;
1012

1113
public class BotHandler implements RequestHandler<Map<String, Object>, String> {
1214

@@ -24,8 +26,11 @@ public String handleRequest(Map<String, Object> input, Context context) {
2426
logger.info("info message");
2527
logger.warn("warning message");
2628
logger.error("error message");
29+
30+
var marker = new BasicMarkerFactory().getMarker("important");
31+
2732
Stream.of("\n", "\r\n", "\r").forEach(injection -> {
28-
logger.info("CRLF{}injection", injection);
33+
logger.trace(marker, "CRLF{}injection", injection);
2934
});
3035
logger.warn("printable stacktrace", new Throwable("Printable Stacktrace Demo"));
3136
return "done";
Original file line numberDiff line numberDiff line change
@@ -1 +1,6 @@
11
requestId=${aws-request-id}
2+
log.uk.bot_by.bot=warn trace@important
3+
# multi-space
4+
logLevelSeparator=\\s+
5+
# single pipe symbol
6+
markerSeparator=\\|

pom.xml

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

6767
<properties>
6868
<!-- version -->
69-
<revision>2.0.0</revision>
69+
<revision>2.1.0</revision>
7070
<changelist>-SNAPSHOT</changelist>
7171
<sha1/>
7272
<!-- java and maven -->

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

+29-11
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@
7272
* <strong>LOG_LEVEL_IN_BRACKETS</strong>, <strong>LOG_SHOW_DATE_TIME</strong>,
7373
* <strong>LOG_SHOW_NAME</strong>, <strong>LOG_SHOW_SHORT_NAME</strong>,
7474
* <strong>LOG_SHOW_THREAD_ID</strong>, <strong>LOG_SHOW_THREAD_NAME</strong>.
75-
*
75+
* <p>
7676
* <strong>Fine-grained configuration with markers</strong>
7777
* <p>
7878
* The AWS Lambda Logger supports markers since <em>v2.0.0</em>.
@@ -85,12 +85,24 @@
8585
* The logger for {@code org.test.Class} has the common <em>warn</em> log level.
8686
* Also, it has additional levels <em>info</em> with the marker <em>iAmMarker</em>
8787
* and <em>trace</em> with markers <em>important</em> and <em>notify-admin</em>.
88+
* <p>
89+
* You can customize level and marker separators with properties <strong>logLevelSeparator</strong>
90+
* and <strong>markerSeparator</strong>. Remember that separators are not a single characters but
91+
* regular expressions. The environment variables are <strong>LOG_LEVEL_SEPARATOR</strong> and
92+
* <strong>LOG_MARKER_SEPARATOR</strong> accordingly.
93+
* <p>
94+
* Example:
95+
* <pre><code class="language-properties">
96+
* log.org.test.Class=warn info@iAmMarker trace@important|notify-admin
97+
* # multi-space
98+
* logLevelSeparator=\\s+
99+
* # single pipe symbol
100+
* markerSeparator=\\|
101+
* </code></pre>
88102
*/
89103
public class LambdaLoggerFactory implements ILoggerFactory {
90104

91105
private static final String AT = "@";
92-
private static final String COLON = ":";
93-
private static final String COMMA = ",";
94106
private static final String CONFIGURATION_FILE = "lambda-logger.properties";
95107
private static final char DOT = '.';
96108
private static final String DOTS = "\\.+";
@@ -102,6 +114,8 @@ public class LambdaLoggerFactory implements ILoggerFactory {
102114
private final DateFormat dateTimeFormat;
103115
private final List<LoggerLevel> defaultLoggerLevel;
104116
private final boolean levelInBrackets;
117+
private final String logLevelSeparator;
118+
private final String markerSeparator;
105119
private final Properties properties;
106120
private final String requestId;
107121
private final boolean showDateTime;
@@ -119,6 +133,9 @@ public LambdaLoggerFactory() {
119133
loggers = new ConcurrentHashMap<>();
120134
properties = loadProperties(configurationFile);
121135
dateTimeFormat = getDateTimeFormat(ConfigurationProperty.DateTimeFormat);
136+
// logLevelSeparator and markerSeparator should be resolved before defaultLoggerLevel
137+
logLevelSeparator = getStringProperty(ConfigurationProperty.LogLevelSeparator);
138+
markerSeparator = getStringProperty(ConfigurationProperty.MarkerSeparator);
122139
defaultLoggerLevel = getLoggerLevelProperty(ConfigurationProperty.DefaultLogLevel);
123140
levelInBrackets = getBooleanProperty(ConfigurationProperty.LevelInBrackets);
124141
requestId = getStringProperty(ConfigurationProperty.RequestId);
@@ -272,13 +289,13 @@ private List<LoggerLevel> parseLoggerLevelString(String loggerLevelString)
272289
throws IllegalArgumentException {
273290
var loggerLevels = new ArrayList<LoggerLevel>();
274291

275-
for (String loggerLevel : loggerLevelString.split(COMMA)) {
292+
for (String loggerLevel : loggerLevelString.split(logLevelSeparator)) {
276293
var loggerLevelBuilder = LoggerLevel.builder();
277294
var loggerLevelWithMarkers = loggerLevel.split(AT);
278295

279296
loggerLevelBuilder.level(Level.valueOf(loggerLevelWithMarkers[0].toUpperCase()));
280297
if (loggerLevelWithMarkers.length > 1) {
281-
for (String markerName : loggerLevelWithMarkers[1].split(COLON)) {
298+
for (String markerName : loggerLevelWithMarkers[1].split(markerSeparator)) {
282299
loggerLevelBuilder.marker(markerName);
283300
}
284301
}
@@ -292,12 +309,13 @@ public enum ConfigurationProperty {
292309

293310
DateTimeFormat("dateTimeFormat", "LOG_DATE_TIME_FORMAT", null), DefaultLogLevel(
294311
"defaultLogLevel", "LOG_DEFAULT_LEVEL", "INFO"), LevelInBrackets("levelInBrackets",
295-
"LOG_LEVEL_IN_BRACKETS", "false"), LogLevel("log.", "LOG_", null), RequestId("requestId",
296-
"LOG_AWS_REQUEST_ID", "AWS_REQUEST_ID"), ShowDateTime("showDateTime", "LOG_SHOW_DATE_TIME",
297-
"false"), ShowLogName("showLogName", "LOG_SHOW_NAME", "true"), ShowShortLogName(
298-
"showShortLogName", "LOG_SHOW_SHORT_NAME", "false"), ShowThreadId("showThreadId",
299-
"LOG_SHOW_THREAD_ID", "false"), ShowThreadName("showThreadName", "LOG_SHOW_THREAD_NAME",
300-
"false");
312+
"LOG_LEVEL_IN_BRACKETS", "false"), LogLevel("log.", "LOG_", null), LogLevelSeparator(
313+
"logLevelSeparator", "LOG_LEVEL_SEPARATOR", ","), MarkerSeparator("markerSeparator",
314+
"LOG_MARKER_SEPARATOR", ":"), RequestId("requestId", "LOG_AWS_REQUEST_ID",
315+
"AWS_REQUEST_ID"), ShowDateTime("showDateTime", "LOG_SHOW_DATE_TIME", "false"), ShowLogName(
316+
"showLogName", "LOG_SHOW_NAME", "true"), ShowShortLogName("showShortLogName",
317+
"LOG_SHOW_SHORT_NAME", "false"), ShowThreadId("showThreadId", "LOG_SHOW_THREAD_ID",
318+
"false"), ShowThreadName("showThreadName", "LOG_SHOW_THREAD_NAME", "false");
301319

302320
public final String defaultValue;
303321
public final String propertyName;

src/site/markdown/usage.md

+14
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,20 @@ The logger for `org.test.Class` has the common _warn_ log level.
4545
Also, it has additional levels _info_ with the marker _iAmMarker_
4646
and _trace_ with markers _important_ and _notify-admin_.
4747

48+
You can customize level and marker separators with properties **logLevelSeparator** and
49+
**markerSeparator**. Remember that separators are not a single characters but regular expressions.
50+
The environment variables are **LOG_LEVEL_SEPARATOR** and **LOG_MARKER_SEPARATOR** accordingly.
51+
52+
Example:
53+
54+
```properties
55+
log.org.test.Class=warn info@iAmMarker trace@important|notify-admin
56+
# multi-space
57+
logLevelSeparator=\\s+
58+
# single pipe symbol
59+
markerSeparator=\\|
60+
```
61+
4862

4963
[manual]: https://www.slf4j.org/manual.html
5064

src/test/java/uk/bot_by/aws_lambda/slf4j/PropertiesTest.java

+24
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,30 @@ void defaultLogLevelMulti() {
260260
() -> assertTrue(logger.isTraceEnabled(markerC), "trace with the marker C"));
261261
}
262262

263+
@DisplayName("Custom level and marker separators")
264+
@Test
265+
void customLevelAndMarkerSeparators() {
266+
// given
267+
var loggerFactory = spy(new LambdaLoggerFactory("custom-separators.properties"));
268+
269+
doReturn(printStream).when(loggerFactory).getPrintStream();
270+
271+
// when
272+
var logger = loggerFactory.getLogger("org.test.Class");
273+
274+
// then
275+
logger.isTraceEnabled();
276+
assertAll("Check custom level and marker separators",
277+
() -> assertFalse(logger.isTraceEnabled(), "trace"),
278+
() -> assertFalse(logger.isDebugEnabled(), "debug"),
279+
() -> assertFalse(logger.isInfoEnabled(), "info"),
280+
() -> assertTrue(logger.isWarnEnabled(), "warn"),
281+
() -> assertTrue(logger.isErrorEnabled(), "error"),
282+
() -> assertTrue(logger.isInfoEnabled(markerA), "info with a marker"),
283+
() -> assertTrue(logger.isTraceEnabled(markerB), "trace with the marker B"),
284+
() -> assertTrue(logger.isTraceEnabled(markerC), "trace with the marker C"));
285+
}
286+
263287
@DisplayName("Read logger properties from the file, get logger then print out debug message")
264288
@Test
265289
void useLoggerProperties() {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
defaultLogLevel=warn info@iAmMarker trace@important|notify-admin
2+
# multi-space
3+
logLevelSeparator=\\s+
4+
# single pipe symbol
5+
markerSeparator=\\|

0 commit comments

Comments
 (0)