Skip to content

Commit 71b4349

Browse files
committed
Abandon custom logging, use slf4j
1 parent 768be5b commit 71b4349

File tree

6 files changed

+93
-199
lines changed

6 files changed

+93
-199
lines changed

pom.xml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>com.digitalpetri.fsm</groupId>
88
<artifactId>strict-machine</artifactId>
9-
<version>1.0.0-RC1</version>
9+
<version>1.0.0-SNAPSHOT</version>
1010

1111
<name>Strict Machine</name>
1212
<description>
@@ -44,6 +44,9 @@
4444
<maven.compiler.release>11</maven.compiler.release>
4545
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
4646

47+
<!-- Dependencies -->
48+
<slf4j.version>2.0.16</slf4j.version>
49+
4750
<!-- Test Dependencies -->
4851
<junit.version>5.10.2</junit.version>
4952

@@ -54,6 +57,12 @@
5457
</properties>
5558

5659
<dependencies>
60+
<dependency>
61+
<groupId>org.slf4j</groupId>
62+
<artifactId>slf4j-api</artifactId>
63+
<version>2.0.16</version>
64+
</dependency>
65+
5766
<dependency>
5867
<groupId>org.junit.jupiter</groupId>
5968
<artifactId>junit-jupiter-api</artifactId>

src/main/java/com/digitalpetri/fsm/FsmContext.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public interface FsmContext<S, E> {
7676
*
7777
* @return the user-configurable context associated with this FSM instance.
7878
*/
79-
Object getContext();
79+
Object getUserContext();
8080

8181
final class Key<T> {
8282

src/main/java/com/digitalpetri/fsm/FsmLogging.java

Lines changed: 0 additions & 64 deletions
This file was deleted.

src/main/java/com/digitalpetri/fsm/Log.java

Lines changed: 0 additions & 104 deletions
This file was deleted.

src/main/java/com/digitalpetri/fsm/StrictMachine.java

Lines changed: 58 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
package com.digitalpetri.fsm;
1212

13-
import com.digitalpetri.fsm.FsmLogging.Level;
1413
import com.digitalpetri.fsm.dsl.ActionContext;
1514
import com.digitalpetri.fsm.dsl.ActionProxy;
1615
import com.digitalpetri.fsm.dsl.Transition;
@@ -26,6 +25,9 @@
2625
import java.util.concurrent.locks.ReentrantReadWriteLock;
2726
import java.util.function.Consumer;
2827
import java.util.function.Function;
28+
import org.slf4j.Logger;
29+
import org.slf4j.LoggerFactory;
30+
import org.slf4j.MDC;
2931

3032
public class StrictMachine<S, E> implements Fsm<S, E> {
3133

@@ -38,23 +40,29 @@ public class StrictMachine<S, E> implements Fsm<S, E> {
3840
private final Map<FsmContext.Key<?>, Object> contextValues = new ConcurrentHashMap<>();
3941
private final AtomicReference<S> state = new AtomicReference<>();
4042

41-
private final Object context;
43+
private final Logger logger;
44+
private final Map<String, String> mdc;
4245
private final Executor executor;
46+
private final Object userContext;
4347
private final ActionProxy<S, E> actionProxy;
4448
private final List<Transition<S, E>> transitions;
4549
private final List<TransitionAction<S, E>> transitionActions;
4650

4751
public StrictMachine(
48-
Object context,
52+
String loggerName,
53+
Map<String, String> mdc,
4954
Executor executor,
55+
Object userContext,
5056
ActionProxy<S, E> actionProxy,
5157
S initialState,
5258
List<Transition<S, E>> transitions,
5359
List<TransitionAction<S, E>> transitionActions
5460
) {
5561

56-
this.context = context;
62+
this.logger = LoggerFactory.getLogger(loggerName);
63+
this.mdc = mdc;
5764
this.executor = executor;
65+
this.userContext = userContext;
5866
this.actionProxy = actionProxy;
5967
this.transitions = transitions;
6068
this.transitionActions = transitionActions;
@@ -171,14 +179,18 @@ public void run() {
171179

172180
state.set(nextState);
173181

174-
if (Log.isLevelEnabled(Level.DEBUG)) {
175-
Log.debug(
176-
context,
177-
"%s x %s = %s",
178-
padRight(String.format("S(%s)", currState)),
179-
padRight(String.format("E(%s)", event)),
180-
padRight(String.format("S'(%s)", nextState))
181-
);
182+
if (logger.isDebugEnabled()) {
183+
mdc.forEach(MDC::put);
184+
try {
185+
logger.debug(
186+
"{} x {} = {}",
187+
padRight(String.format("S(%s)", currState)),
188+
padRight(String.format("E(%s)", event)),
189+
padRight(String.format("S'(%s)", nextState))
190+
);
191+
} finally {
192+
mdc.keySet().forEach(MDC::remove);
193+
}
182194
}
183195

184196
var actionContext = new ActionContextImpl(
@@ -195,27 +207,49 @@ public void run() {
195207
}
196208
}
197209

198-
Log.trace(context, "found %d matching TransitionActions", matchingActions.size());
210+
if (logger.isTraceEnabled()) {
211+
mdc.forEach(MDC::put);
212+
try {
213+
logger.trace("found {} matching TransitionActions", matchingActions.size());
214+
} finally {
215+
mdc.keySet().forEach(MDC::remove);
216+
}
217+
}
199218

200219
matchingActions.forEach(transitionAction -> {
201220
try {
202221
if (actionProxy == null) {
203-
Log.trace(context, "executing TransitionAction: %s", transitionAction);
222+
if (logger.isTraceEnabled()) {
223+
mdc.forEach(MDC::put);
224+
try {
225+
logger.trace("executing TransitionAction: {}", transitionAction);
226+
} finally {
227+
mdc.keySet().forEach(MDC::remove);
228+
}
229+
}
204230

205231
transitionAction.execute(actionContext);
206232
} else {
207-
Log.trace(
208-
context,
209-
"executing (via proxy) TransitionAction: %s", transitionAction
210-
);
233+
if (logger.isTraceEnabled()) {
234+
mdc.forEach(MDC::put);
235+
try {
236+
logger.trace("executing (via proxy) TransitionAction: {}", transitionAction);
237+
} finally {
238+
mdc.keySet().forEach(MDC::remove);
239+
}
240+
}
211241

212242
actionProxy.execute(actionContext, transitionAction::execute);
213243
}
214244
} catch (Throwable ex) {
215-
Log.warn(
216-
context,
217-
"Uncaught Throwable executing TransitionAction: %s\n%s", transitionAction, ex
218-
);
245+
246+
mdc.forEach(MDC::put);
247+
try {
248+
logger.warn("uncaught Throwable executing TransitionAction: {}",
249+
transitionAction, ex);
250+
} finally {
251+
mdc.keySet().forEach(MDC::remove);
252+
}
219253
}
220254
});
221255

@@ -318,8 +352,8 @@ public void set(Key<?> key, Object value) {
318352
}
319353

320354
@Override
321-
public Object getContext() {
322-
return context;
355+
public Object getUserContext() {
356+
return userContext;
323357
}
324358

325359
}

0 commit comments

Comments
 (0)