Skip to content

Commit

Permalink
Abandon FSM logging, use slf4j
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinherron committed Aug 20, 2024
1 parent a1717a1 commit fb599cd
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 32 deletions.
19 changes: 18 additions & 1 deletion src/main/java/com/digitalpetri/netty/fsm/ChannelFsmConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

package com.digitalpetri.netty.fsm;

import java.util.Map;
import java.util.concurrent.Executor;

public interface ChannelFsmConfig {
Expand Down Expand Up @@ -79,12 +80,28 @@ public interface ChannelFsmConfig {
*/
Scheduler getScheduler();

/**
* Get the logger name the FSM should use.
*
* @return the logger name the FSM should use.
*/
String getLoggerName();

/**
* Get the logging context Map a {@link ChannelFsm} instance will use.
*
* <p>Keys and values in the Map will be set on the SLF4J {@link org.slf4j.MDC} when logging.
*
* @return the logging context Map a {@link ChannelFsm} instance will use.
*/
Map<String, String> getLoggingContext();

/**
* Get the user-configurable context associated with this ChannelFsm.
*
* @return the user-configurable context associated with this ChannelFsm.
*/
Object getContext();
Object getUserContext();

/**
* Create a new {@link ChannelFsmConfigBuilder}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@

package com.digitalpetri.netty.fsm;

import java.util.Collections;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
Expand All @@ -26,7 +29,9 @@ public class ChannelFsmConfigBuilder {
private ChannelActions channelActions;
private Executor executor;
private Scheduler scheduler;
private Object context;
private String loggerName;
private Map<String, String> loggingContext = Collections.emptyMap();
private Object userContext;

/**
* @param lazy {@code true} if the ChannelFsm should be lazy,
Expand Down Expand Up @@ -111,12 +116,38 @@ public ChannelFsmConfigBuilder setScheduler(ScheduledExecutorService scheduledEx
}

/**
* @param context the user-configurable context associated with this ChannelFsm.
* @param loggerName the logger name the FSM should use.
* @return this {@link ChannelFsmConfigBuilder}.
* @see ChannelFsmConfig#getContext()
* @see ChannelFsmConfig#getLoggerName()
*/
public ChannelFsmConfigBuilder setContext(Object context) {
this.context = context;
public ChannelFsmConfigBuilder setLoggerName(String loggerName) {
this.loggerName = loggerName;
return this;
}

/**
* Set the logging context Map a {@link ChannelFsm} instance will use.
*
* <p>Keys and values in the Map will be set on the SLF4J {@link org.slf4j.MDC} when logging.
*
* <p>This method makes a defensive copy of {@code loggingContext}.
*
* @param loggingContext the logging context Map a {@link ChannelFsm} instance will use.
* @return this {@link ChannelFsmConfigBuilder}
* @see ChannelFsmConfig#getLoggingContext()
*/
public ChannelFsmConfigBuilder setLoggingContext(Map<String, String> loggingContext) {
this.loggingContext = new ConcurrentHashMap<>(loggingContext);
return this;
}

/**
* @param userContext the user-configurable context associated with this ChannelFsm.
* @return this {@link ChannelFsmConfigBuilder}.
* @see ChannelFsmConfig#getUserContext()
*/
public ChannelFsmConfigBuilder setUserContext(Object userContext) {
this.userContext = userContext;
return this;
}

Expand All @@ -133,6 +164,9 @@ public ChannelFsmConfig build() {
if (scheduler == null) {
scheduler = SharedScheduler.INSTANCE;
}
if (loggerName == null) {
loggerName = ChannelFsm.class.getName();
}

return new ChannelFsmConfigImpl(
lazy,
Expand All @@ -142,7 +176,9 @@ public ChannelFsmConfig build() {
channelActions,
executor,
scheduler,
context
loggerName,
loggingContext,
userContext
);
}

Expand All @@ -166,7 +202,9 @@ private static class ChannelFsmConfigImpl implements ChannelFsmConfig {
private final ChannelActions channelActions;
private final Executor executor;
private final Scheduler scheduler;
private final Object context;
private final String loggerName;
private final Map<String, String> loggingContext;
private final Object userContext;

ChannelFsmConfigImpl(
boolean lazy,
Expand All @@ -176,7 +214,9 @@ private static class ChannelFsmConfigImpl implements ChannelFsmConfig {
ChannelActions channelActions,
Executor executor,
Scheduler scheduler,
Object context
String loggerName,
Map<String, String> loggingContext,
Object userContext
) {

this.lazy = lazy;
Expand All @@ -186,7 +226,9 @@ private static class ChannelFsmConfigImpl implements ChannelFsmConfig {
this.channelActions = channelActions;
this.executor = executor;
this.scheduler = scheduler;
this.context = context;
this.loggerName = loggerName;
this.loggingContext = loggingContext;
this.userContext = userContext;
}

@Override
Expand Down Expand Up @@ -225,8 +267,18 @@ public Scheduler getScheduler() {
}

@Override
public Object getContext() {
return context;
public String getLoggerName() {
return loggerName;
}

@Override
public Map<String, String> getLoggingContext() {
return loggingContext;
}

@Override
public Object getUserContext() {
return userContext;
}

}
Expand Down
58 changes: 38 additions & 20 deletions src/main/java/com/digitalpetri/netty/fsm/ChannelFsmFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import static com.digitalpetri.netty.fsm.CompletionBuilders.completeAsync;

import com.digitalpetri.fsm.FsmContext;
import com.digitalpetri.fsm.Log;
import com.digitalpetri.fsm.dsl.ActionContext;
import com.digitalpetri.fsm.dsl.FsmBuilder;
import com.digitalpetri.netty.fsm.ChannelFsm.ConnectFuture;
Expand All @@ -31,6 +30,9 @@
import io.netty.handler.timeout.IdleStateHandler;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;

public class ChannelFsmFactory {

Expand All @@ -50,7 +52,12 @@ public ChannelFsm newChannelFsm() {
}

ChannelFsm newChannelFsm(State initialState) {
var builder = new FsmBuilder<State, Event>(config.getContext(), config.getExecutor());
var builder = new FsmBuilder<State, Event>(
config.getLoggerName(),
config.getLoggingContext(),
config.getExecutor(),
config.getUserContext()
);

configureChannelFsm(builder, config);

Expand Down Expand Up @@ -197,6 +204,8 @@ private static void configureConnectedState(
ChannelFsmConfig config
) {

Logger logger = LoggerFactory.getLogger(config.getLoggerName());

fb.when(State.Connected)
.on(Event.Disconnect.class)
.transitionTo(State.Disconnecting);
Expand Down Expand Up @@ -233,12 +242,16 @@ public void channelInactive(
ChannelHandlerContext channelContext
) throws Exception {

Log.debug(
config.getContext(),
"channelInactive() local=%s, remote=%s",
channelContext.channel().localAddress(),
channelContext.channel().remoteAddress()
);
config.getLoggingContext().forEach(MDC::put);
try {
logger.debug(
"channelInactive() local={}, remote={}",
channelContext.channel().localAddress(),
channelContext.channel().remoteAddress()
);
} finally {
config.getLoggingContext().keySet().forEach(MDC::remove);
}

if (ctx.currentState() == State.Connected) {
ctx.fireEvent(new Event.ChannelInactive());
Expand All @@ -253,13 +266,17 @@ public void exceptionCaught(
Throwable cause
) {

Log.debug(
config.getContext(),
"exceptionCaught() local=%s, remote=%s%n%s",
channelContext.channel().localAddress(),
channelContext.channel().remoteAddress(),
cause
);
config.getLoggingContext().forEach(MDC::put);
try {
logger.debug(
"exceptionCaught() local={}, remote={}",
channelContext.channel().localAddress(),
channelContext.channel().remoteAddress(),
cause
);
} finally {
config.getLoggingContext().keySet().forEach(MDC::remove);
}

if (ctx.currentState() == State.Connected) {
channelContext.close();
Expand All @@ -276,11 +293,12 @@ public void userEventTriggered(
IdleState idleState = ((IdleStateEvent) evt).state();

if (idleState == IdleState.READER_IDLE) {
Log.debug(
config.getContext(),
"channel idle, maxIdleSeconds=%s",
config.getMaxIdleSeconds()
);
config.getLoggingContext().forEach(MDC::put);
try {
logger.debug("channel idle, maxIdleSeconds={}", config.getMaxIdleSeconds());
} finally {
config.getLoggingContext().keySet().forEach(MDC::remove);
}

ctx.fireEvent(new Event.ChannelIdle());
}
Expand Down

0 comments on commit fb599cd

Please sign in to comment.