最近在玩 mybatis 的时候突然想使用logback了,但是不知道怎么配置,所以百度了很久才百度出来,现在我把我百度出来的贴出来仅供大家参考
参考的网址:https://my.oschina.net/u/2263802/blog/956588
首先创建一个
logback.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <configuration> 3 <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> 4 <!-- encoder 默认配置为PatternLayoutEncoder --> 5 <encoder> 6 <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n 7 </pattern> 8 </encoder> 9 </appender> 10 <logger name="dao" level="DEBUG"/> 11 <logger name="cm.mbs.dao" level="INFO"/> 12 <root level="INFO"> 13 <appender-ref ref="STDOUT"/> 14 </root> 15 </configuration>
2.在POM.xml里面引入logback的jar包
1 <!-- log start --> 2 <dependency> 3 <groupId>org.slf4j</groupId> 4 <artifactId>slf4j-api</artifactId> 5 <version>${slf4j-api.version}</version> 6 </dependency> 7 <dependency> 8 <groupId>ch.qos.logback</groupId> 9 <artifactId>logback-core</artifactId> 10 <version>${logback.version}</version> 11 </dependency> 12 <dependency> 13 <groupId>ch.qos.logback</groupId> 14 <artifactId>logback-access</artifactId> 15 <version>${logback.version}</version> 16 </dependency> 17 <dependency> 18 <groupId>ch.qos.logback</groupId> 19 <artifactId>logback-classic</artifactId> 20 <version>${logback.version}</version> 21 </dependency> 22 <!-- log end -->
3.引入mybatis的jar包
1 <dependency> 2 <groupId>mysql</groupId> 3 <artifactId>mysql-connector-java</artifactId> 4 <version>5.1.46</version> 5 </dependency> 6 7 <dependency> 8 <groupId>org.mybatis</groupId> 9 <artifactId>mybatis</artifactId> 10 <version>3.5.1</version> 11 </dependency>
4.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> <!--使用logback的配置--> <settings> <setting name="logPrefix" value="dao." /> </settings> <!--<settings>--> <!--<!– 指定使用LOG4J输出日志 –>--> <!--<setting name="logImpl" value="LOG4J"/>--> <!--</settings>--> <typeAliases> <!-- 配置包的别名,通常在使用类时需要使用类的全限定名称,使用该配置后只要直接使用类名即可 --> <package name="cm.mbs.entity"/> </typeAliases> <environments default="development"> <environment id="development"> <transactionManager type="JDBC" /> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/test?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true" /> <property name="username" value="root" /> <property name="password" value="root" /> </dataSource> </environment> </environments> <mappers> <!-- 在src/main/resources目录下创建com/etc/mapper目录,也可以更进一步按照模块来创建com/etc/模块名/mapper目录 --> <mapper resource="mapper/UserMapper.xml" /> </mappers> </configuration>