mybatis日志的使用问题:

a:focus {
outline: thin dotted #333;
outline: 5px auto -webkit-focus-ring-color;
outline-offset: -2px;
}
a:hover {
outline: 0;
}
a:active {
outline: 0;
}
a:hover {
color: #005580;
text-decoration: underline;
}
blockquote small:before {
content: ‘\2014 \00A0‘;
}
q:before {
content: "";
}
q:after {
content: "";
}
blockquote:before {
content: "";
}
blockquote:after {
content: "";
}

mybatis日志的使用问题:

mybatis

spring

log4j



在使用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 [[email protected]]

> 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

时间: 2024-08-27 21:10:29

mybatis日志的使用问题:的相关文章

笔记:MyBatis 日志显示-log4j2

在ClassPath路径创建log4j2.xml配置文件,增加如下日志配置: <?xml version="1.0" encoding="UTF-8"?> <Configuration status="OFF"> ????<!-- 附着器配置,日志输出到什么位置 --> ????<Appenders> ????????<!-- 控制台附着器类型,输出结果到 System.out 或是 Syst

论MyBatis日志-张伟东

论MyBatis日志 Mybatis内置的日志工厂提供日志功能,具体的日志实现有以下几种工具: · SLF4J · Apache Commons Logging · Log4j 2 · Log4j · JDK logging 具体选择哪个日志实现工具由MyBatis的内置日志工厂确定.它会使用最先找到的(按上文列举的顺序查找). 如果一个都未找到,日志功能就会被禁用. 不少应用服务器的classpath中已经包含Commons Logging,如Tomcat和WebShpere, 所以MyBat

MyBatis 日志

1?日志Mybatis 通过使用内置的日志工厂提供日志功能. 内置日志工厂将会把日志工作委托给下面的实现之一: SLF4JApache Commons LoggingLog4j 2Log4jJDK loggingMyBatis 内置日志工厂会基于运行时检测信息选择日志委托实现. 它会(按上面罗列的顺序)使用第一个查找到的实现. 当没有找到这些实现时,将会禁用日志功能. 你可以通过在 MyBatis 配置文件 mybatis-config.xml 里面添加一项 setting 来选择其它日志实现.

MyBatis——日志

Logging Mybatis内置的日志工厂提供日志功能,具体的日志实现有以下几种工具: SLF4J Apache Commons Logging Log4j 2 Log4j JDK logging 具体选择哪个日志实现工具由MyBatis的内置日志工厂确定.它会使用最先找到的(按上文列举的顺序查找). 如果一个都未找到,日志功能就会被禁用. 不少应用服务器的classpath中已经包含Commons Logging,如Tomcat和WebShpere, 所以MyBatis会把它作为具体的日志实

【转】mybatis 日志

原文链接 http://mybatis.github.io/mybatis-3/zh/logging.html Logging Mybatis内置的日志工厂提供日志功能,具体的日志实现有以下几种方式: SLF4J Apache Commons Logging Log4j 2 Log4j JDK logging 具体选择哪个日志实现由MyBatis的内置日志工厂确定.它会使用最先找到的(按上文列举的顺序查找). 如果一个都未找到,日志功能就会被禁用. 不少应用服务器的classpath中已经包含C

论MyBatis日志

Mybatis内置的日志工厂提供日志功能,具体的日志实现有以下几种工具: SLF4J Apache Commons Logging Log4j 2 Log4j JDK logging 具体选择哪个日志实现工具由MyBatis的内置日志工厂确定.它会使用最先找到的(按上文列举的顺序查找). 如果一个都未找到,日志功能就会被禁用. 不少应用服务器的classpath中已经包含Commons Logging,如Tomcat和WebShpere, 所以MyBatis会把它作为具体的日志实现.这将意味着,

Mybatis日志信息

问题:开发组说MyBatis的日志信息只能通过顶级日志记录器在debug下打印SQL,所以测试时一大堆的信息,弄得个单元测试,启动要老半天. 为了解决这样的问题,我看了下开发组的日志信息配置,代码如下: log4j.logger.com.ibatis=DEBUG log4j.logger.com.ibatis.common.jdbc.SimpleDataSource=DEBUG log4j.logger.com.ibatis.common.jdbc.ScriptRunner=DEBUG log4

Mybatis日志打印Sql

配置mybatis-config.xml <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <pl

springboot输出日志到指定目录,简单粗暴,springboot输出mybatis日志

springboot官方文档地址https://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#boot-features-custom-log-configuration spring官方地址https://spring.io/docs 使用springboot 默认会打印日志在控制台,但是他默认是不输出到文件的,所以要配置输出路径,那么还要写输出路径, 我们看官方案例 默认情况下,Spring Bo