3

本文使用环境

  1. win7

  2. Idea 14.1.4

  3. jfinal 2.0

1.jfinal如何使用log4j

a.导入jar包

将官网提供的log4j.jar包导入项目

b.配置文件

在项目配置文件(project/src/log4j.properties)中配置如下内容即可生效:

log4j.rootLogger=INFO, stdout, file
log4j.appender.stdout.Target=System.out
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%n%-d{yyyy-MM-dd HH:mm:ss}%n[%p]-[Thread: %t]-[%C.%M()]: %m%n

# Output to the File
log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=./myapp.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%n%-d{yyyy-MM-dd HH:mm:ss}%n[%p]-[Thread: %t]-[%C.%M()]: %m%n

# Druid
log4j.logger.druid.sql=warn,stdout
log4j.logger.druid.sql.DataSource=warn,stdout
log4j.logger.druid.sql.Connection=warn,stdout
log4j.logger.druid.sql.Statement=debug,stdout
log4j.logger.druid.sql.ResultSet=warn,stdout

c.生成日志文件

运行项目后将在project根目录下生成myapp.log的文件。

2.jfinal 异常记录到log4j

2.1 扩展异常类ExceptionIntoLogInterceptor

package interceptor;

import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
import com.jfinal.core.JFinal;
import com.jfinal.log.Logger;

/**
 * 异常处理日志 全局拦截器
 * Created by rener on 2015/12/7.
 */
public class ExceptionIntoLogInterceptor implements Interceptor {

    private static final Logger log = Logger.getLogger(ExceptionIntoLogInterceptor.class);

    @Override
    public  void intercept(Invocation invocation){
        //Controller controller=invocation.getController();
        //HttpServletRequest request=controller.getRequest();
        try{
            invocation.invoke(); //一定要注意,把处理放在invoke之后,因为放在之前的话,是会空指针
        }catch (Exception e){
            //log 处理
            logWrite(invocation, e);
        }finally {
            //记录日志到数据库,暂未实现
            try{

            }catch (Exception ee){

            }
        }


    }


    private void logWrite(Invocation inv,Exception e){
        //开发模式
        if (JFinal.me().getConstants().getDevMode()){
            e.printStackTrace();
        }
        StringBuilder sb =new StringBuilder("\n---Exception Log Begin---\n");
        sb.append("Controller:").append(inv.getController().getClass().getName()).append("\n");
        sb.append("Method:").append(inv.getMethodName()).append("\n");
        sb.append("Exception Type:").append(e.getClass().getName()).append("\n");
        sb.append("Exception Details:");
        log.error(sb.toString(),e);

    }
}

2.2 配置AppConfig.java中的configInterceptor注册全局拦截

 @Override
    public void configInterceptor(Interceptors interceptors){
        //全局拦截器,对所有请求拦截

        //添加控制层全局拦截器
        //interceptors.addGlobalActionInterceptor(new GlobalActionInterceptor());
        interceptors.addGlobalActionInterceptor(new ExceptionIntoLogInterceptor());
        //添加业务层全局拦截器
        //interceptors.addGlobalServiceInterceptor(new GlobalServiceInterceptor());
        interceptors.addGlobalServiceInterceptor(new ExceptionIntoLogInterceptor());

        //兼容老版jfinal写法
        //interceptors.add(new GlobalActionInterceptor());
    }

启用项目后,发生异常将记录到日志文件。


renner
47 声望2 粉丝