Log4j2未记录到滚动文件

时间:2018-07-17 12:15:34

标签: spring-boot log4j2

我使用此文件(log4j2.properties)配置log4j:

# log4j internal level
status                                  = error

# Name of the configuration
name                                    = Log4JPropertiesConfig

# optional properties
property.basePath                       = /home/reguser/Temporary/logs/
property.layoutPattern                  = %d{yyyy-MM-dd HH:mm:ss.SSS} %5p ${hostName} --- [%15.15t] %-40.40c{1.} : %m%n%ex

# appenders in use
appenders                               = console, rolling

# ?? filters                                 = threshold
# ?? filter.threshold.type                   = ThresholdFilter
# ?? filter.threshold.level                  = debug

# ###############################
# CONSOLE APPENDER
# ###############################
appender.console.type                   = Console
appender.console.name                   = consoleAppender
appender.console.layout.type            = PatternLayout
appender.console.layout.pattern         = ${layoutPattern}

# ###############################
# ROLLING FILE APPENDER
# ###############################
appender.rolling.type                   = RollingFile
appender.rolling.name                   = rollingAppender
appender.rolling.fileName               = ${basePath}falke-back-end.log
appender.rolling.filePattern            = ${basePath}falke-back-end_%d{yyyyMMdd-HHmmss}-%i.log.gz
appender.rolling.layout.type            = PatternLayout
appender.rolling.layout.pattern         = ${layoutPattern}
appender.rolling.policies.type          = Policies

appender.rolling.policies.time.type     = TimeBasedTriggeringPolicy
appender.rolling.policies.time.interval = 1
appender.rolling.policies.time.modulate = true
appender.rolling.strategy.type          = DefaultRolloverStrategy
appender.rolling.strategy.max           = 20
appender.rolling.policies.size.type     = SizeBasedTriggeringPolicy
appender.rolling.policies.size.size     = 20MB

# list of all loggers
loggers                                 = rolling

# ###############################
# CUSTOM LOGGER
# Mention package name here in place of example. Classes in this package or subpackages will use ConsoleAppender and RollingFileAppender for logging
# ###############################
logger.rolling.name                     = com.logtest
logger.rolling.level                    = error
logger.rolling.appenderRefs             = rolling
logger.rolling.appenderRef.rolling.ref  = rollingAppender
# inherit from rootLogger -> 'console' : true or false ?
logger.rolling.additivity               = false

# ###############################
# ROOT LOGGER
# Configure root logger for logging error logs in classes which are in package other than above specified package
# ###############################
rootLogger.level                        = error
rootLogger.appenderRefs                 = root
rootLogger.appenderRef.root.ref         = consoleAppender

测试类:

package com.logtest;

public class LogTest {

    public static void printLog() {
        org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(LogTest.class);
        log.trace( "slf4j printLog : trace" );
        log.debug( "slf4j printLog : debug" );
        log.info( "slf4j printLog : info" );
        log.error( "slf4j printLog : error" );


        org.apache.logging.log4j.Logger log4j = org.apache.logging.log4j.LogManager.getLogger(LogTest.class);
        log4j.trace( "log4j printLog : trace" );
        log4j.debug( "log4j printLog : debug" );
        log4j.info( "log4j printLog : info" );
        log4j.error( "log4j printLog : error" );

    }

}

POM的一部分:

<!-- *************************************** -->
<!-- Exclude Spring Boot's Default Logging -->
<!-- *************************************** -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
    <exclusions>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-logging</artifactId>
        </exclusion>
    </exclusions>
</dependency>


<!-- *************************************** -->
<!-- Log4j2 -->
<!-- *************************************** -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>

<!-- *************************************** -->
<!-- Needed for Async Logging with Log4j 2 -->
<!-- *************************************** -->
<dependency>
    <groupId>com.lmax</groupId>
    <artifactId>disruptor</artifactId>
    <version>3.4.0</version>
</dependency>

这是日志输出:

2018-07-17 15:08:47.199  WARN 13543 --- [nio-8080-exec-1] c.l.LogTest                              : slf4j printLog : warn
2018-07-17 15:08:47.199  INFO 13543 --- [nio-8080-exec-1] c.l.LogTest                              : slf4j printLog : info
2018-07-17 15:08:47.199 ERROR 13543 --- [nio-8080-exec-1] c.l.LogTest                              : slf4j printLog : error
2018-07-17 15:08:47.199  WARN 13543 --- [nio-8080-exec-1] c.l.LogTest                              : log4j printLog : warn
2018-07-17 15:08:47.199  INFO 13543 --- [nio-8080-exec-1] c.l.LogTest                              : log4j printLog : info
2018-07-17 15:08:47.200 ERROR 13543 --- [nio-8080-exec-1] c.l.LogTest                              : log4j printLog : error

更改日志级别以进行调试或错误无所谓,输出始终相同;仅打印警告信息和错误。

还创建了“〜/ Temporary / logs /”下的文件“ falke-back-end.log”,但它始终为空。

预期的行为是,“ rootLogger”应仅打印到控制台,而“ rolling”应仅打印到文件。

我在做什么错了?

0 个答案:

没有答案