5.9 - Logging Filter

The Logging filter allows an application to logs MINA protocol events while they are transiting on the filters chain. It can be added dynamically (ie, a session may add a filter whenever it wants).

The tracked events are :

  • exceptionCaught
  • messageReceived
  • messageSent
  • sessionClosed
  • sessionCreated
  • sessionIdle
  • sessionOpened

The event, filterClose, filterWrite and inputClosed events are not tracked.

Adding the filter

This can be done once and for each session, while creating the IoFilterChainBuilder instance :

...
NioSocketAcceptor acceptor = new NioSocketAcceptor();
DefaultIoFilterChainBuilder builderChain = acceptor.getFilterChain();
builderChain.addLast("logger", new LoggingFilter());
...

or it can be added dynamically, in a given session:

...
session.getFilterChain().addLast("logger", new LoggingFilter());
...

(here, the filter is added at the end of the filter’s chain, but it can added at the beginning, using addFirst, or before or after a given filter, with addBefore or addAfter)

Configuring the filter

Each event can be configured individually. For instance, to log the messageReceived event in DEBUG mode, simply add this code in your IoHandler implementation:

...
LoggingFilter loggingFilter = (LoggingFilter)session.getFilterChain().get( LoggingFilter.class );

if (logginFilter != null) {
    loggingFilter.setMessageReceivedLogLevel( LogLevel.DEBUG);
}
...

This is per session, and it’s dynamic.

Removing the filter

It’s always possible to remove the filter for a given session :

...
session.getFilterChain().remove("logger");
...