Created
September 11, 2015 17:07
-
-
Save fastnsilver/82f242dd5b42bfd118e8 to your computer and use it in GitHub Desktop.
Spring Integration Utility for logging messages
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.springframework.integration.channel.DirectChannel; | |
import org.springframework.integration.channel.interceptor.WireTap; | |
import org.springframework.integration.handler.LoggingHandler; | |
import org.springframework.messaging.MessageChannel; | |
public final class LoggingUtils { | |
private static final String DEFAULT_MESSAGE = "'Message: [' + #root + ']'"; | |
private LoggingUtils() { | |
// Prevent Instantiation of util class | |
} | |
public static LoggingHandler createLoggingMessageHandler(String name) { | |
return createLoggingMessageHandler(name, LoggingHandler.Level.INFO); | |
} | |
public static LoggingHandler createLoggingMessageHandler(String name, LoggingHandler.Level level) { | |
return createLoggingMessageHandler(name, level, DEFAULT_MESSAGE); | |
} | |
public static LoggingHandler createLoggingMessageHandler(String name, LoggingHandler.Level level, | |
String expression) { | |
LoggingHandler loggingHandler = new LoggingHandler(level.name()); | |
String expressionString = "'Event: [" + name + "] - ' + " + expression; | |
loggingHandler.setExpression(expressionString); | |
return loggingHandler; | |
} | |
public static MessageChannel createLoggingMessageChannel(String name) { | |
return createLoggingMessageChannel(name, LoggingHandler.Level.INFO); | |
} | |
public static MessageChannel createLoggingMessageChannel(String name, LoggingHandler.Level level) { | |
return createLoggingMessageChannel(name, level, DEFAULT_MESSAGE); | |
} | |
public static MessageChannel createLoggingMessageChannel(String name, LoggingHandler.Level level, | |
String expression) { | |
DirectChannel wiretapChannel = new DirectChannel(); | |
wiretapChannel.subscribe(createLoggingMessageHandler(name, level, expression)); | |
DirectChannel loggingChannel = new DirectChannel(); | |
loggingChannel.addInterceptor(new WireTap(wiretapChannel)); | |
return loggingChannel; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment