Skip to content

Commit 4d6f2df

Browse files
committed
Add LogUtils and HttpLogging
SPR-17012
1 parent 23d4862 commit 4d6f2df

22 files changed

+212
-112
lines changed

spring-web/src/main/java/org/springframework/http/HttpLog.java renamed to spring-core/src/main/java/org/springframework/util/log/CompositeLog.java

Lines changed: 11 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -13,36 +13,23 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package org.springframework.http;
16+
package org.springframework.util.log;
1717

18-
import java.util.ArrayList;
19-
import java.util.Collections;
2018
import java.util.List;
2119
import java.util.function.Predicate;
2220

2321
import org.apache.commons.logging.Log;
24-
import org.apache.commons.logging.LogFactory;
2522
import org.apache.commons.logging.impl.NoOpLog;
2623

27-
import org.springframework.util.ObjectUtils;
28-
2924
/**
30-
* Composite {@link Log} configured with a primary logger and a list of secondary
31-
* ones to fall back on if the main one is not enabled.
32-
*
33-
* <p>This class also declares {@link #webLogger} for use as fallback when
34-
* logging in the "org.springframework.http" package.
25+
* Implementation of {@link Log} that wraps a list of loggers and delegates to
26+
* the first one for which logging is enabled at the given level.
3527
*
3628
* @author Rossen Stoyanchev
3729
* @since 5.1
30+
* @see LogUtils#getCompositeLog
3831
*/
39-
public final class HttpLog implements Log {
40-
41-
/**
42-
* Logger with category "org.springframework.web.HTTP" to use as fallback
43-
* if "org.springframework.web" is on.
44-
*/
45-
public static final Log webLogger = LogFactory.getLog("org.springframework.web.HTTP");
32+
class CompositeLog implements Log {
4633

4734
private static final Log noOpLog = new NoOpLog();
4835

@@ -60,7 +47,12 @@ public final class HttpLog implements Log {
6047
private final Log traceLogger;
6148

6249

63-
private HttpLog(List<Log> loggers) {
50+
/**
51+
* Constructor with list of loggers. For optimal performance, the constructor
52+
* checks and remembers which logger is on for each log category.
53+
* @param loggers the loggers to use
54+
*/
55+
public CompositeLog(List<Log> loggers) {
6456
this.fatalLogger = initLogger(loggers, Log::isFatalEnabled);
6557
this.errorLogger = initLogger(loggers, Log::isErrorEnabled);
6658
this.warnLogger = initLogger(loggers, Log::isWarnEnabled);
@@ -163,33 +155,4 @@ public void trace(Object message) {
163155
public void trace(Object message, Throwable ex) {
164156
this.traceLogger.trace(message, ex);
165157
}
166-
167-
168-
/**
169-
* Create a composite logger that uses the given primary logger, if enabled,
170-
* or falls back on {@link #webLogger}.
171-
* @param primaryLogger the primary logger
172-
* @return a composite logger
173-
*/
174-
public static Log create(Log primaryLogger) {
175-
return createWith(primaryLogger, webLogger);
176-
}
177-
178-
/**
179-
* Create a composite logger that uses the given primary logger, if enabled,
180-
* or falls back on one of the given secondary loggers..
181-
* @param primaryLogger the primary logger
182-
* @param secondaryLoggers fallback loggers
183-
* @return a composite logger
184-
*/
185-
public static Log createWith(Log primaryLogger, Log... secondaryLoggers) {
186-
if (ObjectUtils.isEmpty(secondaryLoggers)) {
187-
return primaryLogger;
188-
}
189-
List<Log> loggers = new ArrayList<>(1 + secondaryLoggers.length);
190-
loggers.add(primaryLogger);
191-
Collections.addAll(loggers, secondaryLoggers);
192-
return new HttpLog(loggers);
193-
}
194-
195158
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* Copyright 2002-2018 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.util.log;
17+
18+
import java.util.ArrayList;
19+
import java.util.Collections;
20+
import java.util.List;
21+
22+
import org.apache.commons.logging.Log;
23+
import org.apache.commons.logging.LogFactory;
24+
25+
/**
26+
* Utilities to assist with logging. Mainly for internal use within the framework
27+
* with Appache Commons Logging.
28+
*
29+
* @author Rossen Stoyanchev
30+
* @since 5.1
31+
*/
32+
public abstract class LogUtils {
33+
34+
/**
35+
* Create a composite logger that delegates to a primary or falls back on a
36+
* secondary logger if logging for the primary logger is not enabled.
37+
* <p>This may be used for fallback logging from lower level packages that
38+
* logically should log together with some higher level package but the two
39+
* don't happen to share a suitable parent package (e.g. logging for the web
40+
* and lower level http and codec packages). For such cases the primary,
41+
* class-based logger can be wrapped with a shared fallback logger.
42+
* @param primaryLogger primary logger to try first
43+
* @param secondaryLogger secondary logger
44+
* @param tertiaryLoggers optionally, more fallback loggers
45+
* @return the resulting logger to use
46+
*/
47+
public static Log getCompositeLog(Log primaryLogger, Log secondaryLogger, Log... tertiaryLoggers) {
48+
List<Log> loggers = new ArrayList<>(2 + tertiaryLoggers.length);
49+
loggers.add(primaryLogger);
50+
loggers.add(secondaryLogger);
51+
Collections.addAll(loggers, tertiaryLoggers);
52+
return new CompositeLog(loggers);
53+
}
54+
55+
/**
56+
* Create a "hidden" logger whose name is intentionally prefixed with "_"
57+
* because its output is either too verbose or otherwise deemed as optional
58+
* or unnecessary to see at any log level by default under the normal package
59+
* based log hierarchy.
60+
* @param clazz the class for which to create a logger
61+
* @return the created logger
62+
*/
63+
public static Log getHiddenLog(Class<?> clazz) {
64+
return LogFactory.getLog("_" + clazz.getName());
65+
}
66+
67+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/**
2+
* Useful helper classes for logging.
3+
*/
4+
@NonNullApi
5+
@NonNullFields
6+
package org.springframework.util.log;
7+
8+
import org.springframework.lang.NonNullApi;
9+
import org.springframework.lang.NonNullFields;
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Copyright 2002-2018 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.http;
17+
18+
import org.apache.commons.logging.Log;
19+
import org.apache.commons.logging.LogFactory;
20+
21+
import org.springframework.util.log.LogUtils;
22+
23+
/**
24+
* Holds the shared logger named "org.springframework.web.HttpLogging" for HTTP
25+
* related logging when "org.springframework.http" is not enabled but
26+
* "org.springframework.web" is.
27+
*
28+
* <p>That means "org.springframework.web" enables all web logging including
29+
* from lower level packages such as "org.springframework.http" and modules
30+
* such as codecs from {@literal "spring-core"} when those are wrapped with
31+
* {@link org.springframework.http.codec.EncoderHttpMessageWriter EncoderHttpMessageWriter} or
32+
* {@link org.springframework.http.codec.DecoderHttpMessageReader DecoderHttpMessageReader}.
33+
*
34+
* <p>To see logging from the primary class loggers simply enable logging for
35+
* "org.springframework.http" and "org.springframework.codec".
36+
*
37+
* @author Rossen Stoyanchev
38+
* @since 5.1
39+
*/
40+
public abstract class HttpLogging {
41+
42+
private static final Log fallbackLogger =
43+
LogFactory.getLog("org.springframework.web." + HttpLogging.class.getSimpleName());
44+
45+
46+
/**
47+
* Create a primary logger for the given class and wrap it with a composite
48+
* that delegates to it or to the fallback logger
49+
* "org.springframework.web.HttpLogging", if the primary is not enabled.
50+
* @param primaryLoggerClass the class for the name of the primary logger
51+
* @return the resulting composite logger
52+
*/
53+
public static Log forLogName(Class<?> primaryLoggerClass) {
54+
Log primaryLogger = LogFactory.getLog(primaryLoggerClass);
55+
return forLog(primaryLogger);
56+
}
57+
58+
/**
59+
* Wrap the given primary logger with a composite logger that delegates to
60+
* it or to the fallback logger "org.springframework.web.HttpLogging", if
61+
* the primary is not enabled.
62+
* @param primaryLogger the primary logger to use
63+
* @return the resulting composite logger
64+
*/
65+
public static Log forLog(Log primaryLogger) {
66+
return LogUtils.getCompositeLog(primaryLogger, fallbackLogger);
67+
}
68+
69+
}

spring-web/src/main/java/org/springframework/http/client/support/AsyncHttpAccessor.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
import java.net.URI;
2121

2222
import org.apache.commons.logging.Log;
23-
import org.apache.commons.logging.LogFactory;
2423

24+
import org.springframework.http.HttpLogging;
2525
import org.springframework.http.HttpMethod;
2626
import org.springframework.lang.Nullable;
2727
import org.springframework.util.Assert;
@@ -44,7 +44,7 @@
4444
public class AsyncHttpAccessor {
4545

4646
/** Logger available to subclasses. */
47-
protected final Log logger = LogFactory.getLog(getClass());
47+
protected final Log logger = HttpLogging.forLogName(getClass());
4848

4949
@Nullable
5050
private org.springframework.http.client.AsyncClientHttpRequestFactory asyncRequestFactory;

spring-web/src/main/java/org/springframework/http/client/support/HttpAccessor.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
import java.net.URI;
2121

2222
import org.apache.commons.logging.Log;
23-
import org.apache.commons.logging.LogFactory;
2423

24+
import org.springframework.http.HttpLogging;
2525
import org.springframework.http.HttpMethod;
2626
import org.springframework.http.client.ClientHttpRequest;
2727
import org.springframework.http.client.ClientHttpRequestFactory;
@@ -45,7 +45,7 @@
4545
public abstract class HttpAccessor {
4646

4747
/** Logger available to subclasses. */
48-
protected final Log logger = LogFactory.getLog(getClass());
48+
protected final Log logger = HttpLogging.forLogName(getClass());
4949

5050
private ClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
5151

spring-web/src/main/java/org/springframework/http/codec/DecoderHttpMessageReader.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
import org.springframework.core.codec.AbstractDecoder;
2828
import org.springframework.core.codec.Decoder;
2929
import org.springframework.core.codec.Hints;
30-
import org.springframework.http.HttpLog;
30+
import org.springframework.http.HttpLogging;
3131
import org.springframework.http.HttpMessage;
3232
import org.springframework.http.MediaType;
3333
import org.springframework.http.ReactiveHttpInputMessage;
@@ -70,7 +70,7 @@ private void initLogger(Decoder<T> decoder) {
7070
if (decoder instanceof AbstractDecoder &&
7171
decoder.getClass().getPackage().getName().startsWith("org.springframework.core.codec")) {
7272

73-
Log logger = HttpLog.create(((AbstractDecoder) decoder).getLogger());
73+
Log logger = HttpLogging.forLog(((AbstractDecoder) decoder).getLogger());
7474
((AbstractDecoder) decoder).setLogger(logger);
7575
}
7676
}

spring-web/src/main/java/org/springframework/http/codec/EncoderHttpMessageWriter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
import org.springframework.core.codec.Hints;
3131
import org.springframework.core.io.buffer.DataBuffer;
3232
import org.springframework.http.HttpHeaders;
33-
import org.springframework.http.HttpLog;
33+
import org.springframework.http.HttpLogging;
3434
import org.springframework.http.MediaType;
3535
import org.springframework.http.ReactiveHttpOutputMessage;
3636
import org.springframework.http.server.reactive.ServerHttpRequest;
@@ -76,7 +76,7 @@ private void initLogger(Encoder<T> encoder) {
7676
if (encoder instanceof AbstractEncoder &&
7777
encoder.getClass().getPackage().getName().startsWith("org.springframework.core.codec")) {
7878

79-
Log logger = HttpLog.create(((AbstractEncoder) encoder).getLogger());
79+
Log logger = HttpLogging.forLog(((AbstractEncoder) encoder).getLogger());
8080
((AbstractEncoder) encoder).setLogger(logger);
8181
}
8282
}

spring-web/src/main/java/org/springframework/http/codec/LoggingCodecSupport.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,8 @@
1616
package org.springframework.http.codec;
1717

1818
import org.apache.commons.logging.Log;
19-
import org.apache.commons.logging.LogFactory;
2019

21-
import org.springframework.http.HttpLog;
20+
import org.springframework.http.HttpLogging;
2221

2322
/**
2423
* Base class for {@link org.springframework.core.codec.Encoder},
@@ -31,7 +30,7 @@
3130
*/
3231
public class LoggingCodecSupport {
3332

34-
protected final Log logger = HttpLog.create(LogFactory.getLog(getClass()));
33+
protected final Log logger = HttpLogging.forLogName(getClass());
3534

3635
/** Whether to log potentially sensitive info (form data at DEBUG and headers at TRACE). */
3736
private boolean enableLoggingRequestDetails = false;

spring-web/src/main/java/org/springframework/http/codec/ResourceHttpMessageWriter.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import java.util.Optional;
2424

2525
import org.apache.commons.logging.Log;
26-
import org.apache.commons.logging.LogFactory;
2726
import org.reactivestreams.Publisher;
2827
import reactor.core.publisher.Flux;
2928
import reactor.core.publisher.Mono;
@@ -39,7 +38,7 @@
3938
import org.springframework.core.io.buffer.DataBufferFactory;
4039
import org.springframework.core.io.support.ResourceRegion;
4140
import org.springframework.http.HttpHeaders;
42-
import org.springframework.http.HttpLog;
41+
import org.springframework.http.HttpLogging;
4342
import org.springframework.http.HttpRange;
4443
import org.springframework.http.HttpStatus;
4544
import org.springframework.http.MediaType;
@@ -73,7 +72,7 @@ public class ResourceHttpMessageWriter implements HttpMessageWriter<Resource> {
7372

7473
private static final ResolvableType REGION_TYPE = ResolvableType.forClass(ResourceRegion.class);
7574

76-
private static final Log logger = HttpLog.create(LogFactory.getLog(ResourceHttpMessageWriter.class));
75+
private static final Log logger = HttpLogging.forLogName(ResourceHttpMessageWriter.class);
7776

7877

7978
private final ResourceEncoder encoder;

spring-web/src/main/java/org/springframework/http/codec/json/Jackson2CodecSupport.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,12 @@
2929
import com.fasterxml.jackson.databind.ObjectMapper;
3030
import com.fasterxml.jackson.databind.type.TypeFactory;
3131
import org.apache.commons.logging.Log;
32-
import org.apache.commons.logging.LogFactory;
3332

3433
import org.springframework.core.GenericTypeResolver;
3534
import org.springframework.core.MethodParameter;
3635
import org.springframework.core.ResolvableType;
3736
import org.springframework.core.codec.Hints;
38-
import org.springframework.http.HttpLog;
37+
import org.springframework.http.HttpLogging;
3938
import org.springframework.lang.Nullable;
4039
import org.springframework.util.Assert;
4140
import org.springframework.util.MimeType;
@@ -66,7 +65,7 @@ public abstract class Jackson2CodecSupport {
6665
new MimeType("application", "*+json", StandardCharsets.UTF_8)));
6766

6867

69-
protected final Log logger = HttpLog.create(LogFactory.getLog(getClass()));
68+
protected final Log logger = HttpLogging.forLogName(getClass());
7069

7170
private final ObjectMapper objectMapper;
7271

0 commit comments

Comments
 (0)