如何启用 org.slf4j 版本的日志记录:Spring 引导中的“2.0.0-alpha1”
How to enable logging in org.slf4j for the version: '2.0.0-alpha1' in Spring boot
我已经使用SLF4J logging打印了所有的日志。我正在使用最新版本的 org.slf4j。
implementation 'org.slf4j:slf4j-api:2.0.0-alpha1'
implementation 'org.slf4j:log4j-over-slf4j:2.0.0-alpha1'
但是我收到以下错误并且没有打印任何日志。
SLF4J: No SLF4J providers were found.
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#noProviders for further details.
SLF4J: Class path contains SLF4J bindings targeting slf4j-api versions prior to 1.8.
SLF4J: Ignoring binding found at [jar:file:/home/user/.gradle/caches/modules-2/files-2.1/ch.qos.logback/logback-classic/1.2.3/7c4f3c474fb2c041d8028740440937705ebb473a/logback-classic-1.2.3.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#ignoredBindings for an explanation.
日志在旧版本 (1.7.25) 上运行良好。是否需要在项目上添加或配置任何内容才能打印这些日志
这是因为 slf4j 是一个抽象,需要与其他预先存在的库绑定。因此,如果您只是简单地使用 slf4j 来打印日志,它就不会工作,因为它没有日志记录级别等的设置。
This link 提供了一些相同的信息。
此外,SpringBoot 的新版本默认集成了 1.7.9 版的 slf4j
它将打印添加 slf4j-log4j12 而不是 slf4j-api
的消息
删除以下内容
implementation 'org.slf4j:slf4j-api:2.0.0-alpha1'
implementation 'org.slf4j:log4j-over-slf4j:2.0.0-alpha1'
添加
implementation group: 'org.slf4j', name: 'slf4j-log4j12', version: '+'
使用Spring Boot 2.x时不需要导入任何日志依赖。所需要的只是导入一些您很可能已经完成的 Spring Boot Starter。例如。 spring-boot-starter-web
,这取决于 spring-boot-starter-logging
,它会引入使用 Spring 框架进行日志记录所需的 spring-jcl
模块。
使用启动器时,默认使用Logback进行日志记录,不需要Log4j。
只需从构建文件中删除对 slf4j
的所有引用。以下是 Spring Boot Web Starter 在 build.gradle 文件中的引用。
compile("org.springframework.boot:spring-boot-starter-web")
我已经使用SLF4J logging打印了所有的日志。我正在使用最新版本的 org.slf4j。
implementation 'org.slf4j:slf4j-api:2.0.0-alpha1'
implementation 'org.slf4j:log4j-over-slf4j:2.0.0-alpha1'
但是我收到以下错误并且没有打印任何日志。
SLF4J: No SLF4J providers were found.
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#noProviders for further details.
SLF4J: Class path contains SLF4J bindings targeting slf4j-api versions prior to 1.8.
SLF4J: Ignoring binding found at [jar:file:/home/user/.gradle/caches/modules-2/files-2.1/ch.qos.logback/logback-classic/1.2.3/7c4f3c474fb2c041d8028740440937705ebb473a/logback-classic-1.2.3.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#ignoredBindings for an explanation.
日志在旧版本 (1.7.25) 上运行良好。是否需要在项目上添加或配置任何内容才能打印这些日志
这是因为 slf4j 是一个抽象,需要与其他预先存在的库绑定。因此,如果您只是简单地使用 slf4j 来打印日志,它就不会工作,因为它没有日志记录级别等的设置。 This link 提供了一些相同的信息。
此外,SpringBoot 的新版本默认集成了 1.7.9 版的 slf4j
它将打印添加 slf4j-log4j12 而不是 slf4j-api
的消息删除以下内容
implementation 'org.slf4j:slf4j-api:2.0.0-alpha1'
implementation 'org.slf4j:log4j-over-slf4j:2.0.0-alpha1'
添加
implementation group: 'org.slf4j', name: 'slf4j-log4j12', version: '+'
使用Spring Boot 2.x时不需要导入任何日志依赖。所需要的只是导入一些您很可能已经完成的 Spring Boot Starter。例如。 spring-boot-starter-web
,这取决于 spring-boot-starter-logging
,它会引入使用 Spring 框架进行日志记录所需的 spring-jcl
模块。
使用启动器时,默认使用Logback进行日志记录,不需要Log4j。
只需从构建文件中删除对 slf4j
的所有引用。以下是 Spring Boot Web Starter 在 build.gradle 文件中的引用。
compile("org.springframework.boot:spring-boot-starter-web")