log4j与slf4j整合

目录

1. 普通应用

  • maven依赖
 <!-- log4j的API -->
    <dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>1.2.17</version>
    </dependency>
    <!-- slf4j核心API -->
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-api</artifactId>
      <version>1.7.25</version>
    </dependency>
    <!-- slf4j-log4j12 slf4j转log4j-->
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-log4j12</artifactId>
      <version>1.7.25</version>
    </dependency>
  • log4j配置文件

参考: java日志框架log配置示例

  • log使用
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

private static Logger LOG = LoggerFactory.getLogger(BaseServlet.class);

2. web应用

  • maven依赖
<!-- log4j的API -->
    <dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>1.2.17</version>
    </dependency>
    <!-- slf4j核心API -->
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-api</artifactId>
      <version>1.7.25</version>
    </dependency>
    <!-- slf4j-log4j12 slf4j转log4j-->
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-log4j12</artifactId>
      <version>1.7.25</version>
    </dependency>
    <!-- springWeb包:log4j的监听器-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>3.2.16.RELEASE</version>
    </dependency>
  • log4j配置

参考: java日志框架log配置示例

  • web.xml配置
<context-param>
        <param-name>log4jConfigLocation</param-name>
        <param-value>classpath:log4j.xml</param-value>
    </context-param>
    <context-param>
        <param-name>log4jRefreshInterval</param-name>
        <param-value>60000</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
    </listener>
  • log使用

上一篇:Apache Log4j 任意代码执行漏洞


下一篇:java日志框架如何选型?