Skip to content

Commit 4ed2380

Browse files
committed
Polish contribution
Reduce visibility Remove duplications Signed-off-by: Violeta Georgieva <[email protected]>
1 parent 2d585b6 commit 4ed2380

File tree

22 files changed

+384
-445
lines changed

22 files changed

+384
-445
lines changed

docs/modules/ROOT/pages/http-server.adoc

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -504,31 +504,31 @@ The following example enables it programmatically:
504504

505505
{examples-link}/errorLog/Application.java
506506
----
507-
include::{examples-dir}/errorLog/Application.java[lines=21..32]
507+
include::{examples-dir}/errorLog/Application.java[lines=18..32]
508508
----
509509

510510
Calling this method takes precedence over the system property configuration.
511511

512-
By default, the logging format is `[{datetime}] [error] [client {remote address}] {exception message}`, but you can
512+
By default, the logging format is `[+{datetime}+] [pid +{PID}+] [client {remote address}] {exception message}`, but you can
513513
specify a custom one as a parameter, as in the following example:
514514

515515
{examples-link}/errorLog/CustomLogErrorFormatApplication.java
516516
----
517-
include::{examples-dir}/errorLog/CustomLogErrorFormatApplication.java[lines=22..36]
517+
include::{examples-dir}/errorLog/CustomLogErrorFormatApplication.java[lines=18..33]
518518
----
519519

520520
You can also filter `HTTP` error logs by using the `ErrorLogFactory#createFilter` method, as in the following example:
521521

522522
{examples-link}/errorLog/FilterLogErrorApplication.java
523523
----
524-
include::{examples-dir}/errorLog/FilterLogErrorApplication.java[lines=22..36]
524+
include::{examples-dir}/errorLog/FilterLogErrorApplication.java[lines=18..33]
525525
----
526526

527527
Note that this method can take a custom format parameter too, as in this example:
528528

529529
{examples-link}/errorLog/CustomFormatAndFilterErrorLogApplication.java.java
530530
----
531-
include::{examples-dir}/errorLog/CustomFormatAndFilterErrorLogApplication.java[lines=23..40]
531+
include::{examples-dir}/errorLog/CustomFormatAndFilterErrorLogApplication.java[lines=18..35]
532532
----
533533
<1> Specifies the filter predicate to use
534534
<2> Specifies the custom format to apply

reactor-netty-core/src/main/java/reactor/netty/NettyPipeline.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,11 @@ public interface NettyPipeline {
8989
String RIGHT = "reactor.right.";
9090

9191
String AccessLogHandler = LEFT + "accessLogHandler";
92-
String ErrorLogHandler = LEFT + "errorLogHandler";
9392
String ChannelMetricsHandler = LEFT + "channelMetricsHandler";
9493
String ChunkedWriter = LEFT + "chunkedWriter";
9594
String CompressionHandler = LEFT + "compressionHandler";
9695
String ConnectMetricsHandler = LEFT + "connectMetricsHandler";
96+
String ErrorLogHandler = LEFT + "errorLogHandler";
9797
String H2CUpgradeHandler = LEFT + "h2cUpgradeHandler";
9898
String H2Flush = LEFT + "h2Flush";
9999
String H2MultiplexHandler = LEFT + "h2MultiplexHandler";

reactor-netty-examples/src/main/java/reactor/netty/examples/documentation/http/server/errorLog/Application.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ public class Application {
2323
public static void main(String[] args) {
2424
DisposableServer server =
2525
HttpServer.create()
26-
.errorLog(true)
27-
.bindNow();
26+
.errorLog(true)
27+
.bindNow();
2828

2929
server.onDispose()
30-
.block();
30+
.block();
3131
}
32-
}
32+
}

reactor-netty-examples/src/main/java/reactor/netty/examples/documentation/http/server/errorLog/CustomFormatAndFilterErrorLogApplication.java

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,19 @@
1717

1818
import reactor.netty.DisposableServer;
1919
import reactor.netty.http.server.HttpServer;
20-
import reactor.netty.http.server.logging.error.DefaultErrorLog;
20+
import reactor.netty.http.server.logging.error.ErrorLog;
2121
import reactor.netty.http.server.logging.error.ErrorLogFactory;
2222

2323
public class CustomFormatAndFilterErrorLogApplication {
2424

2525
public static void main(String[] args) {
2626
DisposableServer server =
2727
HttpServer.create()
28-
.errorLog(
29-
true,
30-
ErrorLogFactory.createFilter(
31-
p -> p.cause() instanceof RuntimeException, //<1>
32-
x -> DefaultErrorLog.create("method={}, uri={}", x.httpServerInfos().method(), x.httpServerInfos().uri()) //<2>
33-
)
34-
)
35-
.bindNow();
28+
.errorLog(true, ErrorLogFactory.createFilter(p -> p.cause() instanceof RuntimeException, //<1>
29+
x -> ErrorLog.create("method={}, uri={}", x.httpServerInfos().method(), x.httpServerInfos().uri()))) //<2>
30+
.bindNow();
3631

3732
server.onDispose()
38-
.block();
33+
.block();
3934
}
40-
}
35+
}

reactor-netty-examples/src/main/java/reactor/netty/examples/documentation/http/server/errorLog/CustomLogErrorFormatApplication.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,17 @@
1717

1818
import reactor.netty.DisposableServer;
1919
import reactor.netty.http.server.HttpServer;
20-
import reactor.netty.http.server.logging.error.DefaultErrorLog;
20+
import reactor.netty.http.server.logging.error.ErrorLog;
2121

2222
public class CustomLogErrorFormatApplication {
2323

2424
public static void main(String[] args) {
2525
DisposableServer server =
2626
HttpServer.create()
27-
.errorLog(
28-
true,
29-
x -> DefaultErrorLog.create("method={}, uri={}", x.httpServerInfos().method(), x.httpServerInfos().uri())
30-
)
31-
.bindNow();
27+
.errorLog(true, x -> ErrorLog.create("method={}, uri={}", x.httpServerInfos().method(), x.httpServerInfos().uri()))
28+
.bindNow();
3229

3330
server.onDispose()
34-
.block();
31+
.block();
3532
}
36-
}
33+
}

reactor-netty-examples/src/main/java/reactor/netty/examples/documentation/http/server/errorLog/FilterLogErrorApplication.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2021-2025 VMware, Inc. or its affiliates, All Rights Reserved.
2+
* Copyright (c) 2025 VMware, Inc. or its affiliates, All Rights Reserved.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -24,13 +24,10 @@ public class FilterLogErrorApplication {
2424
public static void main(String[] args) {
2525
DisposableServer server =
2626
HttpServer.create()
27-
.errorLog(
28-
true,
29-
ErrorLogFactory.createFilter(p -> p.cause() instanceof RuntimeException)
30-
)
31-
.bindNow();
27+
.errorLog(true, ErrorLogFactory.createFilter(p -> p.cause() instanceof RuntimeException))
28+
.bindNow();
3229

3330
server.onDispose()
34-
.block();
31+
.block();
3532
}
36-
}
33+
}

reactor-netty-http/src/main/java/reactor/netty/http/server/Http3Codec.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,12 @@ final class Http3Codec extends ChannelInitializer<QuicStreamChannel> {
5555

5656
final boolean accessLogEnabled;
5757
final Function<AccessLogArgProvider, AccessLog> accessLog;
58-
final boolean errorLogEnabled;
59-
final Function<ErrorLogArgProvider, ErrorLog> errorLog;
6058
final HttpCompressionOptionsSpec compressionOptions;
6159
final BiPredicate<HttpServerRequest, HttpServerResponse> compressPredicate;
6260
final ServerCookieDecoder cookieDecoder;
6361
final ServerCookieEncoder cookieEncoder;
62+
final boolean errorLogEnabled;
63+
final Function<ErrorLogArgProvider, ErrorLog> errorLog;
6464
final HttpServerFormDecoderProvider formDecoderProvider;
6565
final BiFunction<ConnectionInfo, HttpRequest, ConnectionInfo> forwardedHeaderHandler;
6666
final HttpMessageLogFactory httpMessageLogFactory;
@@ -79,12 +79,12 @@ final class Http3Codec extends ChannelInitializer<QuicStreamChannel> {
7979
Http3Codec(
8080
boolean accessLogEnabled,
8181
@Nullable Function<AccessLogArgProvider, AccessLog> accessLog,
82-
boolean errorLogEnabled,
83-
@Nullable Function<ErrorLogArgProvider, ErrorLog> errorLog,
8482
@Nullable HttpCompressionOptionsSpec compressionOptions,
8583
@Nullable BiPredicate<HttpServerRequest, HttpServerResponse> compressPredicate,
8684
ServerCookieDecoder decoder,
8785
ServerCookieEncoder encoder,
86+
boolean errorLogEnabled,
87+
@Nullable Function<ErrorLogArgProvider, ErrorLog> errorLog,
8888
HttpServerFormDecoderProvider formDecoderProvider,
8989
@Nullable BiFunction<ConnectionInfo, HttpRequest, ConnectionInfo> forwardedHeaderHandler,
9090
HttpMessageLogFactory httpMessageLogFactory,
@@ -100,12 +100,12 @@ final class Http3Codec extends ChannelInitializer<QuicStreamChannel> {
100100
boolean validate) {
101101
this.accessLogEnabled = accessLogEnabled;
102102
this.accessLog = accessLog;
103-
this.errorLogEnabled = errorLogEnabled;
104-
this.errorLog = errorLog;
105103
this.compressionOptions = compressionOptions;
106104
this.compressPredicate = compressPredicate;
107105
this.cookieDecoder = decoder;
108106
this.cookieEncoder = encoder;
107+
this.errorLogEnabled = errorLogEnabled;
108+
this.errorLog = errorLog;
109109
this.formDecoderProvider = formDecoderProvider;
110110
this.forwardedHeaderHandler = forwardedHeaderHandler;
111111
this.httpMessageLogFactory = httpMessageLogFactory;
@@ -172,12 +172,12 @@ else if (metricsRecorder instanceof ContextAwareHttpServerMetricsRecorder) {
172172
static ChannelHandler newHttp3ServerConnectionHandler(
173173
boolean accessLogEnabled,
174174
@Nullable Function<AccessLogArgProvider, AccessLog> accessLog,
175-
boolean errorLogEnabled,
176-
@Nullable Function<ErrorLogArgProvider, ErrorLog> errorLog,
177175
@Nullable HttpCompressionOptionsSpec compressionOptions,
178176
@Nullable BiPredicate<HttpServerRequest, HttpServerResponse> compressPredicate,
179177
ServerCookieDecoder decoder,
180178
ServerCookieEncoder encoder,
179+
boolean errorLogEnabled,
180+
@Nullable Function<ErrorLogArgProvider, ErrorLog> errorLog,
181181
HttpServerFormDecoderProvider formDecoderProvider,
182182
@Nullable BiFunction<ConnectionInfo, HttpRequest, ConnectionInfo> forwardedHeaderHandler,
183183
HttpMessageLogFactory httpMessageLogFactory,
@@ -192,7 +192,7 @@ static ChannelHandler newHttp3ServerConnectionHandler(
192192
@Nullable Function<String, String> uriTagValue,
193193
boolean validate) {
194194
return new Http3ServerConnectionHandler(
195-
new Http3Codec(accessLogEnabled, accessLog, errorLogEnabled, errorLog, compressionOptions, compressPredicate, decoder, encoder,
195+
new Http3Codec(accessLogEnabled, accessLog, compressionOptions, compressPredicate, decoder, encoder, errorLogEnabled, errorLog,
196196
formDecoderProvider, forwardedHeaderHandler, httpMessageLogFactory, listener, mapHandle, methodTagValue, metricsRecorder,
197197
minCompressionSize, opsFactory, readTimeout, requestTimeout, uriTagValue, validate));
198198
}

reactor-netty-http/src/main/java/reactor/netty/http/server/HttpServer.java

Lines changed: 73 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
import reactor.netty.http.server.logging.AccessLogFactory;
5353
import reactor.netty.http.server.logging.error.ErrorLog;
5454
import reactor.netty.http.server.logging.error.ErrorLogArgProvider;
55-
import reactor.netty.http.server.logging.error.DefaultErrorLoggingEvent;
55+
import reactor.netty.http.server.logging.error.ErrorLogEvent;
5656
import reactor.netty.http.server.logging.error.ErrorLogFactory;
5757
import reactor.netty.internal.util.Metrics;
5858
import reactor.netty.tcp.SslProvider;
@@ -274,77 +274,6 @@ public final HttpServer accessLog(Function<AccessLogArgProvider, AccessLog> acce
274274
return dup;
275275
}
276276

277-
/**
278-
* Enable or disable the error log. If enabled, the default log system will be used.
279-
* <p>
280-
* Example:
281-
* <pre>
282-
* {@code
283-
* HttpServer.create()
284-
* .port(8080)
285-
* .route(r -> r.get("/hello",
286-
* (req, res) -> res.header(CONTENT_TYPE, TEXT_PLAIN)
287-
* .sendString(Mono.just("Hello World!"))))
288-
* .errorLog(true)
289-
* .bindNow()
290-
* .onDispose()
291-
* .block();
292-
* }
293-
* </pre>
294-
* <p>
295-
*
296-
* Note that this method takes precedence over the {@value reactor.netty.ReactorNetty#ERROR_LOG_ENABLED} system property.
297-
* By default, error logs are formatted as "[{datetime}] [pid {pid}] [client {remote address}] {error message}".
298-
*
299-
* @param enable enable or disable the error log
300-
* @return a new {@link HttpServer}
301-
* @since 1.2.5
302-
*/
303-
public final HttpServer errorLog(boolean enable) {
304-
HttpServer dup = duplicate();
305-
dup.configuration().errorLog = null;
306-
dup.configuration().errorLogEnabled = enable;
307-
return dup;
308-
}
309-
310-
/**
311-
* Enable or disable the error log and customize it through an {@link ErrorLogFactory}.
312-
* <p>
313-
* Example:
314-
* <pre>
315-
* {@code
316-
* HttpServer.create()
317-
* .port(8080)
318-
* .route(r -> r.get("/hello",
319-
* (req, res) -> res.header(CONTENT_TYPE, TEXT_PLAIN)
320-
* .sendString(Mono.just("Hello World!"))))
321-
* .errorLog(true, ErrorLogFactory.createFilter(
322-
* args -> args.cause() instanceof RuntimeException,
323-
* args -> ErrorLog.create("host-name={}", args.httpServerInfos().hostName())))
324-
* .bindNow()
325-
* .onDispose()
326-
* .block();
327-
* }
328-
* </pre>
329-
* <p>
330-
* The {@link ErrorLogFactory} class offers several helper methods to generate such a function,
331-
* notably if one wants to {@link ErrorLogFactory#createFilter(Predicate) filter} some exceptions out of the error log.
332-
* <p>
333-
* Note that this method takes precedence over the {@value reactor.netty.ReactorNetty#ERROR_LOG_ENABLED} system property.
334-
*
335-
* @param enable enable or disable the error log
336-
* @param errorLogFactory the {@link ErrorLogFactory} that creates an {@link ErrorLog} given an {@link ErrorLogArgProvider}
337-
* @return a new {@link HttpServer}
338-
* @since 1.2.5
339-
*/
340-
public final HttpServer errorLog(boolean enable, ErrorLogFactory errorLogFactory) {
341-
Objects.requireNonNull(errorLogFactory, "errorLogFactory");
342-
HttpServer dup = duplicate();
343-
dup.configuration().errorLog = enable ? errorLogFactory : null;
344-
dup.configuration().errorLogEnabled = enable;
345-
return dup;
346-
}
347-
348277
@Override
349278
public final HttpServer bindAddress(Supplier<? extends SocketAddress> bindAddressSupplier) {
350279
return super.bindAddress(bindAddressSupplier);
@@ -490,6 +419,77 @@ public final HttpServer cookieCodec(ServerCookieEncoder encoder, ServerCookieDec
490419
return dup;
491420
}
492421

422+
/**
423+
* Enable or disable the error log. If enabled, the default log system will be used.
424+
* <p>
425+
* Example:
426+
* <pre>
427+
* {@code
428+
* HttpServer.create()
429+
* .port(8080)
430+
* .route(r -> r.get("/hello",
431+
* (req, res) -> res.header(CONTENT_TYPE, TEXT_PLAIN)
432+
* .sendString(Mono.just("Hello World!"))))
433+
* .errorLog(true)
434+
* .bindNow()
435+
* .onDispose()
436+
* .block();
437+
* }
438+
* </pre>
439+
* <p>
440+
*
441+
* Note that this method takes precedence over the {@value reactor.netty.ReactorNetty#ERROR_LOG_ENABLED} system property.
442+
* By default, error logs are formatted as {@code [{datetime}] [pid {pid}] [client {remote address}] {error message}}.
443+
*
444+
* @param enable enable or disable the error log
445+
* @return a new {@link HttpServer}
446+
* @since 1.2.6
447+
*/
448+
public final HttpServer errorLog(boolean enable) {
449+
HttpServer dup = duplicate();
450+
dup.configuration().errorLog = null;
451+
dup.configuration().errorLogEnabled = enable;
452+
return dup;
453+
}
454+
455+
/**
456+
* Enable or disable the error log and customize it through an {@link ErrorLogFactory}.
457+
* <p>
458+
* Example:
459+
* <pre>
460+
* {@code
461+
* HttpServer.create()
462+
* .port(8080)
463+
* .route(r -> r.get("/hello",
464+
* (req, res) -> res.header(CONTENT_TYPE, TEXT_PLAIN)
465+
* .sendString(Mono.just("Hello World!"))))
466+
* .errorLog(true, ErrorLogFactory.createFilter(
467+
* args -> args.cause() instanceof RuntimeException,
468+
* args -> ErrorLog.create("host-name={}", args.httpServerInfos().hostName())))
469+
* .bindNow()
470+
* .onDispose()
471+
* .block();
472+
* }
473+
* </pre>
474+
* <p>
475+
* The {@link ErrorLogFactory} class offers several helper methods to generate such a function,
476+
* notably if one wants to {@link ErrorLogFactory#createFilter(Predicate) filter} some exceptions out of the error log.
477+
* <p>
478+
* Note that this method takes precedence over the {@value reactor.netty.ReactorNetty#ERROR_LOG_ENABLED} system property.
479+
*
480+
* @param enable enable or disable the error log
481+
* @param errorLogFactory the {@link ErrorLogFactory} that creates an {@link ErrorLog} given an {@link ErrorLogArgProvider}
482+
* @return a new {@link HttpServer}
483+
* @since 1.2.6
484+
*/
485+
public final HttpServer errorLog(boolean enable, ErrorLogFactory errorLogFactory) {
486+
Objects.requireNonNull(errorLogFactory, "errorLogFactory");
487+
HttpServer dup = duplicate();
488+
dup.configuration().errorLog = enable ? errorLogFactory : null;
489+
dup.configuration().errorLogEnabled = enable;
490+
return dup;
491+
}
492+
493493
/**
494494
* Specifies a custom request handler for deriving information about the connection.
495495
*
@@ -1325,8 +1325,7 @@ public void onStateChange(Connection connection, State newState) {
13251325
}
13261326
catch (Throwable t) {
13271327
log.error(format(connection.channel(), ""), t);
1328-
connection.channel().pipeline()
1329-
.fireUserEventTriggered(new DefaultErrorLoggingEvent(t));
1328+
connection.channel().pipeline().fireUserEventTriggered(ErrorLogEvent.create(t));
13301329
//"FutureReturnValueIgnored" this is deliberate
13311330
connection.channel()
13321331
.close();

0 commit comments

Comments
 (0)