Skip to content

Commit 2314de9

Browse files
committed
add setMessage and log method to the fluent API
Signed-off-by: Ceki Gulcu <ceki@qos.ch>
1 parent 508a796 commit 2314de9

File tree

3 files changed

+144
-11
lines changed

3 files changed

+144
-11
lines changed

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,23 @@ public LoggingEventBuilder addArgument(Supplier<?> objectSupplier) {
8686
public void setCallerBoundary(String fqcn) {
8787
loggingEvent.setCallerBoundary(fqcn);
8888
}
89-
89+
90+
@Override
91+
public void log() {
92+
log(loggingEvent);
93+
}
94+
95+
@Override
96+
public LoggingEventBuilder setMessage(String message) {
97+
loggingEvent.setMessage(message);
98+
return this;
99+
}
100+
@Override
101+
public LoggingEventBuilder setMessage(Supplier<String> messageSupplier) {
102+
loggingEvent.setMessage(messageSupplier.get());
103+
return this;
104+
}
105+
90106
@Override
91107
public void log(String message) {
92108
loggingEvent.setMessage(message);

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

Lines changed: 100 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,34 +29,131 @@
2929
import org.slf4j.Marker;
3030

3131
/**
32-
* This is main interface in slf4j's fluent API for creating logging events.
32+
* This is the main interface in slf4j's fluent API for creating
33+
* {@link org.slf4j.event.LoggingEvent logging events}.
3334
*
3435
* @author Ceki G&uuml;lc&uuml;
3536
* @since 2.0.0
36-
*
3737
*/
3838
public interface LoggingEventBuilder {
3939

40+
/**
41+
* Set the cause for the logging event being built.
42+
* @param cause a throwable
43+
* @return a LoggingEventBuilder, usually <b>this</b>.
44+
*/
4045
LoggingEventBuilder setCause(Throwable cause);
4146

47+
/**
48+
* A {@link Marker marker} to the event being built.
49+
*
50+
* @param marker a Marker instance to add.
51+
* @return a LoggingEventBuilder, usually <b>this</b>.
52+
*/
4253
LoggingEventBuilder addMarker(Marker marker);
4354

55+
/**
56+
* Add an argument to the event being built.
57+
*
58+
* @param p an Object to add.
59+
* @return a LoggingEventBuilder, usually <b>this</b>.
60+
*/
4461
LoggingEventBuilder addArgument(Object p);
4562

63+
/**
64+
* Add an argument supplier to the event being built.
65+
*
66+
* @param objectSupplier an Object supplier to add.
67+
* @return a LoggingEventBuilder, usually <b>this</b>.
68+
*/
4669
LoggingEventBuilder addArgument(Supplier<?> objectSupplier);
4770

71+
72+
/**
73+
* Add a {@link org.slf4j.event.KeyValuePair key value pair} to the event being built.
74+
*
75+
* @param key the key of the key value pair.
76+
* @param value the value of the key value pair.
77+
* @return a LoggingEventBuilder, usually <b>this</b>.
78+
*/
4879
LoggingEventBuilder addKeyValue(String key, Object value);
4980

50-
LoggingEventBuilder addKeyValue(String key, Supplier<Object> value);
81+
/**
82+
* Add a {@link org.slf4j.event.KeyValuePair key value pair} to the event being built.
83+
*
84+
* @param key the key of the key value pair.
85+
* @param valueSupplier a supplier of a value for the key value pair.
86+
* @return a LoggingEventBuilder, usually <b>this</b>.
87+
*/
88+
LoggingEventBuilder addKeyValue(String key, Supplier<Object> valueSupplier);
89+
90+
/**
91+
* Sets the message of the logging event.
92+
*
93+
* @since 2.0.0-beta0
94+
*/
95+
LoggingEventBuilder setMessage(String message);
5196

97+
/**
98+
* Sets the message of the event via a message supplier.
99+
*
100+
* @param messageSupplier supplies a String to be used as the message for the event
101+
* @since 2.0.0-beta0
102+
*/
103+
LoggingEventBuilder setMessage(Supplier<String> messageSupplier);
104+
105+
/**
106+
* After the logging event is built, performs actual logging. This method must be called
107+
* for logging to occur.
108+
*
109+
* If the call to {@link #log()} is omitted, a {@link org.slf4j.event.LoggingEvent LoggingEvent}
110+
* will be built but no logging will occur.
111+
*
112+
* @since 2.0.0-beta0
113+
*/
114+
void log();
115+
116+
/**
117+
* Equivalent to calling {@link #setMessage(String)} followed by {@link #log()};
118+
*
119+
* @param message the message to log
120+
*/
52121
void log(String message);
53122

123+
/**
124+
* Equivalent to calling {@link #setMessage(String)} followed by {@link #addArgument(Object)}}
125+
* and then {@link #log()}
126+
*
127+
* @param message the message to log
128+
* @param arg an argument to be used with the message to log
129+
*/
54130
void log(String message, Object arg);
55131

132+
/**
133+
* Equivalent to calling {@link #setMessage(String)} followed by two calls to
134+
* {@link #addArgument(Object)} and then {@link #log()}
135+
*
136+
* @param message the message to log
137+
* @param arg0 first argument to be used with the message to log
138+
* @param arg1 second argument to be used with the message to log
139+
*/
56140
void log(String message, Object arg0, Object arg1);
57141

142+
143+
/**
144+
* Equivalent to calling {@link #setMessage(String)} followed by zero or more calls to
145+
* {@link #addArgument(Object)} (depending on the size of args array) and then {@link #log()}
146+
*
147+
* @param message the message to log
148+
* @param args a list (actually an array) of arguments to be used with the message to log
149+
*/
58150
void log(String message, Object... args);
59151

152+
/**
153+
* Equivalent to calling {@link #setMessage(Supplier)} followed by {@link #log()}
154+
*
155+
* @param messageSupplier a Supplier returning a message of type String
156+
*/
60157
void log(Supplier<String> messageSupplier);
61158

62159
}

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

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,15 @@
33
import java.util.function.Supplier;
44

55
import org.slf4j.Marker;
6+
import org.slf4j.event.Level;
67

78
/**
8-
* A no-operation implementation of {@link LoggingEventBuilder}.
9-
* As the name indicates, this implementation does nothing or alternatively returns
10-
* a singleton, i.e. the unique instance of this class.
11-
*
9+
* <p>A no-operation implementation of {@link LoggingEventBuilder}.</p>
10+
*
11+
* <p>As the name indicates, the method in this class do nothing, except when a return value is expected
12+
* in which case a singleton, i.e. the unique instance of this class is returned.
13+
* </p
14+
*
1215
* @author Ceki G&uuml;lc&uuml;
1316
* @since 2.0.0
1417
*
@@ -17,10 +20,15 @@ public class NOPLoggingEventBuilder implements LoggingEventBuilder {
1720

1821
static final NOPLoggingEventBuilder SINGLETON = new NOPLoggingEventBuilder();
1922

20-
2123
private NOPLoggingEventBuilder() {
2224
}
23-
25+
26+
/**
27+
* <p>Returns the singleton instance of this class.
28+
* Used by {@link org.slf4j.Logger#makeLoggingEventBuilder(Level) makeLoggingEventBuilder(Level)}.</p>
29+
*
30+
* @return the singleton instance of this class
31+
*/
2432
public static LoggingEventBuilder singleton() {
2533
return SINGLETON;
2634
}
@@ -56,8 +64,20 @@ public LoggingEventBuilder setCause(Throwable cause) {
5664
}
5765

5866
@Override
59-
public void log(String message) {
67+
public void log() {
68+
}
69+
70+
@Override
71+
public LoggingEventBuilder setMessage(String message) {
72+
return this;
73+
}
74+
@Override
75+
public LoggingEventBuilder setMessage(Supplier<String> messageSupplier) {
76+
return this;
77+
}
6078

79+
@Override
80+
public void log(String message) {
6181
}
6282

6383
@Override

0 commit comments

Comments
 (0)