Spring Boot and Logging

Viewed 2187

I have Spring Boot application with slf4j logging.

Gradle:

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')
 }

Class:

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);

.....

It worked before. Now I have exception where I create Logger. I didn't change anything, only tried to build project again. Maybe Spring Boot version was changed, I don't know.

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

I tried to exclude spring-boot-starter-logging and connect spring-boot-starter-log4j but it not worked for me. Then ClassNotFoundException with Log4j. How can I fix it?

P.S.: When I commented creating logger, I have another exception:

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
3 Answers
Related