Logging ConfigurationPage edited by Emmanuel LécharnyChanges (1)
Full ContentTable of ContentsBackgroundThe Apache MINA uses a system that allows for the developer of the MINA-base application to use their own logging system. SLF4JMINA employs the Simple Logging Facade for Java (SLF4J). You can find information on SLF4J here. This logging utility allows for the implementation of any number of logging systems. You may use log4j, java.util.logging or other logging systems. The nice part about this is that if you want to change from java.util.logging to log4j later on in the development process, you do not need to change your source code at all. Choosing the Right JARsSLF4J uses a static binding. This means there is one JAR file for each supported logging framework. You can use your favorite logging framework by choosing the JAR file that calls the logging framework you chose statically. The following is the table of required JAR files to use a certain logging framework.
There are a few things to keep in mind:
Once configured properly, you can continue to configure the actual logging framework you chose (e.g. modifying log4j.properties). Overriding Jakarta Commons LoggingSLF4J also provides a way to convert the existing applications that use Jakarta Commons Logging to use SLF4J without changing the application code. Just remove commons-loggong JAR file from the class path, and add jcl104-over-slf4j.jar to the class path. log4j exampleFor this example we will use the log4j logging system. We set up a project and place the following snippet into a file called log4j.properties: # Set root logger level to DEBUG and its only appender to A1. log4j.rootLogger=DEBUG, A1
# A1 is set to be a ConsoleAppender.
log4j.appender.A1=org.apache.log4j.ConsoleAppender
# A1 uses PatternLayout.
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%-4r [%t] %-5p %c{1} %x - %m%n
This file will be placed in the src directory of our project. If you are using an IDE, you essentially want the configuration file to be in the classpath for the JVM when you are testing your code.
Next we will set up a simple example server in order to generate some logs. Here we have taken the EchoServer example project and added logging to the class: public static void main(String[] args) throws Exception {
IoAcceptor acceptor = new SocketAcceptor();
DefaultIoFilterChainBuilder chain = acceptor.getFilterChain();
LoggingFilter loggingFilter = new LoggingFilter();
chain.addLast("logging", loggingFilter);
acceptor.setLocalAddress(new InetSocketAddress(PORT));
acceptor.setHandler(new EchoProtocolHandler());
acceptor.bind();
System.out.println("Listening on port " + PORT);
}
As you can see we removed the addLogger method and added in the 2 lines added to the example EchoServer. With a reference to the LoggingFilter, you can set the logging level per event type in your handler that is associated with the IoAcceptor here. In order to specify the IoHandler events that trigger logging and to what levels the logging is performed, there is a method in the LoggingFilter called setLogLevel(IoEventType, LogLevel). Below are the options for this method:
Here are the descriptions of the LogLevels:
With this information, you should be able to get a basic system up and running and be able to expand upon this simple example in order to be generating log information for your system.
Change Notification Preferences
View Online
|
View Changes
|
Add Comment
|
