简体   繁体   中英

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.

Here is my build.gradle file:

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:

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

If you are using Spring Boot then you can configure logging in application.properties or application.yml .

yml configuration

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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