我一直在尝试使用 Log4j 2.6 (属性文件)和 Spring Boot 1.3.5版本,并且Spring的默认日志记录开始了。我没有&# 39;能够找出确切的问题。
这是我的 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
}
这是我的 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
我已经在此查看了多个SO主题,似乎正确设置了所有属性。但有些我怎么也无法找出为什么它不按照我的配置记录到文件和控制台。
答案 0 :(得分:0)
如果您使用的是Spring Boot,则可以在application.properties
或application.yml
中配置日志记录。
yml配置
logging:
file: server.log
level:
'com.myapp': INFO
属性配置
logging.file=server.log
logging.level.com.myapp=INFO
请参阅http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-logging.html了解更多详情
答案 1 :(得分:0)
好像我错过了 application.properties 文件中的日志配置文件参数,因为检测到并加载了log4j没有任何错误但未使用通过spring-boot。
添加以下行解决了这个问题。
logging.config= # location of logging config file
[ P.S。]:通过以下方式找到我的问题的解决方案:log4j2.xml loaded but not applied [JVM argument]