【问题标题】:Spring Boot and LoggingSpring Boot 和日志记录
【发布时间】:2023-07-30 21:22:01
【问题描述】:

我有带有 slf4j 日志记录的 Spring Boot 应用程序。

分级:

buildscript {
    ext {
        springBootVersion = '2.0.0.BUILD-SNAPSHOT'
    }
    repositories {
        mavenCentral()
        maven { url "https://repo.spring.io/snapshot" }
        maven { url "https://repo.spring.io/milestone" }
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

dependencies {
    compile fileTree(dir: 'lib', include: '*.jar')
    compile group: 'com.google.guava', name: 'guava', version: '17.0'

    // Spring
    compile 'org.springframework.boot:spring-boot-starter-web:1.5.1.RELEASE'
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-parent', version: '1.5.1.RELEASE'

    // Spring Security
    compile 'org.springframework.boot:spring-boot-starter-security'

    // Template engine
    compile 'org.springframework.boot:spring-boot-starter-thymeleaf:1.5.1.RELEASE'
    compile 'org.thymeleaf.extras:thymeleaf-extras-springsecurity3:2.1.2.RELEASE'
    compile group: 'org.thymeleaf', name: 'thymeleaf-spring5', version: '3.0.3.M1'

    // DB and ORM
    compile 'org.springframework.boot:spring-boot-starter-data-jpa:1.5.1.RELEASE'
    compile 'org.apache.derby:derby:10.13.1.1'

    // Form validation
    compile 'org.hibernate:hibernate-validator:5.2.2.Final'
    compile 'javax.el:el-api:2.2'

    // SNMP
    compile 'org.snmp4j:snmp4j:1.10.1'
    compile 'org.snmp4j:snmp4j-agent:1.2'

    testCompile('org.springframework.boot:spring-boot-starter-test')
 }

类:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

.....

@SpringBootApplication
public class MyApplication {

    private static final Logger LOGGER = LoggerFactory.getLogger(MyApplication.class);

    public static void main(String[] args) {
        SpringApplication.run(new Class<?>[] {MyApplication.class}, args);

.....

它以前工作过。现在我在创建 Logger 时遇到了异常。我没有改变任何东西,只是尝试再次构建项目。可能是Spring Boot版本变了,我不知道。

Exception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
    at by.virkom.MyApplication.<clinit>(MyApplication.java:18)
Caused by: java.lang.ClassNotFoundException: org.slf4j.LoggerFactory
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 1 more

我试图排除 spring-boot-starter-logging 并连接 spring-boot-starter-log4j 但它对我不起作用。然后使用 Log4j 进行 ClassNotFoundException。我该如何解决?

P.S.:当我评论创建记录器时,我有另一个例外:

Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/boot/SpringApplication
    at by.virkom.MyApplication.main(MyApplication.java:22)
Caused by: java.lang.ClassNotFoundException: org.springframework.boot.SpringApplication
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 1 more

【问题讨论】:

  • 首先尝试将所有org.springframework.boot:spring-boot-xxx 依赖项带到同一版本。其次使用已发布的版本,例如1.5.3.RELEASE 而不是 BUILD-SNAPSHOT
  • 现在我使用1.5.3.RELEASE版本,但问题没有解决

标签: java spring logging spring-boot slf4j


【解决方案1】:

将此添加到您的 gradle 文件中:

compile group: 'org.apache.logging.log4j', name: 'log4j-api', version: '2.5'
compile group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.5'
compile group: 'org.apache.logging.log4j', name: 'log4j-slf4j-impl', version: '2.5'

【讨论】:

  • 谢谢,但这并没有解决我的问题。我也有同样的例外。 java.lang.NoClassDefFoundError: org/apache/log4j/Logger
【解决方案2】:

您必须添加slf4j Logger 的实现,例如 logback

将以下内容添加到您的 gradle 构建中

compile 'ch.qos.logback:logback-classic:1.1.7'

【讨论】:

  • 谢谢,但这对我不起作用。不知道怎么解决。
【解决方案3】:

你有两个选择:

  1. 如果您的应用是 Web 应用,请将 spring-boot-starter-web 添加为依赖项,所有日志记录依赖项都会添加。
  2. 如果您的应用不是 web 应用,请添加 spring-boot-starter-logging 作为依赖项。

来源:https://docs.spring.io/spring-boot/docs/current/reference/html/howto-logging.html

【讨论】:

  • 是的,它的 webapp。我有spring-boot-starter-web 依赖。但它现在不起作用。过去六个月的记录还可以。也许是因为我改变了 gradle 版本。