|
38 | 38 |
|
39 | 39 | import org.slf4j.event.Level; |
40 | 40 | import org.slf4j.spi.DefaultLoggingEventBuilder; |
| 41 | +import org.slf4j.spi.FluentLogApiStub; |
41 | 42 | import org.slf4j.spi.LoggingEventBuilder; |
42 | | -import org.slf4j.spi.NOPLoggingEventBuilder; |
| 43 | +import org.slf4j.spi.NopFluentApiStub; |
| 44 | + |
| 45 | +import java.util.function.Consumer; |
43 | 46 |
|
44 | 47 | /** |
45 | 48 | * The org.slf4j.Logger interface is the main user entry point of SLF4J API. |
@@ -96,30 +99,43 @@ public interface Logger { |
96 | 99 | public String getName(); |
97 | 100 |
|
98 | 101 | /** |
99 | | - * Make a new {@link LoggingEventBuilder} instance as appropriate for this logger and the |
| 102 | + * Make a new {@link FluentLogApiStub} instance as appropriate for this logger and the |
100 | 103 | * desired {@link Level} passed as parameter. If this Logger is disabled for the given Level, then |
101 | | - * a {@link NOPLoggingEventBuilder} is returned. |
| 104 | + * a {@link NopFluentApiStub} is returned. |
102 | 105 | * |
103 | 106 | * |
104 | 107 | * @param level desired level for the event builder |
105 | 108 | * @return a new {@link LoggingEventBuilder} instance as appropriate for this logger |
106 | 109 | * @since 2.0 |
107 | 110 | */ |
108 | | - default public LoggingEventBuilder makeLoggingEventBuilder(Level level) { |
| 111 | + default public FluentLogApiStub makeLoggingEventBuilder(Level level) { |
109 | 112 | if (isEnabledForLevel(level)) { |
110 | 113 | return new DefaultLoggingEventBuilder(this, level); |
111 | 114 | } else { |
112 | | - return NOPLoggingEventBuilder.singleton(); |
| 115 | + return NopFluentApiStub.singleton(); |
113 | 116 | } |
114 | 117 | } |
115 | 118 |
|
| 119 | + default public FluentLogApiStub atLevel(Level level) { |
| 120 | + return makeLoggingEventBuilder(level); |
| 121 | + } |
| 122 | + |
| 123 | + |
116 | 124 | /** |
117 | | - * A convenient alias for {@link #makeLoggingEventBuilder}. |
118 | | - * |
| 125 | + * Use provided LoggingEventBuilder consumer to construct the logging event, and log at provided level. |
| 126 | + * |
| 127 | + * <code> |
| 128 | + * logger.atDebug(log->log.message("Temperature rise from {} to {}") |
| 129 | + * </code> |
| 130 | + * |
119 | 131 | * @since 2.0 |
120 | 132 | */ |
121 | | - default public LoggingEventBuilder atLevel(Level level) { |
122 | | - return makeLoggingEventBuilder(level); |
| 133 | + default public void atLevel(Level level, Consumer<LoggingEventBuilder> eventBuilderConsumer) { |
| 134 | + if (isEnabledForLevel(level)) { |
| 135 | + DefaultLoggingEventBuilder eventBuilder= new DefaultLoggingEventBuilder(this, level); |
| 136 | + eventBuilderConsumer.accept(eventBuilder); |
| 137 | + eventBuilder.log(); |
| 138 | + } |
123 | 139 | } |
124 | 140 |
|
125 | 141 |
|
@@ -233,16 +249,19 @@ default public boolean isEnabledForLevel(Level level) { |
233 | 249 | /** |
234 | 250 | * Entry point for fluent-logging for {@link org.slf4j.event.Level#TRACE} level. |
235 | 251 | * |
236 | | - * @return LoggingEventBuilder instance as appropriate for level TRACE |
| 252 | + * @return FluentLogApiStub instance as appropriate for level TRACE |
237 | 253 | * @since 2.0 |
238 | 254 | */ |
239 | | - default public LoggingEventBuilder atTrace() { |
| 255 | + default public FluentLogApiStub atTrace() { |
240 | 256 | if (isTraceEnabled()) { |
241 | 257 | return makeLoggingEventBuilder(TRACE); |
242 | 258 | } else { |
243 | | - return NOPLoggingEventBuilder.singleton(); |
| 259 | + return NopFluentApiStub.singleton(); |
244 | 260 | } |
245 | 261 | } |
| 262 | + default public void atTrace(Consumer<LoggingEventBuilder> eventBuilderConsumer) { |
| 263 | + this.atLevel(TRACE, eventBuilderConsumer); |
| 264 | + } |
246 | 265 |
|
247 | 266 | /** |
248 | 267 | * Log a message with the specific Marker at the TRACE level. |
@@ -429,17 +448,21 @@ default public LoggingEventBuilder atTrace() { |
429 | 448 | /** |
430 | 449 | * Entry point for fluent-logging for {@link org.slf4j.event.Level#DEBUG} level. |
431 | 450 | * |
432 | | - * @return LoggingEventBuilder instance as appropriate for level DEBUG |
| 451 | + * @return FluentLogApiStub instance as appropriate for level DEBUG |
433 | 452 | * @since 2.0 |
434 | 453 | */ |
435 | | - default public LoggingEventBuilder atDebug() { |
| 454 | + default public FluentLogApiStub atDebug() { |
436 | 455 | if (isDebugEnabled()) { |
437 | 456 | return makeLoggingEventBuilder(DEBUG); |
438 | 457 | } else { |
439 | | - return NOPLoggingEventBuilder.singleton(); |
| 458 | + return NopFluentApiStub.singleton(); |
440 | 459 | } |
441 | 460 | } |
442 | 461 |
|
| 462 | + default public void atDebug(Consumer<LoggingEventBuilder> eventBuilderConsumer) { |
| 463 | + this.atLevel(DEBUG, eventBuilderConsumer); |
| 464 | + } |
| 465 | + |
443 | 466 | /** |
444 | 467 | * Is the logger instance enabled for the INFO level? |
445 | 468 | * |
@@ -568,16 +591,19 @@ default public LoggingEventBuilder atDebug() { |
568 | 591 | /** |
569 | 592 | * Entry point for fluent-logging for {@link org.slf4j.event.Level#INFO} level. |
570 | 593 | * |
571 | | - * @return LoggingEventBuilder instance as appropriate for level INFO |
| 594 | + * @return FluentLogApiStub instance as appropriate for level INFO |
572 | 595 | * @since 2.0 |
573 | 596 | */ |
574 | | - default public LoggingEventBuilder atInfo() { |
| 597 | + default public FluentLogApiStub atInfo() { |
575 | 598 | if (isInfoEnabled()) { |
576 | 599 | return makeLoggingEventBuilder(INFO); |
577 | 600 | } else { |
578 | | - return NOPLoggingEventBuilder.singleton(); |
| 601 | + return NopFluentApiStub.singleton(); |
579 | 602 | } |
580 | 603 | } |
| 604 | + default public void atInfo(Consumer<LoggingEventBuilder> eventBuilderConsumer) { |
| 605 | + this.atLevel(INFO, eventBuilderConsumer); |
| 606 | + } |
581 | 607 |
|
582 | 608 | /** |
583 | 609 | * Is the logger instance enabled for the WARN level? |
@@ -708,16 +734,19 @@ default public LoggingEventBuilder atInfo() { |
708 | 734 | /** |
709 | 735 | * Entry point for fluent-logging for {@link org.slf4j.event.Level#WARN} level. |
710 | 736 | * |
711 | | - * @return LoggingEventBuilder instance as appropriate for level WARN |
| 737 | + * @return FluentLogApiStub instance as appropriate for level WARN |
712 | 738 | * @since 2.0 |
713 | 739 | */ |
714 | | - default public LoggingEventBuilder atWarn() { |
| 740 | + default public FluentLogApiStub atWarn() { |
715 | 741 | if (isWarnEnabled()) { |
716 | 742 | return makeLoggingEventBuilder(WARN); |
717 | 743 | } else { |
718 | | - return NOPLoggingEventBuilder.singleton(); |
| 744 | + return NopFluentApiStub.singleton(); |
719 | 745 | } |
720 | 746 | } |
| 747 | + default public void atWarn(Consumer<LoggingEventBuilder> eventBuilderConsumer) { |
| 748 | + this.atLevel(WARN, eventBuilderConsumer); |
| 749 | + } |
721 | 750 |
|
722 | 751 | /** |
723 | 752 | * Is the logger instance enabled for the ERROR level? |
@@ -849,15 +878,21 @@ default public LoggingEventBuilder atWarn() { |
849 | 878 | /** |
850 | 879 | * Entry point for fluent-logging for {@link org.slf4j.event.Level#ERROR} level. |
851 | 880 | * |
852 | | - * @return LoggingEventBuilder instance as appropriate for level ERROR |
| 881 | + * @return FluentLogApiStub instance as appropriate for level ERROR |
853 | 882 | * @since 2.0 |
854 | 883 | */ |
855 | | - default public LoggingEventBuilder atError() { |
| 884 | + default public FluentLogApiStub atError() { |
856 | 885 | if (isErrorEnabled()) { |
857 | 886 | return makeLoggingEventBuilder(ERROR); |
858 | 887 | } else { |
859 | | - return NOPLoggingEventBuilder.singleton(); |
| 888 | + return NopFluentApiStub.singleton(); |
860 | 889 | } |
861 | 890 | } |
862 | 891 |
|
| 892 | + default public void atError(Consumer<LoggingEventBuilder> eventBuilderConsumer) { |
| 893 | + this.atLevel(ERROR, eventBuilderConsumer); |
| 894 | + } |
| 895 | + |
| 896 | + |
| 897 | + |
863 | 898 | } |
0 commit comments