Skip to content

Commit 9378684

Browse files
committed
fix checkstyle
1 parent 15d7162 commit 9378684

File tree

11 files changed

+101
-33
lines changed

11 files changed

+101
-33
lines changed

powertools-logging/powertools-logging-log4j/src/main/java/org/apache/logging/log4j/layout/template/json/resolver/PowertoolsResolverFactory.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
import org.apache.logging.log4j.core.config.plugins.Plugin;
1919
import org.apache.logging.log4j.core.config.plugins.PluginFactory;
2020

21+
/**
22+
* Factory for {@link PowertoolsResolver}. Log4j plugin to process powertools fields in the layout.json
23+
*/
2124
@Plugin(name = "PowertoolsResolverFactory", category = TemplateResolverFactory.CATEGORY)
2225
public final class PowertoolsResolverFactory implements EventResolverFactory {
2326

powertools-logging/powertools-logging-log4j/src/main/java/software/amazon/lambda/powertools/logging/internal/Log4jLoggingManager.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,14 @@
2020
import org.apache.logging.log4j.core.config.Configurator;
2121
import org.slf4j.Logger;
2222

23+
/**
24+
* LoggingManager for Log4j2 (see {@link LoggingManager}).
25+
*/
2326
public class Log4jLoggingManager implements LoggingManager {
2427

28+
/**
29+
* @inheritDoc
30+
*/
2531
@Override
2632
@SuppressWarnings("java:S4792")
2733
public void resetLogLevel(org.slf4j.event.Level logLevel) {
@@ -30,6 +36,9 @@ public void resetLogLevel(org.slf4j.event.Level logLevel) {
3036
ctx.updateLoggers();
3137
}
3238

39+
/**
40+
* @inheritDoc
41+
*/
3342
@Override
3443
public org.slf4j.event.Level getLogLevel(Logger logger) {
3544
LoggerContext ctx = (LoggerContext) LogManager.getContext(false);

powertools-logging/powertools-logging-logback/src/main/java/software/amazon/lambda/powertools/logging/LambdaEcsEncoder.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,15 @@ public byte[] headerBytes() {
5959
return null;
6060
}
6161

62+
/**
63+
* Main method of the encoder. Encode a logging event into Json format (with Elastic Search fields)
64+
*
65+
* @param event the logging event
66+
* @return the encoded bytes
67+
*/
6268
@Override
6369
public byte[] encode(ILoggingEvent event) {
64-
Map<String, String> mdcPropertyMap = event.getMDCPropertyMap();
70+
final Map<String, String> mdcPropertyMap = event.getMDCPropertyMap();
6571

6672
StringBuilder builder = new StringBuilder(256);
6773
LambdaEcsSerializer.serializeObjectStart(builder);
@@ -118,7 +124,8 @@ public byte[] footerBytes() {
118124
}
119125

120126
/**
121-
* Specify a throwable converter to format the stacktrace according to your need (default is <b>null</b>, no throwableConverter):
127+
* Specify a throwable converter to format the stacktrace according to your need
128+
* (default is <b>null</b>, no throwableConverter):
122129
* <br/>
123130
* <pre>{@code
124131
* <encoder class="software.amazon.lambda.powertools.logging.LambdaEcsEncoder">
@@ -134,7 +141,8 @@ public byte[] footerBytes() {
134141
* </throwableConverter>
135142
* </encoder>
136143
* }</pre>
137-
* @param throwableConverter
144+
*
145+
* @param throwableConverter converter for the throwable
138146
*/
139147
public void setThrowableConverter(ThrowableHandlingConverter throwableConverter) {
140148
this.throwableConverter = throwableConverter;
@@ -156,6 +164,7 @@ public void setThrowableConverter(ThrowableHandlingConverter throwableConverter)
156164
* <includeCloudInfo>false</includeCloudInfo>
157165
* </encoder>
158166
* }</pre>
167+
*
159168
* @param includeCloudInfo if thread information should be logged
160169
*/
161170
public void setIncludeCloudInfo(boolean includeCloudInfo) {
@@ -181,6 +190,7 @@ public void setIncludeCloudInfo(boolean includeCloudInfo) {
181190
* <includeFaasInfo>false</includeFaasInfo>
182191
* </encoder>
183192
* }</pre>
193+
*
184194
* @param includeFaasInfo if function information should be logged
185195
*/
186196
public void setIncludeFaasInfo(boolean includeFaasInfo) {

powertools-logging/powertools-logging-logback/src/main/java/software/amazon/lambda/powertools/logging/LambdaJsonEncoder.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@
2626

2727
/**
2828
* Custom encoder for logback that encodes logs in JSON format.
29-
* It does not use a JSON library but a custom serializer ({@link LambdaJsonSerializer}) to reduce the weight of the library.
29+
* It does not use a JSON library but a custom serializer ({@link LambdaJsonSerializer})
30+
* to reduce the weight of the library.
3031
*/
3132
public class LambdaJsonEncoder extends EncoderBase<ILoggingEvent> {
3233

@@ -94,6 +95,7 @@ public byte[] footerBytes() {
9495
* <timestampFormat>yyyy-MM-dd'T'HH:mm:ss.SSSZz</timestampFormat>
9596
* </encoder>
9697
* }</pre>
98+
*
9799
* @param timestampFormat format of the timestamp (compatible with {@link java.text.SimpleDateFormat})
98100
*/
99101
public void setTimestampFormat(String timestampFormat) {
@@ -108,14 +110,16 @@ public void setTimestampFormat(String timestampFormat) {
108110
* <timestampFormatTimezoneId>Europe/Paris</timestampFormatTimezoneId>
109111
* </encoder>
110112
* }</pre>
113+
*
111114
* @param timestampFormatTimezoneId Zone Id (see {@link java.util.TimeZone})
112115
*/
113116
public void setTimestampFormatTimezoneId(String timestampFormatTimezoneId) {
114117
this.timestampFormatTimezoneId = timestampFormatTimezoneId;
115118
}
116119

117120
/**
118-
* Specify a throwable converter to format the stacktrace according to your need (default is <b>null</b>, no throwableConverter):
121+
* Specify a throwable converter to format the stacktrace according to your need
122+
* (default is <b>null</b>, no throwableConverter):
119123
* <br/>
120124
* <pre>{@code
121125
* <encoder class="software.amazon.lambda.powertools.logging.LambdaJsonEncoder">
@@ -131,7 +135,8 @@ public void setTimestampFormatTimezoneId(String timestampFormatTimezoneId) {
131135
* </throwableConverter>
132136
* </encoder>
133137
* }</pre>
134-
* @param throwableConverter
138+
*
139+
* @param throwableConverter converter for the throwable
135140
*/
136141
public void setThrowableConverter(ThrowableHandlingConverter throwableConverter) {
137142
this.throwableConverter = throwableConverter;
@@ -145,6 +150,7 @@ public void setThrowableConverter(ThrowableHandlingConverter throwableConverter)
145150
* <includeThreadInfo>true</includeThreadInfo>
146151
* </encoder>
147152
* }</pre>
153+
*
148154
* @param includeThreadInfo if thread information should be logged
149155
*/
150156
public void setIncludeThreadInfo(boolean includeThreadInfo) {
@@ -172,6 +178,7 @@ public void setIncludeThreadInfo(boolean includeThreadInfo) {
172178
* <includePowertoolsInfo>false</includePowertoolsInfo>
173179
* </encoder>
174180
* }</pre>
181+
*
175182
* @param includePowertoolsInfo if function information should be logged
176183
*/
177184
public void setIncludePowertoolsInfo(boolean includePowertoolsInfo) {

powertools-logging/powertools-logging-logback/src/main/java/software/amazon/lambda/powertools/logging/internal/JsonUtils.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,15 @@
1414

1515
package software.amazon.lambda.powertools.logging.internal;
1616

17+
/**
18+
* Json tools to serialize attributes manually, to avoid using further dependencies (jackson, gson...)
19+
*/
1720
public class JsonUtils {
1821

22+
private JsonUtils() {
23+
// static utils
24+
}
25+
1926
protected static void serializeAttribute(StringBuilder builder, String attr, String value, boolean notBegin) {
2027
if (value != null) {
2128
if (notBegin) {
@@ -33,6 +40,10 @@ protected static void serializeAttribute(StringBuilder builder, String attr, Str
3340
}
3441
}
3542

43+
protected static void serializeAttribute(StringBuilder builder, String attr, String value) {
44+
serializeAttribute(builder, attr, value, true);
45+
}
46+
3647
protected static void serializeAttributeAsString(StringBuilder builder, String attr, String value,
3748
boolean notBegin) {
3849
if (value != null) {
@@ -47,16 +58,13 @@ protected static void serializeAttributeAsString(StringBuilder builder, String a
4758
}
4859
}
4960

50-
protected static void serializeAttribute(StringBuilder builder, String attr, String value) {
51-
serializeAttribute(builder, attr, value, true);
52-
}
53-
5461
protected static void serializeAttributeAsString(StringBuilder builder, String attr, String value) {
5562
serializeAttributeAsString(builder, attr, value, true);
5663
}
5764

5865
/**
59-
* As MDC is a Map<String, String>, we need to check the type to output numbers and booleans correctly (without quotes)
66+
* As MDC is a {@code Map<String, String>}, we need to check the type
67+
* to output numbers and booleans correctly (without quotes)
6068
*/
6169
private static boolean isString(String str) {
6270
if (str == null) {

powertools-logging/powertools-logging-logback/src/main/java/software/amazon/lambda/powertools/logging/internal/LambdaJsonSerializer.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,12 @@ public static void serializeThreadPriority(StringBuilder builder, String threadP
107107
serializeAttribute(builder, THREAD_PRIORITY_ATTR_NAME, threadPriority);
108108
}
109109

110-
public static void serializePowertools(StringBuilder builder, Map<String, String> mdc, boolean includePowertoolsInfo) {
110+
public static void serializePowertools(StringBuilder builder, Map<String, String> mdc,
111+
boolean includePowertoolsInfo) {
111112
TreeMap<String, String> sortedMap = new TreeMap<>(mdc);
112113
sortedMap.forEach((k, v) -> {
113-
if ((PowertoolsLoggedFields.stringValues().contains(k) && includePowertoolsInfo) || !PowertoolsLoggedFields.stringValues().contains(k)) {
114+
if ((PowertoolsLoggedFields.stringValues().contains(k) && includePowertoolsInfo)
115+
|| !PowertoolsLoggedFields.stringValues().contains(k)) {
114116
serializeAttribute(builder, k, v);
115117
}
116118
});

powertools-logging/powertools-logging-logback/src/main/java/software/amazon/lambda/powertools/logging/internal/LogbackLoggingManager.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
import org.slf4j.ILoggerFactory;
2222
import org.slf4j.LoggerFactory;
2323

24+
/**
25+
* LoggingManager for Logback (see {@link LoggingManager}).
26+
*/
2427
public class LogbackLoggingManager implements LoggingManager {
2528

2629
private final LoggerContext loggerContext;
@@ -33,6 +36,9 @@ public LogbackLoggingManager() {
3336
loggerContext = (LoggerContext) loggerFactory;
3437
}
3538

39+
/**
40+
* @inheritDoc
41+
*/
3642
@Override
3743
@SuppressWarnings("java:S4792")
3844
public void resetLogLevel(org.slf4j.event.Level logLevel) {
@@ -42,6 +48,9 @@ public void resetLogLevel(org.slf4j.event.Level logLevel) {
4248
}
4349
}
4450

51+
/**
52+
* @inheritDoc
53+
*/
4554
@Override
4655
public org.slf4j.event.Level getLogLevel(org.slf4j.Logger logger) {
4756
return org.slf4j.event.Level.valueOf(loggerContext.getLogger(logger.getName()).getEffectiveLevel().toString());

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@
3737
* <li>function_name</li>
3838
* <li>function_version</li>
3939
* <li>function_arn</li>
40-
* <li>MemoryLimitInMB</li>
40+
* <li>function_memory_size</li>
41+
* <li>function_request_id</li>
4142
* </ul>
4243
*
4344
* <p>By default {@code Logging} will also create keys for:</p>
@@ -56,17 +57,20 @@
5657
* <p>By default {@code Logging} will not log the event which has trigger the invoke of the Lambda function.
5758
* This can be enabled using {@code @Logging(logEvent = true)}.</p>
5859
*
59-
* <p>By default {@code Logging} all debug logs will follow log4j2 configuration unless configured via
60-
* POWERTOOLS_LOGGER_SAMPLE_RATE environment variable {@code @Logging(samplingRate = <0.0-1.0>)}.</p>
61-
*
6260
* <p>To append additional keys to each log entry you can use {@link LoggingUtils#appendKey(String, String)}</p>
6361
*/
6462
@Retention(RetentionPolicy.RUNTIME)
6563
@Target(ElementType.METHOD)
6664
public @interface Logging {
6765

66+
/**
67+
* Set to true if you want to log the event received by the Lambda function handler
68+
*/
6869
boolean logEvent() default false;
6970

71+
/**
72+
* Sampling rate to change log level to DEBUG. (values must be >=0.0, <=1.0)
73+
*/
7074
double samplingRate() default 0;
7175

7276
/**

powertools-logging/src/main/java/software/amazon/lambda/powertools/logging/internal/LambdaLoggingAspect.java

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ private static LoggingManager loadLoggingManager() {
9393
* <li>JAVA_TOOL_OPTIONS="-Dslf4j.binding=org.apache.logging.slf4j.SLF4JServiceProvider"</li>
9494
* </ul>
9595
* </p>
96+
*
9697
* @return an instance of {@link LoggingManager} or null
9798
*/
9899
private static Optional<LoggingManager> getLoggingManagerFromSlf4jBinding() {
@@ -115,18 +116,26 @@ private static Optional<LoggingManager> getLoggingManagerFromSlf4jBinding() {
115116
.loadClass(managerClass);
116117
loggingManager = (LoggingManager) log4backManagerClass.newInstance();
117118
} else {
118-
LOG.warn("slf4j.binding {} not supported, fallback to Service Loader. " +
119-
"Only log4j and logback are supported.", slf4jBinding);
119+
LOG.warn("slf4j.binding {} not supported, fallback to Service Loader. "
120+
+ "Only log4j and logback are supported.", slf4jBinding);
120121
}
121122
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
122-
LOG.warn("Could not instantiate LoggingManager based on slf4j.binding {}, " +
123-
"make sure to add either powertools-logging-log4j or powertools-logging-logback module",
123+
LOG.warn("Could not instantiate LoggingManager based on slf4j.binding {}, "
124+
+ "make sure to add either powertools-logging-log4j or powertools-logging-logback module",
124125
slf4jBinding);
125126
}
126127
}
127128
return Optional.ofNullable(loggingManager);
128129
}
129130

131+
/**
132+
* Use {@link ServiceLoader} to lookup for a {@link LoggingManager}.
133+
* A file <i>software.amazon.lambda.powertools.logging.internal.LoggingManager</i> must be created in
134+
* <i>META-INF/services/</i> folder with the appropriate implementation of the {@link LoggingManager}
135+
*
136+
* @return an instance of {@link LoggingManager}
137+
* @throws IllegalStateException if no {@link LoggingManager} could be found
138+
*/
130139
private static LoggingManager getLoggingManagerFromServiceLoader() {
131140
LoggingManager loggingManager;
132141

@@ -136,12 +145,12 @@ private static LoggingManager getLoggingManagerFromServiceLoader() {
136145
loggingManagerList.add(lm);
137146
}
138147
if (loggingManagerList.isEmpty()) {
139-
throw new IllegalStateException("No LoggingManager was found on the classpath, " +
140-
"make sure to add either powertools-logging-log4j or powertools-logging-logback to your dependencies");
148+
throw new IllegalStateException("No LoggingManager was found on the classpath, "
149+
+ "make sure to add either powertools-logging-log4j or powertools-logging-logback to your dependencies");
141150
} else if (loggingManagerList.size() > 1) {
142151
throw new IllegalStateException(
143-
"Multiple LoggingManagers were found on the classpath: " + loggingManagerList +
144-
", make sure to have only one of powertools-logging-log4j OR powertools-logging-logback to your dependencies");
152+
"Multiple LoggingManagers were found on the classpath: " + loggingManagerList
153+
+ ", make sure to have only one of powertools-logging-log4j OR powertools-logging-logback to your dependencies");
145154
} else {
146155
loggingManager = loggingManagerList.get(0);
147156
}
@@ -157,6 +166,9 @@ private static void resetLogLevels(Level logLevel) {
157166
public void callAt(Logging logging) {
158167
}
159168

169+
/**
170+
* Main method of the aspect
171+
*/
160172
@Around(value = "callAt(logging) && execution(@Logging * *.*(..))", argNames = "pjp,logging")
161173
public Object around(ProceedingJoinPoint pjp,
162174
Logging logging) throws Throwable {
@@ -215,8 +227,8 @@ private void setLogLevelBasedOnSamplingRate(final ProceedingJoinPoint pjp,
215227
if (samplingRate > sample) {
216228
resetLogLevels(Level.DEBUG);
217229

218-
LOG.debug("Changed log level to DEBUG based on Sampling configuration. " +
219-
"Sampling Rate: {}, Sampler Value: {}.", samplingRate, sample);
230+
LOG.debug("Changed log level to DEBUG based on Sampling configuration. "
231+
+ "Sampling Rate: {}, Sampler Value: {}.", samplingRate, sample);
220232
} else if (LEVEL_AT_INITIALISATION != loggingManager.getLogLevel(LOG)) {
221233
resetLogLevels(LEVEL_AT_INITIALISATION);
222234
}
@@ -229,8 +241,8 @@ private double samplingRate(final Logging logging) {
229241
try {
230242
return Double.parseDouble(sampleRate);
231243
} catch (NumberFormatException e) {
232-
LOG.warn("Skipping sampling rate on environment variable configuration because of invalid " +
233-
"value. Sampling rate: {}", sampleRate);
244+
LOG.warn("Skipping sampling rate on environment variable configuration because of invalid "
245+
+ "value. Sampling rate: {}", sampleRate);
234246
}
235247
}
236248
return logging.samplingRate();
@@ -311,7 +323,7 @@ private Object[] logFromInputStream(final ProceedingJoinPoint pjp) {
311323

312324
private byte[] bytesFromInputStreamSafely(final InputStream inputStream) throws IOException {
313325
try (ByteArrayOutputStream out = new ByteArrayOutputStream();
314-
InputStreamReader reader = new InputStreamReader(inputStream, UTF_8)) {
326+
InputStreamReader reader = new InputStreamReader(inputStream, UTF_8)) {
315327
OutputStreamWriter writer = new OutputStreamWriter(out, UTF_8);
316328
int n;
317329
char[] buffer = new char[4096];

powertools-logging/src/main/java/software/amazon/lambda/powertools/logging/internal/LoggingManager.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@
2525
* </ul>
2626
* <p>
2727
* Implementations are provided in submodules and loaded thanks to a {@link java.util.ServiceLoader}
28-
* (define a file named <code>software.amazon.lambda.powertools.logging.internal.LoggingManager</code> in <code>src/main/resources/META-INF/services</code> with the qualified name of the implementation).
28+
* (define a file named <code>software.amazon.lambda.powertools.logging.internal.LoggingManager</code>
29+
* in <code>src/main/resources/META-INF/services</code> with the qualified name of the implementation).
2930
*/
3031
public interface LoggingManager {
3132
/**
@@ -39,7 +40,8 @@ public interface LoggingManager {
3940
* Retrieve the log Level of a specific logger
4041
*
4142
* @param logger the logger (slf4j) for which to retrieve the log Level
42-
* @return the Level (slf4j) of this logger. Note that SLF4J only support ERROR, WARN, INFO, DEBUG, TRACE while some frameworks may support others (OFF, FATAL, ...)
43+
* @return the Level (slf4j) of this logger.
44+
* Note that SLF4J only support ERROR, WARN, INFO, DEBUG, TRACE while some frameworks may support others (OFF, FATAL, ...)
4345
*/
4446
Level getLogLevel(Logger logger);
4547
}

0 commit comments

Comments
 (0)