Skip to content

Commit 0d58cf9

Browse files
authored
Logging delimiter parametrized (#4745)
Signed-off-by: Maxim Nesen <maxim.nesen@oracle.com>
1 parent 6b87643 commit 0d58cf9

File tree

9 files changed

+264
-79
lines changed

9 files changed

+264
-79
lines changed

core-common/src/main/java/org/glassfish/jersey/logging/ClientLoggingFilter.java

+10-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2016, 2019 Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2016, 2021 Oracle and/or its affiliates. All rights reserved.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License v. 2.0, which is available at
@@ -53,18 +53,20 @@
5353
final class ClientLoggingFilter extends LoggingInterceptor implements ClientRequestFilter, ClientResponseFilter {
5454

5555
/**
56-
* Create a logging filter with custom logger and custom settings of entity
56+
* Create a logging filter using builder instance with custom logger and custom settings of entity
5757
* logging.
5858
*
59-
* @param logger the logger to log messages to.
60-
* @param level level at which the messages will be logged.
61-
* @param verbosity verbosity of the logged messages. See {@link Verbosity}.
62-
* @param maxEntitySize maximum number of entity bytes to be logged (and buffered) - if the entity is larger,
59+
* @param builder loggingFeatureBuilder which contains values for:
60+
* logger the logger to log messages to.
61+
* level level at which the messages will be logged.
62+
* verbosity verbosity of the logged messages. See {@link Verbosity}.
63+
* maxEntitySize maximum number of entity bytes to be logged (and buffered) - if the entity is larger,
6364
* logging filter will print (and buffer in memory) only the specified number of bytes
6465
* and print "...more..." string at the end. Negative values are interpreted as zero.
66+
* separator delimiter for particular log lines. Default is Linux new line delimiter
6567
*/
66-
public ClientLoggingFilter(final Logger logger, final Level level, final Verbosity verbosity, final int maxEntitySize) {
67-
super(logger, level, verbosity, maxEntitySize);
68+
public ClientLoggingFilter(LoggingFeature.LoggingFeatureBuilder builder) {
69+
super(builder);
6870
}
6971

7072
@Override

core-common/src/main/java/org/glassfish/jersey/logging/LoggingFeature.java

+129-37
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2016, 2019 Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2016, 2021 Oracle and/or its affiliates. All rights reserved.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License v. 2.0, which is available at
@@ -40,6 +40,7 @@
4040
* <li>{@link #LOGGING_FEATURE_LOGGER_LEVEL}</li>
4141
* <li>{@link #LOGGING_FEATURE_VERBOSITY}</li>
4242
* <li>{@link #LOGGING_FEATURE_MAX_ENTITY_SIZE}</li>
43+
* <li>{@link #LOGGING_FEATURE_SEPARATOR}</li>
4344
* </ul>
4445
* <p>
4546
* If any of the configuration value is not set, following default values are applied:
@@ -56,13 +57,15 @@
5657
* <li>{@link #LOGGING_FEATURE_LOGGER_LEVEL_SERVER}</li>
5758
* <li>{@link #LOGGING_FEATURE_VERBOSITY_SERVER}</li>
5859
* <li>{@link #LOGGING_FEATURE_MAX_ENTITY_SIZE_SERVER}</li>
60+
* <li>{@link #LOGGING_FEATURE_SEPARATOR_SERVER}</li>
5961
* </ul>
6062
* Client configurable properties:
6163
* <ul>
6264
* <li>{@link #LOGGING_FEATURE_LOGGER_NAME_CLIENT}</li>
6365
* <li>{@link #LOGGING_FEATURE_LOGGER_LEVEL_CLIENT}</li>
6466
* <li>{@link #LOGGING_FEATURE_VERBOSITY_CLIENT}</li>
6567
* <li>{@link #LOGGING_FEATURE_MAX_ENTITY_SIZE_CLIENT}</li>
68+
* <li>{@link #LOGGING_FEATURE_SEPARATOR_CLIENT}</li>
6669
* </ul>
6770
*
6871
* @author Ondrej Kosatka
@@ -86,11 +89,16 @@ public class LoggingFeature implements Feature {
8689
* Default verbosity for entity logging. See {@link Verbosity}.
8790
*/
8891
public static final Verbosity DEFAULT_VERBOSITY = Verbosity.PAYLOAD_TEXT;
92+
/**
93+
* Default separator for entity logging.
94+
*/
95+
public static final String DEFAULT_SEPARATOR = "\n";
8996

9097
private static final String LOGGER_NAME_POSTFIX = ".logger.name";
9198
private static final String LOGGER_LEVEL_POSTFIX = ".logger.level";
9299
private static final String VERBOSITY_POSTFIX = ".verbosity";
93100
private static final String MAX_ENTITY_POSTFIX = ".entity.maxSize";
101+
private static final String SEPARATOR_POSTFIX = ".separator";
94102
private static final String LOGGING_FEATURE_COMMON_PREFIX = "jersey.config.logging";
95103
/**
96104
* Common logger name property.
@@ -108,6 +116,10 @@ public class LoggingFeature implements Feature {
108116
* Common property for configuring a maximum number of bytes of entity to be logged.
109117
*/
110118
public static final String LOGGING_FEATURE_MAX_ENTITY_SIZE = LOGGING_FEATURE_COMMON_PREFIX + MAX_ENTITY_POSTFIX;
119+
/**
120+
* Common property for configuring logging separator.
121+
*/
122+
public static final String LOGGING_FEATURE_SEPARATOR = LOGGING_FEATURE_COMMON_PREFIX + SEPARATOR_POSTFIX;
111123

112124
private static final String LOGGING_FEATURE_SERVER_PREFIX = "jersey.config.server.logging";
113125
/**
@@ -126,6 +138,10 @@ public class LoggingFeature implements Feature {
126138
* Server property for configuring a maximum number of bytes of entity to be logged.
127139
*/
128140
public static final String LOGGING_FEATURE_MAX_ENTITY_SIZE_SERVER = LOGGING_FEATURE_SERVER_PREFIX + MAX_ENTITY_POSTFIX;
141+
/**
142+
* Server property for configuring separator.
143+
*/
144+
public static final String LOGGING_FEATURE_SEPARATOR_SERVER = LOGGING_FEATURE_SERVER_PREFIX + SEPARATOR_POSTFIX;
129145

130146
private static final String LOGGING_FEATURE_CLIENT_PREFIX = "jersey.config.client.logging";
131147
/**
@@ -144,11 +160,12 @@ public class LoggingFeature implements Feature {
144160
* Client property for configuring a maximum number of bytes of entity to be logged.
145161
*/
146162
public static final String LOGGING_FEATURE_MAX_ENTITY_SIZE_CLIENT = LOGGING_FEATURE_CLIENT_PREFIX + MAX_ENTITY_POSTFIX;
163+
/**
164+
* Client property for logging separator.
165+
*/
166+
public static final String LOGGING_FEATURE_SEPARATOR_CLIENT = LOGGING_FEATURE_CLIENT_PREFIX + SEPARATOR_POSTFIX;
147167

148-
private final Logger filterLogger;
149-
private final Verbosity verbosity;
150-
private final Integer maxEntitySize;
151-
private final Level level;
168+
private final LoggingFeatureBuilder builder;
152169

153170
/**
154171
* Creates the feature with default values.
@@ -199,47 +216,84 @@ public LoggingFeature(Logger logger, Integer maxEntitySize) {
199216
* and print "...more..." string at the end. Negative values are interpreted as zero.
200217
*/
201218
public LoggingFeature(Logger logger, Level level, Verbosity verbosity, Integer maxEntitySize) {
202-
this.filterLogger = logger;
203-
this.level = level;
204-
this.verbosity = verbosity;
205-
this.maxEntitySize = maxEntitySize;
219+
220+
this(LoggingFeature.builder()
221+
.withLogger(logger)
222+
.level(level)
223+
.verbosity(verbosity)
224+
.maxEntitySize(maxEntitySize)
225+
.separator(DEFAULT_SEPARATOR)
226+
);
227+
228+
}
229+
230+
/**
231+
* Constructor based on logging feature builder. All parameters are passed through a builder instance.
232+
*
233+
* @param builder instance of a builder with required logging feature parameters
234+
*/
235+
public LoggingFeature(LoggingFeatureBuilder builder) {
236+
this.builder = builder;
206237
}
207238

208239
@Override
209240
public boolean configure(FeatureContext context) {
210-
boolean enabled = false;
241+
boolean enabled = context.getConfiguration().getRuntimeType() != null;
211242

212-
if (context.getConfiguration().getRuntimeType() == RuntimeType.CLIENT) {
213-
ClientLoggingFilter clientLoggingFilter = (ClientLoggingFilter) createLoggingFilter(context, RuntimeType.CLIENT);
214-
context.register(clientLoggingFilter);
215-
enabled = true;
216-
}
217-
if (context.getConfiguration().getRuntimeType() == RuntimeType.SERVER) {
218-
ServerLoggingFilter serverClientFilter = (ServerLoggingFilter) createLoggingFilter(context, RuntimeType.SERVER);
219-
context.register(serverClientFilter);
220-
enabled = true;
243+
if (enabled) {
244+
context.register(createLoggingFilter(context, context.getConfiguration().getRuntimeType()));
221245
}
246+
222247
return enabled;
223248
}
224249

250+
/**
251+
* builder method to create LoggingFeature with required settings
252+
*
253+
* @return Builder for LoggingFeature
254+
*/
255+
public static LoggingFeatureBuilder builder() {
256+
return new LoggingFeatureBuilder();
257+
}
258+
225259
private LoggingInterceptor createLoggingFilter(FeatureContext context, RuntimeType runtimeType) {
226-
Map properties = context.getConfiguration().getProperties();
227-
String filterLoggerName = CommonProperties.getValue(
260+
261+
final LoggingFeatureBuilder loggingBuilder =
262+
configureBuilderParameters(builder, context, runtimeType);
263+
264+
return (runtimeType == RuntimeType.SERVER)
265+
? new ServerLoggingFilter(loggingBuilder)
266+
: new ClientLoggingFilter(loggingBuilder);
267+
}
268+
269+
private static LoggingFeatureBuilder configureBuilderParameters(LoggingFeatureBuilder builder,
270+
FeatureContext context, RuntimeType runtimeType) {
271+
272+
final Map properties = context.getConfiguration().getProperties();
273+
//get values from properties (if any)
274+
final String filterLoggerName = CommonProperties.getValue(
228275
properties,
229276
runtimeType == RuntimeType.SERVER ? LOGGING_FEATURE_LOGGER_NAME_SERVER : LOGGING_FEATURE_LOGGER_NAME_CLIENT,
230277
CommonProperties.getValue(
231278
properties,
232279
LOGGING_FEATURE_LOGGER_NAME,
233280
DEFAULT_LOGGER_NAME
234281
));
235-
String filterLevel = CommonProperties.getValue(
282+
final String filterLevel = CommonProperties.getValue(
236283
properties,
237284
runtimeType == RuntimeType.SERVER ? LOGGING_FEATURE_LOGGER_LEVEL_SERVER : LOGGING_FEATURE_LOGGER_LEVEL_CLIENT,
238285
CommonProperties.getValue(
239286
context.getConfiguration().getProperties(),
240287
LOGGING_FEATURE_LOGGER_LEVEL,
241288
DEFAULT_LOGGER_LEVEL));
242-
Verbosity filterVerbosity = CommonProperties.getValue(
289+
final String filterSeparator = CommonProperties.getValue(
290+
properties,
291+
runtimeType == RuntimeType.SERVER ? LOGGING_FEATURE_SEPARATOR_SERVER : LOGGING_FEATURE_SEPARATOR_CLIENT,
292+
CommonProperties.getValue(
293+
context.getConfiguration().getProperties(),
294+
LOGGING_FEATURE_SEPARATOR,
295+
DEFAULT_SEPARATOR));
296+
final Verbosity filterVerbosity = CommonProperties.getValue(
243297
properties,
244298
runtimeType == RuntimeType.SERVER ? LOGGING_FEATURE_VERBOSITY_SERVER : LOGGING_FEATURE_VERBOSITY_CLIENT,
245299
CommonProperties.getValue(
@@ -257,19 +311,16 @@ private LoggingInterceptor createLoggingFilter(FeatureContext context, RuntimeTy
257311
DEFAULT_MAX_ENTITY_SIZE
258312
));
259313

260-
Level loggerLevel = Level.parse(filterLevel);
261-
262-
if (runtimeType == RuntimeType.SERVER) {
263-
return new ServerLoggingFilter(filterLogger != null ? filterLogger : Logger.getLogger(filterLoggerName),
264-
level != null ? level : loggerLevel,
265-
verbosity != null ? verbosity : filterVerbosity,
266-
maxEntitySize != null ? maxEntitySize : filterMaxEntitySize);
267-
} else {
268-
return new ClientLoggingFilter(filterLogger != null ? filterLogger : Logger.getLogger(filterLoggerName),
269-
level != null ? level : loggerLevel,
270-
verbosity != null ? verbosity : filterVerbosity,
271-
maxEntitySize != null ? maxEntitySize : filterMaxEntitySize);
272-
}
314+
final Level loggerLevel = Level.parse(filterLevel);
315+
316+
//configure builder vs properties values
317+
builder.filterLogger = builder.filterLogger == null ? Logger.getLogger(filterLoggerName) : builder.filterLogger;
318+
builder.verbosity = builder.verbosity == null ? filterVerbosity : builder.verbosity;
319+
builder.maxEntitySize = builder.maxEntitySize == null ? filterMaxEntitySize : builder.maxEntitySize;
320+
builder.level = builder.level == null ? loggerLevel : builder.level;
321+
builder.separator = builder.separator == null ? filterSeparator : builder.separator;
322+
323+
return builder;
273324
}
274325

275326
/**
@@ -313,4 +364,45 @@ public enum Verbosity {
313364
*/
314365
PAYLOAD_ANY
315366
}
316-
}
367+
368+
/**
369+
* Builder class for logging feature configuration. Accepts parameters for the filter logger, verbosity, max
370+
* entity size, level, and separator.
371+
*/
372+
public static class LoggingFeatureBuilder {
373+
374+
Logger filterLogger;
375+
Verbosity verbosity;
376+
Integer maxEntitySize;
377+
Level level;
378+
String separator;
379+
380+
public LoggingFeatureBuilder() {
381+
382+
}
383+
public LoggingFeatureBuilder withLogger(Logger logger) {
384+
this.filterLogger = logger;
385+
return this;
386+
}
387+
public LoggingFeatureBuilder verbosity(Verbosity verbosity) {
388+
this.verbosity = verbosity;
389+
return this;
390+
}
391+
public LoggingFeatureBuilder maxEntitySize(Integer maxEntitySize) {
392+
this.maxEntitySize = maxEntitySize;
393+
return this;
394+
}
395+
public LoggingFeatureBuilder level(Level level) {
396+
this.level = level;
397+
return this;
398+
}
399+
public LoggingFeatureBuilder separator(String separator) {
400+
this.separator = separator;
401+
return this;
402+
}
403+
404+
public LoggingFeature build() {
405+
return new LoggingFeature(this);
406+
}
407+
}
408+
}

core-common/src/main/java/org/glassfish/jersey/logging/LoggingFeatureAutoDiscoverable.java

+10-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2016, 2019 Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2016, 2021 Oracle and/or its affiliates. All rights reserved.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License v. 2.0, which is available at
@@ -34,6 +34,9 @@
3434
import static org.glassfish.jersey.logging.LoggingFeature.LOGGING_FEATURE_MAX_ENTITY_SIZE;
3535
import static org.glassfish.jersey.logging.LoggingFeature.LOGGING_FEATURE_MAX_ENTITY_SIZE_CLIENT;
3636
import static org.glassfish.jersey.logging.LoggingFeature.LOGGING_FEATURE_MAX_ENTITY_SIZE_SERVER;
37+
import static org.glassfish.jersey.logging.LoggingFeature.LOGGING_FEATURE_SEPARATOR;
38+
import static org.glassfish.jersey.logging.LoggingFeature.LOGGING_FEATURE_SEPARATOR_CLIENT;
39+
import static org.glassfish.jersey.logging.LoggingFeature.LOGGING_FEATURE_SEPARATOR_SERVER;
3740
import static org.glassfish.jersey.logging.LoggingFeature.LOGGING_FEATURE_VERBOSITY;
3841
import static org.glassfish.jersey.logging.LoggingFeature.LOGGING_FEATURE_VERBOSITY_CLIENT;
3942
import static org.glassfish.jersey.logging.LoggingFeature.LOGGING_FEATURE_VERBOSITY_SERVER;
@@ -71,20 +74,23 @@ private boolean commonPropertyConfigured(Map properties) {
7174
return properties.containsKey(LOGGING_FEATURE_LOGGER_NAME)
7275
|| properties.containsKey(LOGGING_FEATURE_LOGGER_LEVEL)
7376
|| properties.containsKey(LOGGING_FEATURE_VERBOSITY)
74-
|| properties.containsKey(LOGGING_FEATURE_MAX_ENTITY_SIZE);
77+
|| properties.containsKey(LOGGING_FEATURE_MAX_ENTITY_SIZE)
78+
|| properties.containsKey(LOGGING_FEATURE_SEPARATOR);
7579
}
7680

7781
private boolean clientConfigured(Map properties) {
7882
return properties.containsKey(LOGGING_FEATURE_LOGGER_NAME_CLIENT)
7983
|| properties.containsKey(LOGGING_FEATURE_LOGGER_LEVEL_CLIENT)
8084
|| properties.containsKey(LOGGING_FEATURE_VERBOSITY_CLIENT)
81-
|| properties.containsKey(LOGGING_FEATURE_MAX_ENTITY_SIZE_CLIENT);
85+
|| properties.containsKey(LOGGING_FEATURE_MAX_ENTITY_SIZE_CLIENT)
86+
|| properties.containsKey(LOGGING_FEATURE_SEPARATOR_CLIENT);
8287
}
8388

8489
private boolean serverConfigured(Map properties) {
8590
return properties.containsKey(LOGGING_FEATURE_LOGGER_NAME_SERVER)
8691
|| properties.containsKey(LOGGING_FEATURE_LOGGER_LEVEL_SERVER)
8792
|| properties.containsKey(LOGGING_FEATURE_VERBOSITY_SERVER)
88-
|| properties.containsKey(LOGGING_FEATURE_MAX_ENTITY_SIZE_SERVER);
93+
|| properties.containsKey(LOGGING_FEATURE_MAX_ENTITY_SIZE_SERVER)
94+
|| properties.containsKey(LOGGING_FEATURE_SEPARATOR_SERVER);
8995
}
9096
}

0 commit comments

Comments
 (0)