/ 中存储网

Apache mina的日志配置

2014-02-04 15:43:01 来源:itjs.cn

用Log4J 1.2.x的框架时,我们需要三个jar包:log4j-1.2.3.jar  slf4j-api-1.6.2jar  slf4j-log4j12-1.6.2jar 从名字上可以看出第三个是主要的jar包,提供了前两者的增强功能。

下面我们以一个例子来说明日志过滤器的配置方法:

首先我们创建一个log4j的配置文件并设置日志输出级别为debug:

log4j.appender.MINA=org.apache.log4j.ConsoleAppender

log4j.appender.MINA.layout=org.apache.log4j.PatternLayout

log4j.appender.MINA.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss}  %m%n

#to console#

log4j.appender.stdout=org.apache.log4j.ConsoleAppender

log4j.appender.stdout.Target=System.out

log4j.appender.stdout.layout=org.apache.log4j.PatternLayout

log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss}  %m%n

#to file#

log4j.appender.file=org.apache.log4j.FileAppender

log4j.appender.file.File=./log/minademos.log

log4j.appender.file.layout=org.apache.log4j.PatternLayout

log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss}  %l  %m%n

#error/warn/info/debug#

log4j.rootLogger=debug, stdout,MINA,file

接下来我们写一个服务器端的代码,在8080端口启动一个服务:

package com.yushh.test.logging;

import java.io.IOException;

import java.net.InetSocketAddress;

import org.apache.mina.core.filterchain.DefaultIoFilterChainBuilder;

import org.apache.mina.core.service.IoAcceptor;

import org.apache.mina.filter.logging.LogLevel;

import org.apache.mina.filter.logging.LoggingFilter;

import org.apache.mina.transport.socket.nio.NioSocketAcceptor;

public class EchoServer {

private static final int PORT = 8080;

public static void main(String[] args) throws IOException {

IoAcceptor acceptor = new NioSocketAcceptor();

DefaultIoFilterChainBuilder chain = acceptor.getFilterChain();

LoggingFilter loggingFilter = new LoggingFilter();

chain.addLast("logging", loggingFilter);

acceptor.setDefaultLocalAddress(new InetSocketAddress(PORT));

acceptor.setHandler(new EchoProtocolHandler());

acceptor.bind();

System.out.println("Listening on port " + PORT);

}

}

注意此处没有具体配置loggingFilter,下面我们用dos telnet本机的8080端口得到如下的日志信息:

Listening on port 8080

2012-03-15 14:34:56  CREATED

2012-03-15 14:34:56  CREATED

2012-03-15 14:34:56  OPENED

2012-03-15 14:34:56  OPENED

2012-03-15 14:34:58  RECEIVED: HeapBuffer[pos=0 lim=1 cap=2048: 61]

2012-03-15 14:34:58  RECEIVED: HeapBuffer[pos=0 lim=1 cap=2048: 61]

2012-03-15 14:35:00  RECEIVED: HeapBuffer[pos=0 lim=1 cap=2048: 62]

2012-03-15 14:35:00  RECEIVED: HeapBuffer[pos=0 lim=1 cap=2048: 62]

2012-03-15 14:35:01  RECEIVED: HeapBuffer[pos=0 lim=1 cap=1024: 63]

2012-03-15 14:35:01  RECEIVED: HeapBuffer[pos=0 lim=1 cap=1024: 63]

2012-03-15 14:35:02  RECEIVED: HeapBuffer[pos=0 lim=1 cap=1024: 41]

2012-03-15 14:35:02  RECEIVED: HeapBuffer[pos=0 lim=1 cap=1024: 41]

2012-03-15 14:35:05  CLOSED

2012-03-15 14:35:05  CLOSED

可见session的几种生命周期都被答应出来,从输出的RECEIVED信息中可以看见我在客户端输入的字符为‘abcA’,呵呵这个以后再讨论。

下面我们修改一下服务器代码:

package com.yushh.test.logging;

import java.io.IOException;

import java.net.InetSocketAddress;

import org.apache.mina.core.filterchain.DefaultIoFilterChainBuilder;

import org.apache.mina.core.service.IoAcceptor;

import org.apache.mina.filter.logging.LogLevel;

import org.apache.mina.filter.logging.LoggingFilter;

import org.apache.mina.transport.socket.nio.NioSocketAcceptor;

public class EchoServer {

private static final int PORT = 8080;

public static void main(String[] args) throws IOException {

IoAcceptor acceptor = new NioSocketAcceptor();

DefaultIoFilterChainBuilder chain = acceptor.getFilterChain();

LoggingFilter loggingFilter = new LoggingFilter();

loggingFilter.setSessionClosedLogLevel(LogLevel.NONE);

loggingFilter.setSessionCreatedLogLevel(LogLevel.DEBUG);

loggingFilter.setSessionOpenedLogLevel(LogLevel.INFO);

chain.addLast("logging", loggingFilter);

acceptor.setDefaultLocalAddress(new InetSocketAddress(PORT));

acceptor.setHandler(new EchoProtocolHandler());

acceptor.bind();

System.out.println("Listening on port " + PORT);

}

}

我们在LoggingFilter 增加了日志输出级别,设置当session关闭时不打印任何日志,当session建立时日志为debug级别,当session打开的时候日志为info级别,同样的客户端测试结果如下:

Listening on port 8080

2012-03-15 14:38:03  CREATED

2012-03-15 14:38:03  CREATED

2012-03-15 14:38:03  OPENED

2012-03-15 14:38:03  OPENED

2012-03-15 14:38:05  RECEIVED: HeapBuffer[pos=0 lim=1 cap=2048: 61]

2012-03-15 14:38:05  RECEIVED: HeapBuffer[pos=0 lim=1 cap=2048: 61]

2012-03-15 14:38:05  RECEIVED: HeapBuffer[pos=0 lim=1 cap=2048: 62]

2012-03-15 14:38:05  RECEIVED: HeapBuffer[pos=0 lim=1 cap=2048: 62]

2012-03-15 14:38:08  RECEIVED: HeapBuffer[pos=0 lim=1 cap=1024: 63]

2012-03-15 14:38:08  RECEIVED: HeapBuffer[pos=0 lim=1 cap=1024: 63]

2012-03-15 14:38:09  RECEIVED: HeapBuffer[pos=0 lim=1 cap=1024: 41]

2012-03-15 14:38:09  RECEIVED: HeapBuffer[pos=0 lim=1 cap=1024: 41]

可见mina优先使用代码中的 日志输出级别。