Skip to content

Commit 154b4e4

Browse files
committed
issues/375 add arg(primitiveType) methods in LoggingEventBuilder interface
Signed-off-by: Ceki Gulcu <[email protected]>
1 parent 93722a1 commit 154b4e4

File tree

4 files changed

+268
-11
lines changed

4 files changed

+268
-11
lines changed

slf4j-api/src/main/java/org/slf4j/Logger.java

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,9 @@ default public LoggingEventBuilder makeLoggingEventBuilder(Level level) {
116116
}
117117

118118
/**
119-
* Make a new {@link LoggingEventBuilder} instance as appropriate for this logger and the
119+
* <p>Make a new {@link LoggingEventBuilder} instance as appropriate for this logger and the
120120
* desired {@link Level} passed as parameter. If this Logger is disabled for the given Level, then
121-
* a {@link NOPLoggingEventBuilder} is returned.
121+
* a {@link NOPLoggingEventBuilder} is returned. This is the main optimization in the fluent API.</p>
122122
*
123123
*
124124
* @param level desired level for the event builder
@@ -243,8 +243,11 @@ default public boolean isEnabledForLevel(Level level) {
243243
public boolean isTraceEnabled(Marker marker);
244244

245245
/**
246-
* Entry point for fluent-logging for {@link org.slf4j.event.Level#TRACE} level.
247-
*
246+
* Entry point for fluent-logging for {@link org.slf4j.event.Level#TRACE} level.
247+
*
248+
* <p>If this logger is disabled for the TRACE level, then a {@link NOPLoggingEventBuilder} instance is returned.
249+
* As the name indicates, this builder does not perform any operations. This is the main optimization in the fluent API.</p>
250+
*
248251
* @return LoggingEventBuilder instance as appropriate for level TRACE
249252
* @since 2.0
250253
*/
@@ -441,7 +444,10 @@ default public LoggingEventBuilder atTrace() {
441444

442445
/**
443446
* Entry point for fluent-logging for {@link org.slf4j.event.Level#DEBUG} level.
444-
*
447+
*
448+
* <p>If this logger is disabled for the DEBUG level, then a {@link NOPLoggingEventBuilder} instance is returned.
449+
* As the name indicates, this builder does not perform any operations. This is the main optimization in the fluent API.</p>
450+
*
445451
* @return LoggingEventBuilder instance as appropriate for level DEBUG
446452
* @since 2.0
447453
*/
@@ -582,7 +588,10 @@ default public LoggingEventBuilder atDebug() {
582588

583589
/**
584590
* Entry point for fluent-logging for {@link org.slf4j.event.Level#INFO} level.
585-
*
591+
*
592+
* <p>If this logger is disabled for the INFO level, then a {@link NOPLoggingEventBuilder} instance is returned.
593+
* As the name indicates, this builder does not perform any operations. This is the main optimization in the fluent API.</p>
594+
586595
* @return LoggingEventBuilder instance as appropriate for level INFO
587596
* @since 2.0
588597
*/
@@ -723,7 +732,10 @@ default public LoggingEventBuilder atInfo() {
723732

724733
/**
725734
* Entry point for fluent-logging for {@link org.slf4j.event.Level#WARN} level.
726-
*
735+
*
736+
* <p>If this logger is disabled for the WARN level, then a {@link NOPLoggingEventBuilder} instance is returned.
737+
* As the name indicates, this builder does not perform any operations. This is the main optimization in the fluent API.</p>
738+
*
727739
* @return LoggingEventBuilder instance as appropriate for level WARN
728740
* @since 2.0
729741
*/
@@ -865,7 +877,10 @@ default public LoggingEventBuilder atWarn() {
865877

866878
/**
867879
* Entry point for fluent-logging for {@link org.slf4j.event.Level#ERROR} level.
868-
*
880+
*
881+
* <p>If this logger is disabled for the ERROR level, then a {@link NOPLoggingEventBuilder} instance is returned.
882+
* As the name indicates, this builder does not perform any operations.</p>
883+
*
869884
* @return LoggingEventBuilder instance as appropriate for level ERROR
870885
* @since 2.0
871886
*/

slf4j-api/src/main/java/org/slf4j/spi/DefaultLoggingEventBuilder.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,11 @@
3434
import org.slf4j.event.LoggingEvent;
3535

3636
/**
37-
* Default implementation of {@link LoggingEventBuilder}
37+
* Default implementation of {@link LoggingEventBuilder}.
38+
*
39+
* <p>It is assumed that when </p>
40+
*
41+
* @since 2.0.0
3842
*/
3943
public class DefaultLoggingEventBuilder implements LoggingEventBuilder, CallerBoundaryAware {
4044

@@ -76,6 +80,7 @@ public LoggingEventBuilder addArgument(Object p) {
7680
return this;
7781
}
7882

83+
7984
@Override
8085
public LoggingEventBuilder addArgument(Supplier<?> objectSupplier) {
8186
loggingEvent.addArgument(objectSupplier.get());

slf4j-api/src/main/java/org/slf4j/spi/LoggingEventBuilder.java

Lines changed: 140 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ public interface LoggingEventBuilder {
5858

5959
/**
6060
* Add an argument to the event being built.
61+
* Synonymous with {@link #arg(Object)}.
6162
*
6263
* @param p an Object to add.
6364
* @return a LoggingEventBuilder, usually <b>this</b>.
@@ -66,15 +67,153 @@ public interface LoggingEventBuilder {
6667
LoggingEventBuilder addArgument(Object p);
6768

6869
/**
69-
* Add an argument supplier to the event being built.
70+
* Add an argument to the event being built.
71+
* Synonymous with {@link #addArgument(Object)}.
7072
*
73+
* @param p an Object to add.
74+
* @return a LoggingEventBuilder, usually <b>this</b>.
75+
* @since 2.1.0
76+
*/
77+
@CheckReturnValue
78+
default LoggingEventBuilder arg(Object p) {
79+
return addArgument(p);
80+
}
81+
82+
/**
83+
* <p>Add an argument supplier to the event being built. Synonymous with {@link #arg(Supplier)}.
84+
* </p>
7185
* @param objectSupplier an Object supplier to add.
7286
* @return a LoggingEventBuilder, usually <b>this</b>.
7387
*/
7488
@CheckReturnValue
7589
LoggingEventBuilder addArgument(Supplier<?> objectSupplier);
7690

7791

92+
/**
93+
* <p>Add an argument supplier to the event being built. Synonymous with {@link #addArgument(Supplier)}.
94+
* </p>
95+
*
96+
* @param objectSupplier an Object supplier to add.
97+
* @return a LoggingEventBuilder, usually <b>this</b>.
98+
* @since 2.1.0
99+
*/
100+
@CheckReturnValue
101+
default LoggingEventBuilder arg(Supplier<?> objectSupplier) {
102+
return addArgument(objectSupplier);
103+
}
104+
105+
/**
106+
* Add a value of type <code>boolean</code> to the event being built.
107+
*
108+
* <p>The default implementation simply casts to <code>Boolean</code>. However, However, the NOP implementation, i.e.
109+
* {@link NOPLoggingEventBuilder}, skips the cast.</p>
110+
*
111+
* @param b a value of type <code>boolean</code> value to add.
112+
* @return a LoggingEventBuilder, usually <b>this</b>.
113+
* @since 2.1.0
114+
*/
115+
default public LoggingEventBuilder arg(boolean b) {
116+
return addArgument((Boolean) b);
117+
}
118+
119+
/**
120+
* Add a value of type <code>char</code> to the event being built.
121+
*
122+
* <p>The default implementation simply casts to <code>Character</code>. However, the NOP implementation, i.e.
123+
* {@link NOPLoggingEventBuilder}, skips the cast.</p>
124+
*
125+
* @param c a value of type <code>char</code> value to add.
126+
* @return a LoggingEventBuilder, usually <b>this</b>.
127+
* @since 2.1.0
128+
*/
129+
default public LoggingEventBuilder arg(char c) {
130+
return addArgument((Character) c);
131+
}
132+
133+
/**
134+
* Add a value of type <code>byte</code> to the event being built.
135+
*
136+
* <p>The default implementation simply casts to <code>Byte</code>. However, the NOP implementation, i.e.
137+
* {@link NOPLoggingEventBuilder}, skips the cast.</p>
138+
*
139+
* @param b a value of type <code>byte</code> value to add.
140+
* @return a LoggingEventBuilder, usually <b>this</b>.
141+
* @since 2.1.0
142+
*/
143+
default public LoggingEventBuilder arg(byte b) {
144+
return addArgument((Byte) b);
145+
}
146+
147+
/**
148+
* Add a value of type <code>short</code> to the event being built.
149+
*
150+
* <p>The default implementation simply casts to <code>Short</code>. However, the NOP implementation, i.e.
151+
* {@link NOPLoggingEventBuilder}, skips the cast.</p>
152+
*
153+
* @param s a value of type <code>short</code> value to add.
154+
* @return a LoggingEventBuilder, usually <b>this</b>.
155+
* @since 2.1.0
156+
*/
157+
default public LoggingEventBuilder arg(short s) {
158+
return addArgument((Short) s);
159+
}
160+
161+
/**
162+
* Add a value of type <code>int</code> to the event being built.
163+
*
164+
* <p>The default implementation simply casts to <code>Integer</code>. However, the NOP implementation, i.e.
165+
* {@link NOPLoggingEventBuilder}, skips the cast.</p>
166+
*
167+
* @param i a value of type <code>int</code> value to add.
168+
* @return a LoggingEventBuilder, usually <b>this</b>.
169+
* @since 2.1.0
170+
*/
171+
default public LoggingEventBuilder arg(int i) {
172+
return addArgument((Integer) i);
173+
}
174+
175+
/**
176+
* Add a value of type <code>long</code> to the event being built.
177+
*
178+
* <p>The default implementation simply casts to <code>Long</code>. However, the NOP implementation, i.e.
179+
* {@link NOPLoggingEventBuilder}, skips the cast.</p>
180+
*
181+
* @param l a value of type <code>long</code> value to add.
182+
* @return a LoggingEventBuilder, usually <b>this</b>.
183+
* @since 2.1.0
184+
*/
185+
default public LoggingEventBuilder arg(long l) {
186+
return addArgument((Long) l);
187+
}
188+
189+
/**
190+
* Add a value of type <code>float</code> to the event being built.
191+
*
192+
* <p>The default implementation simply casts to <code>Float</code>. However, the NOP implementation, i.e.
193+
* {@link NOPLoggingEventBuilder}, skips the cast.</p>
194+
*
195+
* @param f a value of type <code>float</code> value to add.
196+
* @return a LoggingEventBuilder, usually <b>this</b>.
197+
* @since 2.1.0
198+
*/
199+
default public LoggingEventBuilder arg(float f) {
200+
return addArgument((Float) f);
201+
}
202+
203+
/**
204+
* Add a value of type <code>double</code> to the event being built.
205+
*
206+
* <p>The default implementation simply casts to <code>Double</code>. However, the NOP implementation skips the cast.</p>
207+
*
208+
* @param d a value of type <code>double</code> value to add.
209+
* @return a LoggingEventBuilder, usually <b>this</b>.
210+
* @since 2.1.0
211+
*/
212+
default LoggingEventBuilder arg(double d) {
213+
return arg((Double) d);
214+
}
215+
216+
78217
/**
79218
* Add a {@link org.slf4j.event.KeyValuePair key value pair} to the event being built.
80219
*

slf4j-api/src/main/java/org/slf4j/spi/NOPLoggingEventBuilder.java

Lines changed: 99 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,22 @@
22

33
import java.util.function.Supplier;
44

5+
import org.slf4j.Logger;
56
import org.slf4j.Marker;
67
import org.slf4j.event.Level;
78

89
/**
910
* <p>A no-operation implementation of {@link LoggingEventBuilder}.</p>
1011
*
11-
* <p>As the name indicates, the method in this class do nothing, except when a return value is expected
12+
* <p>As the name indicates, the methods in this class do nothing, except when a return value is expected
1213
* in which case a singleton, i.e. the unique instance of this class is returned.
1314
* </p
1415
*
16+
* <p>The default implementations of {@link Logger#atTrace()}, {@link Logger#atDebug()} , {@link Logger#atInfo()},
17+
* {@link Logger#atWarn()} and {@link Logger#atError()}, return an instance of {@link NOPLoggingEventBuilder}
18+
* when the relevant level is disabled for current logger. This is the core optimization in the fluent API.</p>
19+
*
20+
*
1521
* @author Ceki G&uuml;lc&uuml;
1622
* @since 2.0.0
1723
*
@@ -48,6 +54,98 @@ public LoggingEventBuilder addArgument(Supplier<?> objectSupplier) {
4854
return singleton();
4955
}
5056

57+
/**
58+
* Add a value of type <code>boolean</code> to the event being built.
59+
*
60+
* <p>The default implementation simply casts to <code>Boolean</code>. However, the NOP implementation skips the cast.</p>
61+
*
62+
* @param b a value of type <code>boolean</code> value to add.
63+
* @return a LoggingEventBuilder, usually <b>this</b>.
64+
* @since 2.1.0
65+
*/
66+
public LoggingEventBuilder arg(boolean b) {
67+
return singleton();
68+
}
69+
70+
/**
71+
* Add a value of type <code>char</code> to the event being built.
72+
*
73+
* <p>The default implementation simply casts to <code>Character</code>. However, the NOP implementation skips the cast.</p>
74+
*
75+
* @param c a value of type <code>char</code> value to add.
76+
* @return a LoggingEventBuilder, usually <b>this</b>.
77+
* @since 2.1.0
78+
*/
79+
public LoggingEventBuilder arg(char c) {
80+
return singleton();
81+
}
82+
83+
/**
84+
* Add a value of type <code>byte</code> to the event being built.
85+
*
86+
* <p>The default implementation simply casts to <code>Byte</code>. However, the NOP implementation skips the cast.</p>
87+
*
88+
* @param b a value of type <code>byte</code> value to add.
89+
* @return a LoggingEventBuilder, usually <b>this</b>.
90+
* @since 2.1.0
91+
*/
92+
public LoggingEventBuilder arg(byte b) {
93+
return singleton();
94+
}
95+
96+
/**
97+
* Add a value of type <code>short</code> to the event being built.
98+
*
99+
* <p>The default implementation simply casts to <code>Short</code>. However, the NOP implementation skips the cast.</p>
100+
*
101+
* @param s a value of type <code>short</code> value to add.
102+
* @return a LoggingEventBuilder, usually <b>this</b>.
103+
* @since 2.1.0
104+
*/
105+
public LoggingEventBuilder arg(short s) {
106+
return singleton();
107+
}
108+
109+
/**
110+
* Add a value of type <code>int</code> to the event being built.
111+
*
112+
* <p>The default implementation simply casts to <code>Integer</code>. However, the NOP implementation skips the cast.</p>
113+
*
114+
* @param i a value of type <code>int</code> value to add.
115+
* @return a LoggingEventBuilder, usually <b>this</b>.
116+
* @since 2.1.0
117+
*/
118+
public LoggingEventBuilder arg(int i) {
119+
return singleton();
120+
}
121+
122+
/**
123+
* Add a value of type <code>long</code> to the event being built.
124+
*
125+
* <p>The default implementation simply casts to <code>Long</code>. However, the NOP implementation skips the cast.</p>
126+
*
127+
* @param l a value of type <code>long</code> value to add.
128+
* @return a LoggingEventBuilder, usually <b>this</b>.
129+
* @since 2.1.0
130+
*/
131+
public LoggingEventBuilder arg(long l) {
132+
return singleton();
133+
}
134+
135+
/**
136+
* Add a value of type <code>float</code> to the event being built.
137+
*
138+
* <p>The default implementation simply casts to <code>Float</code>. However, the NOP implementation skips the cast.</p>
139+
*
140+
* @param f a value of type <code>float</code> value to add.
141+
* @return a LoggingEventBuilder, usually <b>this</b>.
142+
* @since 2.1.0
143+
*/
144+
public LoggingEventBuilder arg(float f) {
145+
return singleton();
146+
}
147+
148+
51149
@Override
52150
public LoggingEventBuilder addKeyValue(String key, Object value) {
53151
return singleton();

0 commit comments

Comments
 (0)