文章目录
使用logback输出日志
spring应用默认的日志输出工具;
项目依赖
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
兼容使用log4j(v1)的代码
需包含的依赖:
<dependency>
<!-- 提供log4j(v1)的api,slf4j介入实际输出 -->
<groupId>org.slf4j</groupId>
<artifactId>log4j-over-slf4j</artifactId>
<version>${slf4j.version}</version>
</dependency>
需排除的依赖:
<exclusions>
<exclusion>
<!-- log4j(v1)的api+核心功能 -->
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
<exclusion>
<!-- 避免slf4j最终绑定到log4j(v1) -->
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
<exclusion>
<!-- log4j(v1)的安全漏洞修复版 -->
<groupId>ch.qos.reload4j</groupId>
<artifactId>reload4j</artifactId>
</exclusion>
<exclusion>
<!-- 避免slf4j最终绑定到log4j(v1) -->
<groupId>org.slf4j</groupId>
<artifactId>slf4j-reload4j</artifactId>
</exclusion>
</exclusions>
兼容使用jcl的代码
需包含的依赖:
<dependency>
<!-- 提供commons-logging的api,slf4j介入实际输出 -->
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>${slf4j.version}</version>
</dependency>
需排除的依赖:
<exclusions>
<exclusion>
<!-- jcl的api+核心功能 -->
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
<exclusion>
<!-- 避免slf4j最终绑定到jcl -->
<groupId>org.slf4j</groupId>
<artifactId>slf4j-jcl</artifactId>
</exclusion>
</exclusions>
兼容使用log4j(v2)的代码
需包含的依赖:
<dependency>
<!-- 提供log4j(v2)的api -->
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>${log4j2.version}</version>
</dependency>
<dependency>
<!-- 实际输出交接给slf4j -->
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-to-slf4j</artifactId>
<version>${log4j2.version}</version>
</dependency>
需排除的依赖:
<exclusions>
<exclusion>
<!-- log4j(v2)的核心功能 -->
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
</exclusion>
<exclusion>
<!-- 避免slf4j最终绑定到log4j(v2) -->
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
</exclusion>
</exclusions>
使用log4j(v2)输出日志
hive3默认的日志输出工具;
项目依赖
<dependency>
<!-- 传递引入log4j-api和log4j-core -->
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>${log4j2.version}</version>
</dependency>
兼容使用log4j(v1)的代码
需包含的依赖:
<dependency>
<!-- 提供log4j(v1)的api,slf4j介入实际输出 -->
<groupId>org.slf4j</groupId>
<artifactId>log4j-over-slf4j</artifactId>
<version>${slf4j.version}</version>
</dependency>
需排除的依赖:
<exclusions>
<exclusion>
<!-- log4j(v1)的api+核心功能 -->
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
<exclusion>
<!-- 避免slf4j最终绑定到log4j(v1) -->
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
<exclusion>
<!-- log4j(v1)的安全漏洞修复版 -->
<groupId>ch.qos.reload4j</groupId>
<artifactId>reload4j</artifactId>
</exclusion>
<exclusion>
<!-- 避免slf4j最终绑定到log4j(v1) -->
<groupId>org.slf4j</groupId>
<artifactId>slf4j-reload4j</artifactId>
</exclusion>
</exclusions>
兼容使用jcl的代码
需包含的依赖:
<dependency>
<!-- 提供commons-logging的api,slf4j介入实际输出 -->
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>${slf4j.version}</version>
</dependency>
需排除的依赖:
<exclusions>
<exclusion>
<!-- jcl的api+核心功能 -->
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
<exclusion>
<!-- 避免slf4j最终绑定到jcl -->
<groupId>org.slf4j</groupId>
<artifactId>slf4j-jcl</artifactId>
</exclusion>
</exclusions>
兼容使用logback的代码
排除依赖即可,logback并不提供专用的loggerFactory来初始化logger对象
<exclusions>
<exclusion>
<!-- 避免slf4j最终绑定到logback -->
<groupId>ch.qos.logback</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>