android log4j gradle,springboot + gradle 集成log4j2

exclude springboot自带的日志框架

springboot 本身自带logback日志框架 以及项目中的其它依赖可能带有日志框架, 因此需要在gradle全局禁用其它日志框架

configurations.all {

exclude group:'org.springframework.boot', module: 'spring-boot-starter-logging'

exclude module: 'logback-classic'

exclude module: 'logback-core'

}

复制代码

通常exclude的两种方式

方式1

如果build.gradle中没有分项目的话, exclude就是放在文件中, 和dependencies同级即可

移除前:

f31a4a8b812e0df600ab50423647e784.png

移除后:

be3ecb377b0c7ce5d3d70d013b4b3ccc.png

方式二

如果build.gradle分项目了

80ac55fc86b4183739ff0d102b7e86d6.png

其中settings.gradle如下,有rootProject 并且include了多个项目

rootProject.name = 'gradletest'

include 'app'

include 'module1'

include 'module2'

复制代码

那么exclude就需要放在subprojects代码块中, 就会对所有子项目(所有子项目合起来就是你的项目)生效

subprojects {

// 省略其它内容

configurations.all {

exclude group:'org.springframework.boot', module: 'spring-boot-starter-logging'

exclude module: 'logback-classic'

exclude module: 'logback-core'

}

}

复制代码

引入log4j2依赖

如果springboot项目继承了spring-boot-starter-parent, 使用起步依赖, log4j2依赖可以不用加依赖版本号

compile 'org.springframework.boot:spring-boot-starter-log4j2'

复制代码

否则需要加依赖版本号

compile 'org.springframework.boot:spring-boot-starter-log4j2:2.4.0'

复制代码

添加log4j2-spring.xml文件

在项目合适位置的resource中添加log4j2的配置文件

如果自定义了文件名(包括log4j2.xml这个文件名),需要在application.yml中配置

logging:

config: xxxx.xml

复制代码

默认名log4j2-spring.xml,使用该名称就无需在application.yml中额外配置

log4j2-spring.xml具体如何配置可以参考我的另一篇文章

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 Spring 中使用 SLF4J 和 Log4j2,需要在 Gradle 中添加以下依赖: ``` dependencies { implementation 'org.springframework.boot:spring-boot-starter' implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'org.springframework.boot:spring-boot-starter-log4j2' implementation 'org.slf4j:slf4j-api:1.7.30' implementation 'org.slf4j:jul-to-slf4j:1.7.30' implementation 'org.slf4j:log4j-over-slf4j:1.7.30' implementation 'org.apache.logging.log4j:log4j-core:2.14.1' implementation 'org.apache.logging.log4j:log4j-slf4j-impl:2.14.1' } ``` 其中,`spring-boot-starter-log4j2` 包含了 Log4j2 的依赖,`slf4j-api` 是 SLF4J 的接口,`jul-to-slf4j` 和 `log4j-over-slf4j` 是将 JUL 和 Log4j 转换为 SLF4J 的实现,`log4j-core` 和 `log4j-slf4j-impl` 是 Log4j2 的实现。 在 Spring 中配置 Log4j2,需要在 `src/main/resources` 目录下创建一个 `log4j2.xml` 文件,配置如下: ``` <?xml version="1.0" encoding="UTF-8"?> <Configuration status="WARN"> <Appenders> <Console name="Console" target="SYSTEM_OUT"> <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/> </Console> </Appenders> <Loggers> <Root level="info"> <AppenderRef ref="Console"/> </Root> </Loggers> </Configuration> ``` 这里使用了一个控制台日志输出的 Appender,配置了日志输出的格式和级别。最后将 Appender 添加到 Root Logger 中即可。 在 Spring 中使用日志,可以直接使用 SLF4J 的接口,例如: ``` import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class MyClass { private static final Logger logger = LoggerFactory.getLogger(MyClass.class); public void myMethod() { logger.debug("Debug message"); logger.info("Info message"); logger.warn("Warn message"); logger.error("Error message"); } } ``` 这里使用了 `LoggerFactory.getLogger` 方法创建了一个 Logger 实例,然后使用不同级别的日志输出信息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值