Log

作者: svatyvabin | 来源:发表于2017-09-05 10:41 被阅读60次

使用 Log 库

引入依赖

推荐使用 SLF4J,但是他只是一个接口,也就是说,还需要绑定额外的 Log 实现,才能够 Log,但是用这个的好处就是,你以后想换其他 Log 实现的时候,代码是不需要修改的。

Log 实现推荐使用 Logback,引入 logback-classic 库和 logback-core 库(被 logback-classic 依赖)。使用 maven 引入依赖:

    <dependencies>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.22</version>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.1.8</version>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-core</artifactId>
            <version>1.1.8</version>
        </dependency>
    </dependencies>

使用 Log

最简单的 Log 程序:

import org.slf4j.Logger;
import static org.slf4j.LoggerFactory.getLogger;

public class LogTest {
    private static final Logger LOG = getLogger(LogTest.class);

    public static void main(String[] args) {
        LOG.trace("trace");
        LOG.debug("debug");
        LOG.info("info: {}, {}", "hello");
        LOG.warn("warn");
        LOG.error("error");
    }
}

给 IntelliJ IDEA 添加 Live Templates 以便于快速创建 Logger 对象:

private static final org.slf4j.Logger LOG = org.slf4j.LoggerFactory.getLogger($CLASS_NAME$.class);

这里使用了完整的包,IntelliJ IDEA 会自动导入。
添加之后,需要设置一下 CLASS_NAME 的公式,设置为 className() 即可。

多重绑定 Log 解决方法

当你引入了 Logback 后,又引入了其他的 Log 实现(例如 Log4J),就会提示多绑定的错误:

SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/repository/ch/qos/logback/logback-classic/1.1.8/logback-classic-1.1.8.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/repository/org/slf4j/slf4j-log4j12/1.7.24/slf4j-log4j12-1.7.24.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.

解决办法是:

  1. 可以先用 Maven 看看整个依赖树,找到哪个库引入了其他 Log 实现:
mvn dependency:tree
  1. 根据官方文档,把其他库引入进来的 Log4J 库 exclusion:
<dependencies>
  <dependency>
    <groupId> org.apache.cassandra</groupId>
    <artifactId>cassandra-all</artifactId>
    <version>0.8.1</version>

    <exclusions>
      <exclusion> 
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
      </exclusion>
      <exclusion> 
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
      </exclusion>
    </exclusions> 

  </dependency>
</dependencies>
  1. 可能需要重复执行步骤1, 2

Logback 配置

以 Maven 为例,配置文件 logback.xml 在 resources 目录下。

一个简单的配置如下(仅终端输出):

<configuration>
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
        </encoder>
    </appender>

    <root level="debug">
        <appender-ref ref="STDOUT"/>
    </root>
</configuration>

输出 JdbcTemplate 的 SQL 语句

在 logback.xml 配置中添加:

<!--JdbcTemplate SQL语句-->
<logger name="org.springframework.jdbc.core.JdbcTemplate" level="DEBUG"/>

<!--JdbcTemplate SQL参数-->
<logger name="org.springframework.jdbc.core.StatementCreatorUtils" level="TRACE"/>

如果调用了 JdbcTemplate 但是没显示 SQL 语句和参数,可能是因为没有添加 jcl-over-slf4jlog4j-over-slf4j 这两个依赖

输出 MyBatis 的 SQL 语句和参数

<!--整个 mapper 包-->
<logger name="in.weib.mapper" level="DEBUG"/>

相关文章

网友评论

      本文标题:Log

      本文链接:https://www.haomeiwen.com/subject/lwkxgttx.html