1
1
/*
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.
3
3
*
4
4
* This program and the accompanying materials are made available under the
5
5
* terms of the Eclipse Public License v. 2.0, which is available at
40
40
* <li>{@link #LOGGING_FEATURE_LOGGER_LEVEL}</li>
41
41
* <li>{@link #LOGGING_FEATURE_VERBOSITY}</li>
42
42
* <li>{@link #LOGGING_FEATURE_MAX_ENTITY_SIZE}</li>
43
+ * <li>{@link #LOGGING_FEATURE_SEPARATOR}</li>
43
44
* </ul>
44
45
* <p>
45
46
* If any of the configuration value is not set, following default values are applied:
56
57
* <li>{@link #LOGGING_FEATURE_LOGGER_LEVEL_SERVER}</li>
57
58
* <li>{@link #LOGGING_FEATURE_VERBOSITY_SERVER}</li>
58
59
* <li>{@link #LOGGING_FEATURE_MAX_ENTITY_SIZE_SERVER}</li>
60
+ * <li>{@link #LOGGING_FEATURE_SEPARATOR_SERVER}</li>
59
61
* </ul>
60
62
* Client configurable properties:
61
63
* <ul>
62
64
* <li>{@link #LOGGING_FEATURE_LOGGER_NAME_CLIENT}</li>
63
65
* <li>{@link #LOGGING_FEATURE_LOGGER_LEVEL_CLIENT}</li>
64
66
* <li>{@link #LOGGING_FEATURE_VERBOSITY_CLIENT}</li>
65
67
* <li>{@link #LOGGING_FEATURE_MAX_ENTITY_SIZE_CLIENT}</li>
68
+ * <li>{@link #LOGGING_FEATURE_SEPARATOR_CLIENT}</li>
66
69
* </ul>
67
70
*
68
71
* @author Ondrej Kosatka
@@ -86,11 +89,16 @@ public class LoggingFeature implements Feature {
86
89
* Default verbosity for entity logging. See {@link Verbosity}.
87
90
*/
88
91
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 " ;
89
96
90
97
private static final String LOGGER_NAME_POSTFIX = ".logger.name" ;
91
98
private static final String LOGGER_LEVEL_POSTFIX = ".logger.level" ;
92
99
private static final String VERBOSITY_POSTFIX = ".verbosity" ;
93
100
private static final String MAX_ENTITY_POSTFIX = ".entity.maxSize" ;
101
+ private static final String SEPARATOR_POSTFIX = ".separator" ;
94
102
private static final String LOGGING_FEATURE_COMMON_PREFIX = "jersey.config.logging" ;
95
103
/**
96
104
* Common logger name property.
@@ -108,6 +116,10 @@ public class LoggingFeature implements Feature {
108
116
* Common property for configuring a maximum number of bytes of entity to be logged.
109
117
*/
110
118
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 ;
111
123
112
124
private static final String LOGGING_FEATURE_SERVER_PREFIX = "jersey.config.server.logging" ;
113
125
/**
@@ -126,6 +138,10 @@ public class LoggingFeature implements Feature {
126
138
* Server property for configuring a maximum number of bytes of entity to be logged.
127
139
*/
128
140
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 ;
129
145
130
146
private static final String LOGGING_FEATURE_CLIENT_PREFIX = "jersey.config.client.logging" ;
131
147
/**
@@ -144,11 +160,12 @@ public class LoggingFeature implements Feature {
144
160
* Client property for configuring a maximum number of bytes of entity to be logged.
145
161
*/
146
162
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 ;
147
167
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 ;
152
169
153
170
/**
154
171
* Creates the feature with default values.
@@ -199,47 +216,84 @@ public LoggingFeature(Logger logger, Integer maxEntitySize) {
199
216
* and print "...more..." string at the end. Negative values are interpreted as zero.
200
217
*/
201
218
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 ;
206
237
}
207
238
208
239
@ Override
209
240
public boolean configure (FeatureContext context ) {
210
- boolean enabled = false ;
241
+ boolean enabled = context . getConfiguration (). getRuntimeType () != null ;
211
242
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 ()));
221
245
}
246
+
222
247
return enabled ;
223
248
}
224
249
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
+
225
259
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 (
228
275
properties ,
229
276
runtimeType == RuntimeType .SERVER ? LOGGING_FEATURE_LOGGER_NAME_SERVER : LOGGING_FEATURE_LOGGER_NAME_CLIENT ,
230
277
CommonProperties .getValue (
231
278
properties ,
232
279
LOGGING_FEATURE_LOGGER_NAME ,
233
280
DEFAULT_LOGGER_NAME
234
281
));
235
- String filterLevel = CommonProperties .getValue (
282
+ final String filterLevel = CommonProperties .getValue (
236
283
properties ,
237
284
runtimeType == RuntimeType .SERVER ? LOGGING_FEATURE_LOGGER_LEVEL_SERVER : LOGGING_FEATURE_LOGGER_LEVEL_CLIENT ,
238
285
CommonProperties .getValue (
239
286
context .getConfiguration ().getProperties (),
240
287
LOGGING_FEATURE_LOGGER_LEVEL ,
241
288
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 (
243
297
properties ,
244
298
runtimeType == RuntimeType .SERVER ? LOGGING_FEATURE_VERBOSITY_SERVER : LOGGING_FEATURE_VERBOSITY_CLIENT ,
245
299
CommonProperties .getValue (
@@ -257,19 +311,16 @@ private LoggingInterceptor createLoggingFilter(FeatureContext context, RuntimeTy
257
311
DEFAULT_MAX_ENTITY_SIZE
258
312
));
259
313
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 ;
273
324
}
274
325
275
326
/**
@@ -313,4 +364,45 @@ public enum Verbosity {
313
364
*/
314
365
PAYLOAD_ANY
315
366
}
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
+ }
0 commit comments