mybatis日志的使用问题:

mybatis日志的使用问题:


在使用mybatis的时候,经常会遇到日志无法输出的问题;今天解决这个问题;

1、仅使用mybatis的时候如何输出:


一般单独使用mybatis+junit的时候会使用到日志的输出,使用是需要添加下面的代码:

org.apache.ibatis.logging.LogFactory.useStdOutLogging();

使用示例:

@Test
  public void testCondition(){
      try {
          //如果想在控制台中输出日志的内容,那么必须添加上这句话
          org.apache.ibatis.logging.LogFactory.useStdOutLogging();
          Reader reader = Resources.getResourceAsReader("mybatis-config.xml");
          SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder()
                  .build(reader);
          reader.close();
          SqlSession session = null;
          session = sqlSessionFactory.openSession();
          StudentMapper studentMapper = (StudentMapper) session.getMapper(StudentMapper.class);
          StudentExample studentExample = new StudentExample();
          GeneratedCriteria generatedCriteria = studentExample.createCriteria();
          generatedCriteria.addCriterion("1=1");
          List<Student> student = studentMapper.selectByExample(studentExample);
          for (Student student2 : student) {
              System.out.println(student2.getName());
          }
      } catch (IOException e) {
          e.printStackTrace();
      }
  }

运行查看输入的结果:

Logging initialized using 'org.apache.ibatis.logging.stdout.StdOutImpl' >adapter.
PooledDataSource forcefully closed/removed all connections.
PooledDataSource forcefully closed/removed all connections.
PooledDataSource forcefully closed/removed all connections.
PooledDataSource forcefully closed/removed all connections.
Openning JDBC Connection
Created connection 33431531.
ooo Using Connection [com.mysql.jdbc.JDBC4Connection@1fe1feb]
> Preparing: select id, name, gender, major, grade, supervisor_id from student WHERE ( 1=1 )
> Parameters:
<
Columns: id, name, gender, major, grade, supervisor_id
<
Row: 1, 李林, 男, 计算机科学与技术, 2011, 1
<== Row: 2, 陈明, 男, 软件技术, 2012, 1
<== Row: 4, 陈明2, 男, 软件技术, 2012, 2
李林
陈明
陈明2

同时在log4j.properties文件的配置信息如下:

# Global logging configuration
log4j.rootLogger=debug
# MyBatis logging configuration...
log4j.logger.org.mybatis.example=DEBUG
# Console output...
### Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

2、和spring配合使用时,日志的输出内容


spring+mybatis时输入的配置文件内容为:

## set log levels ###
#log4j.rootLogger = debug , stdout , D , E
log4j.rootLogger = debug , stdout , D
 
###  output to the 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{ABSOLUTE} %5p %c{ 1 }:%L - %m%n
log4j.appender.stdout.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [%c]-[%p] %m%n
 
### Output to the log file ###
log4j.appender.D = org.apache.log4j.DailyRollingFileAppender
log4j.appender.D.File = ${springmvc.root}/WEB-INF/logs/error.log 
log4j.appender.D.Append = true
log4j.appender.D.Threshold = ERROR 
log4j.appender.D.layout = org.apache.log4j.PatternLayout
log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n

posted @ 2015-05-12 12:48  thero  阅读(1457)  评论(0编辑  收藏  举报