Skip to content

Commit 31024b1

Browse files
committed
Add sections on logging for Spring MVC and WebFlux
Issue: SPR-17032
1 parent 92eaf99 commit 31024b1

File tree

2 files changed

+127
-0
lines changed

2 files changed

+127
-0
lines changed

src/docs/asciidoc/web/webflux.adoc

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -712,6 +712,76 @@ a heartbeat and ignore.
712712

713713

714714

715+
[[webflux-logging]]
716+
=== Logging
717+
[.small]#<<web.adoc#mvc-logging,Same in Spring MVC>>#
718+
719+
DEBUG level logging in Spring WebFlux is designed to be compact, minimal, and
720+
human-friendly. It focuses on high value bits of information that are useful over and
721+
over again vs others that are useful only when debugging a specific issue.
722+
723+
TRACE level logging generally follows the same principles as DEBUG (and for example also
724+
should not be a firehose) but can be used for debugging any issue. In addition some log
725+
messages may show a different level of detail at TRACE vs DEBUG.
726+
727+
Good logging comes from the experience of using the logs. If you spot anything that does
728+
not meet the stated goals, please let us know.
729+
730+
731+
[[webflux-logging-id]]
732+
==== Log Id
733+
734+
In WebFlux, a single request may be executed over multiple threads and the thread id
735+
is not useful for correlating log messages that belong to a specific request. This is why
736+
WebFlux log messages are prefixed with a request specific id by default.
737+
738+
On the server side the log id is stored in the `ServerWebExchange` attribute
739+
{api-spring-framework}/web/server/ServerWebExchange.html#LOG_ID_ATTRIBUTE[LOG_ID_ATTRIBUTE]
740+
while a fully formatted prefix based on that id is available via
741+
`ServerWebExchange#getLogPrefix()`. On the `WebClient` side, the log id is stored in the
742+
`ClientRequest` attribute
743+
{api-spring-framework}/web/reactive/function/client/ClientRequest.html#LOG_ID_ATTRIBUTE[LOG_ID_ATTRIBUTE]
744+
while a fully formatted prefix is available via `ClientRequest#logPrefix()`.
745+
746+
747+
[[webflux-logging-sensitive-data]]
748+
==== Sensitive Data
749+
[.small]#<<web.adoc#mvc-logging-sensitive-data,Same in Spring MVC>>#
750+
751+
DEBUG and TRACE logging may log sensitive information. This is why form parameters and
752+
headers are masked by default and their logging in full must be enabled explicitly.
753+
754+
For server side requests:
755+
756+
[source,java,indent=0]
757+
[subs="verbatim,quotes"]
758+
----
759+
@Configuration
760+
@EnableWebFlux
761+
class MyConfig implements WebFluxConfigurer {
762+
763+
@Override
764+
public void configureHttpMessageCodecs(ServerCodecConfigurer configurer) {
765+
configurer.defaultCodecs().enableLoggingRequestDetails(true);
766+
}
767+
}
768+
----
769+
770+
For client side requests:
771+
772+
[source,java,indent=0]
773+
[subs="verbatim,quotes"]
774+
----
775+
Consumer<ClientCodecConfigurer> consumer = configurer ->
776+
configurer.defaultCodecs().enableLoggingRequestDetails(true);
777+
778+
WebClient webClient = WebClient.builder()
779+
.exchangeStrategies(ExchangeStrategies.builder().codecs(consumer).build())
780+
.build();
781+
----
782+
783+
784+
715785

716786
[[webflux-dispatcher-handler]]
717787
== DispatcherHandler

src/docs/asciidoc/web/webmvc.adoc

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1036,6 +1036,63 @@ Once the Servlet 3.0 configuration is in place, simply add a bean of type
10361036

10371037

10381038

1039+
[[mvc-logging]]
1040+
=== Logging
1041+
[.small]#<<web-reactive.adoc#webflux-logging,Same in Spring WebFlux>>#
1042+
1043+
DEBUG level logging in Spring MVC is designed to be compact, minimal, and
1044+
human-friendly. It focuses on high value bits of information that are useful over and
1045+
over again vs others that are useful only when debugging a specific issue.
1046+
1047+
TRACE level logging generally follows the same principles as DEBUG (and for example also
1048+
should not be a firehose) but can be used for debugging any issue. In addition some log
1049+
messages may show a different level of detail at TRACE vs DEBUG.
1050+
1051+
Good logging comes from the experience of using the logs. If you spot anything that does
1052+
not meet the stated goals, please let us know.
1053+
1054+
1055+
[[mvc-logging-sensitive-data]]
1056+
==== Sensitive Data
1057+
[.small]#<<web-reactive.adoc#webflux-logging-sensitive-data,Same in Spring WebFlux>>#
1058+
1059+
DEBUG and TRACE logging may log sensitive information. This is why request parameters and
1060+
headers are masked by default and their logging in full must be enabled explicitly
1061+
through the `enableLoggingRequestDetails` property on `DispatcherServlet`.
1062+
1063+
For example if using Java config:
1064+
1065+
[source,java,indent=0]
1066+
[subs="verbatim,quotes"]
1067+
----
1068+
public class MyInitializer
1069+
extends AbstractAnnotationConfigDispatcherServletInitializer {
1070+
1071+
@Override
1072+
protected Class<?>[] getRootConfigClasses() {
1073+
return ... ;
1074+
}
1075+
1076+
@Override
1077+
protected Class<?>[] getServletConfigClasses() {
1078+
return ... ;
1079+
}
1080+
1081+
@Override
1082+
protected String[] getServletMappings() {
1083+
return ... ;
1084+
}
1085+
1086+
@Override
1087+
protected void customizeRegistration(Dynamic registration) {
1088+
registration.setInitParameter("enableLoggingRequestDetails", "true");
1089+
}
1090+
1091+
}
1092+
----
1093+
1094+
1095+
10391096

10401097
[[filters]]
10411098
== Filters

0 commit comments

Comments
 (0)