简体   繁体   English

使用Spring Boot 1.3.5(gradle)的Log4j 2.6(属性文件)不起作用

[英]Log4j 2.6 (properties file) with Spring Boot 1.3.5 (gradle) not working

Ive been trying to use Log4j 2.6 (properties file) with Spring Boot 1.3.5 version and the Spring's default logging kicks in. I haven't been able to trace out the exact issue. 我一直在尝试将Log4j 2.6 (属性文件)与Spring Boot 1.3.5版本一起使用 ,并且Spring的默认日志记录开始了。我一直无法找出确切的问题。

Here is my build.gradle file: 这是我的build.gradle文件:

buildscript {
    repositories {
        maven {
            url "https://plugins.gradle.org/m2/"
        }
    }
    dependencies {
        classpath "org.jfrog.buildinfo:build-info-extractor-gradle:3.1.1"
        classpath 'org.ajoberstar:gradle-git:1.3.0-milestone.1'
        classpath("org.springframework.boot:spring-boot-gradle-plugin:1.3.3.RELEASE") 
    }
}

...

dependencies {
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-web', version: '1.3.5.RELEASE'
    providedRuntime group: 'org.springframework.boot', name: 'spring-boot-starter-tomcat', version: '1.3.5.RELEASE'

    compile group: 'org.springframework.boot', name: 'spring-boot-starter-actuator', version: '1.3.5.RELEASE'
    compile group: 'org.apache.kafka', name: 'kafka-clients', version: '0.10.1.0'
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-log4j2', version: '1.3.5.RELEASE'

    compile group: 'org.apache.logging.log4j', name: 'log4j-api', version: '2.6'
    compile group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.6'

    testCompile group: 'org.testng', name: 'testng', version: '6.8.8'
    testCompile group: 'org.json', name: 'json', version: '20160212'
    testCompile 'org.glassfish.jersey.core:jersey-client:2.22.2'
    testCompile group: 'org.springframework.boot', name: 'spring-boot-starter-test', version: '1.3.5.RELEASE'
    testCompile group: 'org.apache.kafka', name: 'kafka_2.11', version: '0.10.1.0'
}

configurations {
    all*.exclude module: 'spring-boot-starter-logging'
    providedRuntime
}

This is my log4j2.properties file: 这是我的log4j2.properties文件:

name = PropertiesConfig

property.logDir = logs
property.filename = ingestion
property.pattern = %d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

appenders = console, rolling

appender.console.type = Console
appender.console.name = STDOUT
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = ${pattern}

appender.rolling.type = RollingFile
appender.rolling.name = RollingFile
appender.rolling.fileName = ${logDir}/${filename}.log
appender.rolling.filePattern = ${logDir}/${filename}.%d{yyyy-MM-dd}.log.gz
appender.rolling.layout.type = PatternLayout
appender.rolling.layout.pattern = ${pattern}
appender.rolling.policies.type = Policies
appender.rolling.policies.time.type = TimeBasedTriggeringPolicy
appender.rolling.policies.time.interval = 2
appender.rolling.policies.time.modulate = true
appender.rolling.policies.size.type = SizeBasedTriggeringPolicy
appender.rolling.policies.size.size=100MB
appender.rolling.strategy.type = DefaultRolloverStrategy
appender.rolling.strategy.max = 5

loggers = rolling

logger.rolling.name = com.dir.mysubdir
logger.rolling.level = WARN
#logger.rolling.additivity = false
logger.rolling.appenderRefs = rolling
logger.rolling.appenderRef.rolling.ref = RollingFile

# Root logger option
rootLogger.level = WARN
rootLogger.appenderRefs = stdout
rootLogger.appenderRef.stdout.ref = STDOUT

I've looked into multiple SO topics on this and seem to have all the properties set correctly. 我研究了多个SO主题,并且似乎正确设置了所有属性。 But some how i'm not able to find out why it isn't logging to file and console as per my configuration. 但是有些我无法找出为什么它没有按照我的配置记录到文件和控制台。

An empty log file logs/ingestion.log as configured in the log4j2.properties file gets created when I bootRun the application. 引导运行应用程序时,将创建在log4j2.properties文件中配置的日志文件logs / ingestion.log。

If you are using Spring Boot then you can configure logging in application.properties or application.yml . 如果您使用的是Spring Boot,则可以在application.propertiesapplication.yml配置日志记录。

yml configuration yml配置

logging:
    file: server.log
    level:
         'com.myapp': INFO

properties configuration 属性配置

logging.file=server.log
logging.level.com.myapp=INFO

refer http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-logging.html for more details 有关更多详细信息,请参见http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-logging.html

Itseems like I had missed out including the logging config file parameter in application.properties file, because of which the log4j was detected and loaded without any error but not used by spring-boot. 好像我错过了在application.properties文件中包括日志记录配置文件参数一样,由于这个原因,检测到并装入了log4j时没有任何错误,但是spring-boot并未使用它。
Adding the below line solved the issue. 添加以下行即可解决此问题。

logging.config= # location of logging config file

[ PS ]: Found the solution to my problem from this: log4j2.xml loaded but not applied [JVM argument] [ PS ]:从此找到了我的问题的解决方案: log4j2.xml已加载但未应用[JVM参数]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM