Log4j 验证异常是否作为最后一个参数传递

Log4j Validating if Exception is passed as last argument

我正在将 slf4j 与 log4j 结合使用。我的 POM 依赖项如下所示 -

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>1.7.5</version>
</dependency>
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-log4j12</artifactId>
    <version>1.7.5</version>
</dependency>
<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.17</version>
</dependency>

SLF4J FAQ所述

The SLF4J API supports parametrization in the presence of an exception, assuming the exception is the last parameter.

有没有办法确定异常是否作为最后一个参数传递给所有记录器的方法调用(如 errorinfo)?

没有简单的方法可以在编译时甚至运行时进行这种检查。

但是您可以使用 FindBugs and the findbugs-slf4j plugin to check this statically with SLF4J_PLACE_HOLDER_MISMATCHSLF4J_FORMAT_SHOULD_BE_CONST 的组合。