SpringBoot中的Log4j [英] Log4j in SpringBoot

查看:287
本文介绍了SpringBoot中的Log4j的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是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

我的示例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 $ b $ class HelloGroovy {

静态记录器perfLog = Logger.getLogger(perfLog)

@RequestMapping(/ logger)
字符串记录器(){
log.info创建了一个名为标识符的新项目
log.error创建了一个新的item命名标识符
log.warn创建了一个名为identifier的新项目

System.out.println(Test)
perfLog.trace(Test)
返回Logger Called。



static main(args){

SpringApplication.run(this,args)
}




$ b $ p
$ b

所有get都是控制台中的前3行打印,然后是Test,post我没有在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'
}

存储库{
maven {
url'http:// artifactory。 myorg.com: 8081 / artifactory / plugins-release'
}
}

依赖项{
compile'org.codehaus.groovy:groovy-all:2.3.3'
编译'org.springframework.boot:spring-boot-starter-web'

testCompile(junit:junit)
}

任务包装器类型:Wrapper){
gradleVersion ='1.11'
}


解决你有 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你可以找出如何做同样的事情)。

 <依赖关系> 
< groupId> org.springframework.boot< / groupId>
< artifactId> spring-boot-starter-actuator< / artifactId>
<排除项>
<排除>
< groupId> org.springframework.boot< / groupId>
< artifactId> spring-boot-starter-logging< / artifactId>
< /排除>
< /排除>
< /依赖关系>
< dependency>
< groupId> org.springframework.boot< / groupId>
< artifactId> spring-boot-starter-log4j< / artifactId>
< /依赖关系>

等价于Gradle:

 依赖关系{
compile'org.codehaus.groovy:groovy'
compile('org.springframework.boot:spring-boot-starter-web'){
exclude模块:'org.springframework.boot:spring-boot-starter-logging'
}
compile('org.springframework.boot:spring-boot-starter-log4j')
}

(感谢无论谁发布该文件作为编辑)。

I'm newbie to Spring Boot and working on a simple log4j demo using Spring Boot. I used the gradle project and have spring-boot-starter-web and groovy dependencies. Below is my log4j.properties file content. All I need is , when i execute the main program and use annotation @Log4J i must be able to save the log.perflog to a file in my local (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

My sample groovy Class:

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

}

All get is the first 3 lines print in the console and then "Test" , post that nothing shows up in the file i have mentioned in the log4j.properties.

My build.gradle file

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

解决方案

You have spring-boot-starter-web as a direct dependency and it doesn't use log4j for logging so log4j is not on your classpath. You would need to exclude spring-boot-starter-logging and include spring-boot-starter-log4j (like here https://github.com/spring-projects/spring-boot/blob/master/spring-boot-samples/spring-boot-sample-actuator-log4j/pom.xml#L36 - that's Maven but if you know Gradle you can figure out how to do the same thing).

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

Gradle equivalent:

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

(Thanks to whoever posted that as an edit.)

这篇关于SpringBoot中的Log4j的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆