Vin*_*jan 2 log4j gradle log4j2 spring-boot
我是Spring Boot的新手,使用Spring Boot进行简单的log4j演示.我使用了gradle项目并拥有spring-boot-starter-web和groovy依赖项.下面是我的log4j.properties文件内容.我需要的是,当我执行主程序并使用注释@ Log4J时,我必须能够将log.perflog保存到本地(windows)中的文件中.
log4j.rootLogger = WARN , stdout, cslLog
log4j.logger.perfLog = WARN, perfLog
log4j.additivity.perfLog = false
log4j.appender.perfLog = org.apache.log4j.RollingFileAppender
log4j.appender.perfLog.File = ${GRAILS_HOME}/logs/csl.log
log4j.appender.perfLog.Append = true
log4j.appender.perfLog.ImmediateFlush = true
log4j.appender.perfLog.MaxFileSize=200MB
log4j.appender.perfLog.MaxBackupIndex = 1
Run Code Online (Sandbox Code Playgroud)
我的样本groovy类:
package sample.actuator.log4j
import groovy.util.logging.Log4j;
import org.apache.log4j.Logger;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@Log4j
@RestController
@EnableAutoConfiguration
class HelloGroovy {
static Logger perfLog = Logger.getLogger("perfLog")
@RequestMapping("/logger")
String logger() {
log.info "created a new item named identifier"
log.error "created a new item named identifier"
log.warn "created a new item named identifier"
System.out.println("Test")
perfLog.trace("Test")
return "Logger Called."
}
static main(args) {
SpringApplication.run(this, args)
}
}
Run Code Online (Sandbox Code Playgroud)
所有get是在控制台中打印的前3行然后"Test",发布我在log4j.properties中提到的文件中没有显示任何内容.
我的build.gradle文件
buildscript {
repositories {
maven {
url 'http://artifactory.myorg.com:8081/artifactory/plugins-release'
}
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.1.5.RELEASE")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'spring-boot'
jar {
baseName = 'log4jOwn'
}
repositories {
maven {
url 'http://artifactory.myorg.com:8081/artifactory/plugins-release'
}
}
dependencies {
compile 'org.codehaus.groovy:groovy-all:2.3.3'
compile 'org.springframework.boot:spring-boot-starter-web'
testCompile("junit:junit")
}
task wrapper(type: Wrapper) {
gradleVersion = '1.11'
}
Run Code Online (Sandbox Code Playgroud)
Dav*_*yer 15
您具有spring-boot-starter-web直接依赖性,并且它不使用log4j进行日志记录,因此log4j不在您的类路径中.您需要排除spring-boot-starter-logging和包含spring-boot-starter-log4j(例如https://github.com/spring-projects/spring-boot/blob/master/spring-boot-samples/spring-boot-sample-actuator-log4j/pom.xml# L36 - 那是Maven,但如果你知道Gradle你可以弄清楚如何做同样的事情).
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j</artifactId>
</dependency>
Run Code Online (Sandbox Code Playgroud)
Gradle当量:
dependencies {
compile 'org.codehaus.groovy:groovy'
compile ('org.springframework.boot:spring-boot-starter-web'){
exclude module: 'org.springframework.boot:spring-boot-starter-logging'
}
compile ('org.springframework.boot:spring-boot-starter-log4j')
}
Run Code Online (Sandbox Code Playgroud)
(感谢任何人将其作为编辑发布.)
| 归档时间: |
|
| 查看次数: |
18313 次 |
| 最近记录: |