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

Commit 578e36e

Browse files
committed
Merge branch '7-update-example-implementation-of-none-level' into 'main'
Resolve "Multi-level and multi-marker configuration" Closes #7 See merge request bot-by/slf4j-aws-lambda!7
2 parents a46d6e0 + 0b1b87b commit 578e36e

34 files changed

+359
-177
lines changed

changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
88
### Added
99
- Support Markers
1010
- Update dependencies
11+
- Update javadocs and site
1112

1213
## 1.1.0 - 2022-06-29
1314
### Added

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666

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

readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ It looks for the `lambda-logger.properties` resource and read properties:
5555
Must be one of (_trace_, _debug_, _info_, _warn_, _error_), a value is case-insensitive.
5656
If not specified, defaults to _info_.
5757
* **levelInBrackets** - Should the level string be output in brackets? Defaults to `false`.
58+
* **log.a.b.c** - Logging detail level for a LambdaLogger instance named _a.b.c_.
5859
* **requestId** - Set the context name of AWS request ID. Defaults to `AWS_REQUEST_ID`.
5960
* **showDateTime** - Set to `true` if you want the current date and time to be included in output
6061
messages. Defaults to `false`.

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

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

18-
import static java.util.Arrays.copyOfRange;
1918
import static java.util.Objects.isNull;
2019
import static java.util.Objects.nonNull;
2120

@@ -38,19 +37,66 @@
3837

3938
/**
4039
* Responsible for building {@link Logger} using the {@link LambdaLogger} implementation.
40+
* <p>
41+
* The configuration is similar to <a
42+
* href="https://www.slf4j.org/api/org/slf4j/simple/SimpleLogger.html">SLF4J Simple</a>.
43+
* <p>
44+
* It looks for the {@code lambda-logger.properties} resource and read properties:
45+
* <ul>
46+
* <li><strong>dateTimeFormat</strong> - The date and time format to be used in the output messages.
47+
* The pattern describing the date and time format is defined by {@link java.text.SimpleDateFormat}.
48+
* If the format is not specified or is invalid, the number of milliseconds since start up
49+
* will be output.</li>
50+
* <li><strong>defaultLogLevel</strong> - Default log level for all instances of LambdaLogger.
51+
* Must be one of (<em>trace</em>, <em>debug</em>, <em>info</em>, <em>warn</em>, <em>error</em>),
52+
* a value is case-insensitive. If not specified, defaults to <em>info</em>.</li>
53+
* <li><strong>levelInBrackets</strong> - Should the level string be output in brackets?
54+
* Defaults to {@code false}.</li>
55+
* <li><strong>log.a.b.c</strong> - Logging detail level for a LambdaLogger instance named <em>a.b.c</em></li>
56+
* <li><strong>requestId</strong> - Set the context name of <strong>AWS request ID</strong>.
57+
* Defaults to {@code AWS_REQUEST_ID}.</li>
58+
* <li><strong>showDateTime</strong> - Set to {@code true} if you want the current date and time
59+
* to be included in output messages. Defaults to {@code false}.</li>
60+
* <li><strong>showLogName</strong> - Set to {@code true} if you want the Logger instance name
61+
* to be included in output messages. Defaults to {@code true}.</li>
62+
* <li><strong>showShortLogName</strong> - Set to {@code true} if you want the last component of the name
63+
* to be included in output messages. Defaults to {@code false}.</li>
64+
* <li><strong>showThreadId</strong> - If you would like to output the current thread id,
65+
* then set to {@code true}. Defaults to {@code false}.</li>
66+
* <li><strong>showThreadName</strong> - Set to {@code true} if you want to output
67+
* the current thread name. Defaults to {@code false}.</li>
68+
* </ul>
69+
* <p>
70+
* The environment variables overrides the properties: <strong>LOG_AWS_REQUEST_ID</strong>,
71+
* <strong>LOG_DATE_TIME_FORMAT</strong>, <strong>LOG_DEFAULT_LEVEL</strong>,
72+
* <strong>LOG_LEVEL_IN_BRACKETS</strong>, <strong>LOG_SHOW_DATE_TIME</strong>,
73+
* <strong>LOG_SHOW_NAME</strong>, <strong>LOG_SHOW_SHORT_NAME</strong>,
74+
* <strong>LOG_SHOW_THREAD_ID</strong>, <strong>LOG_SHOW_THREAD_NAME</strong>.
4175
*
42-
* @see LoggerConfiguration LambdaLogger's configuration
76+
* <h4>Fine-grained configuration with markers</h4>
77+
* <p>
78+
* The AWS Lambda Logger supports markers since <em>v2.0.0</em>.
79+
* The log level (default or detail) can have some log level and each level can have some markers.
80+
* <p>
81+
* Example:
82+
* <pre><code class="language-properties">
83+
* log.org.test.Class=warn,info@iAmMarker,trace@important:notify-admin
84+
* </pre>
85+
* The logger for {@code org.test.Class} has the common <em>warn</em> log level.
86+
* Also, it has additional levels <em>info</em> with the marker <em>iAmMarker</em>
87+
* and <em>trace</em> with markers <em>important</em> and <em>notify-admin</em>.
4388
*/
4489
public class LambdaLoggerFactory implements ILoggerFactory {
4590

91+
private static final String AT = "@";
92+
private static final String COLON = ":";
93+
private static final String COMMA = ",";
4694
private static final String CONFIGURATION_FILE = "lambda-logger.properties";
4795
private static final char DOT = '.';
4896
private static final String DOTS = "\\.+";
4997
private static final String NONE = "";
5098
private static final String UNDERSCORE = "_";
5199
private static final String SPACES = "\\s+";
52-
private static final String COMMA = ",";
53-
private static final String AT = "@";
54100

55101
private final ConcurrentMap<String, Logger> loggers;
56102
private final DateFormat dateTimeFormat;
@@ -65,8 +111,13 @@ public class LambdaLoggerFactory implements ILoggerFactory {
65111
private final boolean showThreadName;
66112

67113
public LambdaLoggerFactory() {
114+
this(CONFIGURATION_FILE);
115+
}
116+
117+
@VisibleForTesting
118+
LambdaLoggerFactory(String configurationFile) {
68119
loggers = new ConcurrentHashMap<>();
69-
properties = loadProperties();
120+
properties = loadProperties(configurationFile);
70121
dateTimeFormat = getDateTimeFormat(ConfigurationProperty.DateTimeFormat);
71122
defaultLoggerLevel = getLoggerLevelProperty(ConfigurationProperty.DefaultLogLevel);
72123
levelInBrackets = getBooleanProperty(ConfigurationProperty.LevelInBrackets);
@@ -204,11 +255,11 @@ private Properties getProperties() {
204255
return properties;
205256
}
206257

207-
private Properties loadProperties() {
258+
private Properties loadProperties(String configurationFile) {
208259
var properties = new Properties();
209260

210261
try (InputStream configurationInputStream = Thread.currentThread().getContextClassLoader()
211-
.getResourceAsStream(CONFIGURATION_FILE)) {
262+
.getResourceAsStream(configurationFile)) {
212263
properties.load(configurationInputStream);
213264
} catch (IOException e) {
214265
// ignored
@@ -227,8 +278,7 @@ private List<LoggerLevel> parseLoggerLevelString(String loggerLevelString)
227278

228279
loggerLevelBuilder.level(Level.valueOf(loggerLevelWithMarkers[0].toUpperCase()));
229280
if (loggerLevelWithMarkers.length > 1) {
230-
for (String markerName : copyOfRange(loggerLevelWithMarkers, 1,
231-
loggerLevelWithMarkers.length)) {
281+
for (String markerName : loggerLevelWithMarkers[1].split(COLON)) {
232282
loggerLevelBuilder.marker(markerName);
233283
}
234284
}

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

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

32-
/**
33-
* {@link LambdaLogger}'s configuration.
34-
* <p>
35-
* The configuration is similar to <a
36-
* href="https://www.slf4j.org/api/org/slf4j/simple/SimpleLogger.html">SLF4J Simple</a>.
37-
* <p>
38-
* It looks for the {@code lambda-logger.properties} resource and read properties:
39-
* <ul>
40-
* <li><strong>dateTimeFormat</strong> - The date and time format to be used in the output messages.
41-
* The pattern describing the date and time format is defined by {@link java.text.SimpleDateFormat}.
42-
* If the format is not specified or is invalid, the number of milliseconds since start up
43-
* will be output.</li>
44-
* <li><strong>defaultLogLevel</strong> - Default log level for all instances of LambdaLogger.
45-
* Must be one of (<em>trace</em>, <em>debug</em>, <em>info</em>, <em>warn</em>, <em>error</em>),
46-
* a value is case-insensitive. If not specified, defaults to <em>info</em>.</li>
47-
* <li><strong>levelInBrackets</strong> - Should the level string be output in brackets?
48-
* Defaults to {@code false}.</li>
49-
* <li><strong>requestId</strong> - Set the context name of <strong>AWS request ID</strong>.
50-
* Defaults to {@code AWS_REQUEST_ID}.</li>
51-
* <li><strong>showDateTime</strong> - Set to {@code true} if you want the current date and time
52-
* to be included in output messages. Defaults to {@code false}.</li>
53-
* <li><strong>showLogName</strong> - Set to {@code true} if you want the Logger instance name
54-
* to be included in output messages. Defaults to {@code true}.</li>
55-
* <li><strong>showShortLogName</strong> - Set to {@code true} if you want the last component of the name
56-
* to be included in output messages. Defaults to {@code false}.</li>
57-
* <li><strong>showThreadId</strong> - If you would like to output the current thread id,
58-
* then set to {@code true}. Defaults to {@code false}.</li>
59-
* <li><strong>showThreadName</strong> - Set to {@code true} if you want to output
60-
* the current thread name. Defaults to {@code false}.</li>
61-
* </ul>
62-
* <p>
63-
* The environment variables overrides the properties: <strong>LOG_AWS_REQUEST_ID</strong>,
64-
* <strong>LOG_DATE_TIME_FORMAT</strong>, <strong>LOG_DEFAULT_LEVEL</strong>,
65-
* <strong>LOG_LEVEL_IN_BRACKETS</strong>, <strong>LOG_SHOW_DATE_TIME</strong>,
66-
* <strong>LOG_SHOW_NAME</strong>, <strong>LOG_SHOW_SHORT_NAME</strong>,
67-
* <strong>LOG_SHOW_THREAD_ID</strong>, <strong>LOG_SHOW_THREAD_NAME</strong>.
68-
*/
6932
class LoggerConfiguration {
7033

7134
private static final String DOT = ".";
@@ -143,9 +106,6 @@ boolean showThreadName() {
143106
return showThreadName;
144107
}
145108

146-
/**
147-
* {@link LambdaLogger} configuration's builder.
148-
*/
149109
static class Builder {
150110

151111
private DateFormat dateTimeFormat;
@@ -162,58 +122,23 @@ static class Builder {
162122
private Builder() {
163123
}
164124

165-
/**
166-
* Build a LambdaLoggerConfiguration instance.
167-
*
168-
* @return a configuration instance
169-
*/
170125
LoggerConfiguration build() {
171126
requireNonNull(loggerPredicates, "Logger level is null");
172127
requireNonNull(name, "Logger name is null");
173128
requireNonNull(requestId, "AWS request ID is null");
174129
return new LoggerConfiguration(this);
175130
}
176131

177-
/**
178-
* The date and time format to be used in the output messages.
179-
* <p>
180-
* The pattern describing the date and time format is defined by
181-
* {@link java.text.SimpleDateFormat}. If the format is not specified or is invalid, the number
182-
* of milliseconds since start up will be output.
183-
*
184-
* @param dateTimeFormat date and time format
185-
* @return a builder
186-
*/
187132
Builder dateTimeFormat(@Nullable DateFormat dateTimeFormat) {
188133
this.dateTimeFormat = dateTimeFormat;
189134
return this;
190135
}
191136

192-
/**
193-
* Should the level string be output in brackets?
194-
* <p>
195-
* Defaults to {@code false}.
196-
*
197-
* @param levelInBrackets use brackets
198-
* @return a builder
199-
*/
200137
Builder levelInBrackets(boolean levelInBrackets) {
201138
this.levelInBrackets = levelInBrackets;
202139
return this;
203140
}
204141

205-
/**
206-
* The log level of the Logger instance.
207-
* <p>
208-
* Must be one of (<em>trace</em>, <em>debug</em>, <em>info</em>, <em>warn</em>,
209-
* <em>error</em>), a value is case-insensitive. If not specified, defaults to <em>info</em>.
210-
* <p>
211-
* You can add some different log levels: it does not overwrite previous levels but add new one
212-
* to a list. There has to be at least one log level.
213-
*
214-
* @param loggerLevel log level
215-
* @return a builder
216-
*/
217142
Builder loggerLevel(@NotNull Level loggerLevel) {
218143
if (isNull(loggerPredicates)) {
219144
loggerPredicates = new ArrayList<>();
@@ -222,19 +147,6 @@ Builder loggerLevel(@NotNull Level loggerLevel) {
222147
return this;
223148
}
224149

225-
/**
226-
* The marked log level of the Logger instance.
227-
* <p>
228-
* Must be one of (<em>trace</em>, <em>debug</em>, <em>info</em>, <em>warn</em>,
229-
* <em>error</em>), a value is case-insensitive. If not specified, defaults to <em>info</em>.
230-
* <p>
231-
* You can add some different log levels: it does not overwrite previous levels but add new one
232-
* to a list. There has to be at least one log level.
233-
*
234-
* @param loggerLevel default log level
235-
* @param loggerMarkers markers
236-
* @return a builder
237-
*/
238150
Builder loggerLevel(@NotNull Level loggerLevel, @NotNull Marker... loggerMarkers) {
239151
if (isNull(loggerPredicates)) {
240152
loggerPredicates = new ArrayList<>();
@@ -253,89 +165,36 @@ Builder loggerLevel(@NotNull Level loggerLevel, @NotNull Marker... loggerMarkers
253165
return this;
254166
}
255167

256-
/**
257-
* Name of the Logger instance.
258-
*
259-
* @param name logger name
260-
* @return a builder
261-
*/
262168
Builder name(@NotNull String name) {
263169
this.name = name;
264170
return this;
265171
}
266172

267-
/**
268-
* Context name of AWS request ID.
269-
*
270-
* @param requestId context name of AWS request ID
271-
* @return a builder
272-
*/
273173
Builder requestId(@NotNull String requestId) {
274174
this.requestId = requestId;
275175
return this;
276176
}
277177

278-
/**
279-
* Set to {@code true} if you want the current date and time to be included in output messages.
280-
* <p>
281-
* Defaults to {@code false}.
282-
*
283-
* @param showDateTime show date and time
284-
* @return a builder
285-
*/
286178
Builder showDateTime(boolean showDateTime) {
287179
this.showDateTime = showDateTime;
288180
return this;
289181
}
290182

291-
/**
292-
* Set to {@code true} if you want the Logger instance name to be included in output messages.
293-
* <p>
294-
* Defaults to {@code true}.
295-
*
296-
* @param showLogName show log name
297-
* @return a builder
298-
*/
299183
Builder showLogName(boolean showLogName) {
300184
this.showLogName = showLogName;
301185
return this;
302186
}
303187

304-
/**
305-
* Set to {@code true} if you want the last component of the name to be included in output
306-
* messages.
307-
* <p>
308-
* Defaults to {@code false}.
309-
*
310-
* @param showShortLogName show short log name
311-
* @return a builder
312-
*/
313188
Builder showShortLogName(boolean showShortLogName) {
314189
this.showShortLogName = showShortLogName;
315190
return this;
316191
}
317192

318-
/**
319-
* If you would like to output the current thread id, then set to {@code true}.
320-
* <p>
321-
* Defaults to {@code false}.
322-
*
323-
* @param showThreadId show the current thread ID
324-
* @return a builder
325-
*/
326193
Builder showThreadId(boolean showThreadId) {
327194
this.showThreadId = showThreadId;
328195
return this;
329196
}
330197

331-
/**
332-
* Set to {@code true} if you want to output the current thread name.
333-
* <p>
334-
* Defaults to {@code false}.
335-
*
336-
* @param showThreadName show the current thread name
337-
* @return a builder
338-
*/
339198
Builder showThreadName(boolean showThreadName) {
340199
this.showThreadName = showThreadName;
341200
return this;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import org.slf4j.event.Level;
2424
import org.slf4j.helpers.BasicMarkerFactory;
2525

26-
public class LoggerLevel {
26+
class LoggerLevel {
2727

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

0 commit comments

Comments
 (0)