Java Spring引导和日志记录
我有一个带有slf4j日志记录的Spring引导应用程序 格拉德尔:Java Spring引导和日志记录,java,spring,logging,spring-boot,slf4j,Java,Spring,Logging,Spring Boot,Slf4j,我有一个带有slf4j日志记录的Spring引导应用程序 格拉德尔: buildscript { ext { springBootVersion = '2.0.0.BUILD-SNAPSHOT' } repositories { mavenCentral() maven { url "https://repo.spring.io/snapshot" } maven { url "https://repo.sp
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);
.....
将此项添加到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'
您必须添加
slf4j
记录器的实现,如logback
将以下内容添加到gradle构建中
编译'ch.qos.logback:logback classic:1.1.7'
您有两个选项:
springbootstarterweb
作为依赖项,所有日志依赖项都将被添加spring boot starter日志记录添加为依赖项
来源:首先尝试将所有
org.springframework.boot:springboot xxx
依赖项放到同一版本。其次,使用发布的版本,例如1.5.3.RELEASE
而不是BUILD-SNAPSHOT
。现在我使用1.5.3.RELEASE
版本,但这个问题不能解决问题,但对我来说不起作用。我不知道怎么解决。谢谢,但这并没有解决我的问题。我也有同样的例外java.lang.NoClassDefFoundError:org/apache/log4j/Logger
Yes,它的webapp。我有springbootstarterweb
依赖关系。但现在它不起作用了。过去六个月的日志记录还可以。也许是因为我改变了gradle的版本。
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
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'