JFinal + Druid + log4j 打印sql语句以及参数的问题

1、GlobalConfig -> JFinalConfig 配置

@Override
   public void configPlugin(Plugins me) {
      DruidPlugin druidPlugin = createDruidPlugin();
      druidPlugin.setValidationQuery("select 1 from dual");

      WallFilter wallFilter = new WallFilter();
      wallFilter.setDbType("oracle");
      druidPlugin.addFilter(wallFilter);

      Log4jFilter log4jFilter = new Log4jFilter();
      druidPlugin.addFilter(log4jFilter);

      StatFilter statFilter = new StatFilter();
      statFilter.setMergeSql(true);
      statFilter.setLogSlowSql(true);
      statFilter.setDbType("oracle");
      statFilter.setSlowSqlMillis(500L);
      druidPlugin.addFilter(statFilter);

      me.add(druidPlugin);

      // 配置C3p0数据库连接池插件(Oracle)
//    C3p0Plugin c3p0Plugin = createC3p0Plugin();
//    me.add(c3p0Plugin);

      ActiveRecordPlugin activeRecordPlugin = new ActiveRecordPlugin(druidPlugin);
      activeRecordPlugin.setBaseSqlTemplatePath(PathKit.getRootClassPath());
      activeRecordPlugin.addSqlTemplate("global.sql");
      //MappingKit.mapping(activeRecordPlugin);
      activeRecordPlugin.setShowSql(IS_DEBUG);

      //配置Oracle方言
      activeRecordPlugin.setDialect(new OracleDialect());
      //配置属性名(字段名)大小写不敏感容器工厂
      activeRecordPlugin.setContainerFactory(new CaseInsensitiveContainerFactory());
      me.add(activeRecordPlugin);
      //activeRecordPlugin.addMapping("T_SYS_USER", User.class);
   }
   
   public static DruidPlugin createDruidPlugin(){
   String driverClassName = PropKit.get("jdbc.driverClassName");
   String url = PropKit.get("jdbc.url");
   String userName = PropKit.get("jdbc.userName");
   String passWord = PropKit.get("jdbc.passWord");
   return new DruidPlugin(url, userName, passWord,driverClassName);
}

    @Override
   public void configPlugin(Plugins me) {
      DruidPlugin druidPlugin = createDruidPlugin();
      druidPlugin.setValidationQuery("select 1 from dual");

      WallFilter wallFilter = new WallFilter();
      wallFilter.setDbType("oracle");
      druidPlugin.addFilter(wallFilter);

      Log4jFilter log4jFilter = new Log4jFilter();
      druidPlugin.addFilter(log4jFilter);

      StatFilter statFilter = new StatFilter();
      statFilter.setMergeSql(true);
      statFilter.setLogSlowSql(true);
      statFilter.setDbType("oracle");
      statFilter.setSlowSqlMillis(500L);
      druidPlugin.addFilter(statFilter);

      me.add(druidPlugin);

      // 配置C3p0数据库连接池插件(Oracle)
//    C3p0Plugin c3p0Plugin = createC3p0Plugin();
//    me.add(c3p0Plugin);

      ActiveRecordPlugin activeRecordPlugin = new ActiveRecordPlugin(druidPlugin);
      activeRecordPlugin.setBaseSqlTemplatePath(PathKit.getRootClassPath());
      activeRecordPlugin.addSqlTemplate("global.sql");
      //MappingKit.mapping(activeRecordPlugin);
      activeRecordPlugin.setShowSql(IS_DEBUG);

      //配置Oracle方言
      activeRecordPlugin.setDialect(new OracleDialect());
      //配置属性名(字段名)大小写不敏感容器工厂
      activeRecordPlugin.setContainerFactory(new CaseInsensitiveContainerFactory());
      me.add(activeRecordPlugin);
      //activeRecordPlugin.addMapping("T_SYS_USER", User.class);
   }
   
   @Override
   public void configHandler(Handlers me) {
//    me.add(new DownFileHandler());

      DruidStatViewHandler druidStatViewHandler = new DruidStatViewHandler("/druid", new IDruidStatViewAuth() {
         @Override
         public boolean isPermitted(HttpServletRequest httpServletRequest) {
            return true;
         }
      });
      me.add(druidStatViewHandler);
   }

2,log4j配置

log4j.rootLogger=warn,A1,A2

#1.打印druid连接池SQL语句,info级别的语句打印到info_slowsql.log,warn级别语句打印到warn_slowlog.log;
#2.将日志写到文件中; 

# Druid
log4j.logger.druid.sql=info,A1,A2
log4j.logger.druid.sql.DataSource=info,A1,A2
log4j.logger.druid.sql.Connection=info,A1,A2
log4j.logger.druid.sql.Statement=info,A1,A2
log4j.logger.druid.sql.ResultSet=info,A1,A2

log4j.appender.A1=org.apache.log4j.DailyRollingFileAppender
log4j.appender.A1.File=../logs/ep.log
log4j.appender.A1.DatePattern='.'yyyy-MM-dd
#log4j.appender.A1.File=${catalina.home}/logs/info_slowsql.log
#log4j.appender.A1.File=${WebApp.TomcatHome}/logs/info_slowsql.log
log4j.appender.A1.ImmediateFlush=true
log4j.appender.A1.Append=true
log4j.appender.A1.Threshold=info
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=[%d{yyyy-MM-dd HH:mm:ss}] [%p] [%c] - %m%n

log4j.appender.A2=org.apache.log4j.DailyRollingFileAppender
log4j.appender.A2.File=../logs/ep.log
log4j.appender.A2.DatePattern='.'yyyy-MM-dd
#log4j.appender.A2.File=${catalina.home}/logs/warn_slowsql.log
#log4j.appender.A2.File=${WebApp.TomcatHome}/logs/warn_slowsql.log
log4j.appender.A2.ImmediateFlush=true
log4j.appender.A2.Append=true
log4j.appender.A2.Threshold=warn
log4j.appender.A2.layout=org.apache.log4j.PatternLayout
log4j.appender.A2.layout.ConversionPattern=[%d{yyyy-MM-dd HH:mm:ss}] [%p] [%c] - %m%n


依旧不显示Druid的日志输出,不知道什么原因

评论区

JFinal

2017-04-19 10:54

前天有个小伙伴,也为 druid 做了一个 filter,也是没生效,他单步调试到了 DruidDataSource 内部,发现 filter 是存在的,证明与 jfinal 是无关的

jfinal 本质上只是将你写的 Log4jFilter 直接转交给了 druid,并未做任何干预,建议你看一下 druid 的官方文档,或许升级一下 druid 的 jar 包就可以了

chunming

2018-04-18 11:41

@BigSnake1989 不可以使用info级别的日志, 这个是由druid源码决定的,里面直接this.statementLogger.debug(message); 所以在配置log4j时log4j.appender.A2.Threshold=warn ,log4j.logger.druid.sql , log4j.logger.druid.sql.Statement 必须为debug

热门反馈

扫码入社