虽然创建了空文件,但无法将任何包含写入日志文件

时间:2017-08-21 10:35:36

标签: log4j log4j2

使用以下配置将消息记录到控制台和文件。我只能看到控制台输出。虽然创建了文件,但它没有任何包含

status = error
name = PropertiesConfig

property.filename = C:/target/rolling/rollingtest.log

filter.threshold.type = ThresholdFilter
filter.threshold.level = debug

appender.console.type = Console
appender.console.name = STDOUT
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = %d{yyyy-MM-dd'T'hh:mm:ss.SSSXXX} %-5p [%t] %x %c %M - %m%n

appender.rolling.type = RollingFile
appender.rolling.name = RollingFile
appender.rolling.fileName = ${filename}
appender.rolling.filePattern = C:/target/rolling2/test1-%d{MM-dd-yy-HH-mm-ss}-%i.log.gz
appender.rolling.layout.type = PatternLayout
appender.rolling.layout.pattern = %d{yyyy-MM-dd'T'hh:mm:ss.SSSXXX} %-5p [%t] %x %c %M - %m%n
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=1KB
appender.rolling.strategy.type = DefaultRolloverStrategy
appender.rolling.strategy.max = 5

logger.rolling.name = com.example.my.app
logger.rolling.level = debug
logger.rolling.additivity = false
logger.rolling.appenderRef.rolling.ref = RollingFile

rootLogger.level = info
rootLogger.appenderRef.stdout.ref = STDOUT

1 个答案:

答案 0 :(得分:1)

我已经测试了你的属性,它们对我来说似乎没问题。请考虑以下事项:

  • 要将邮件捕获到文件,您需要使用代码生成debug条消息
  • 要捕获消息到文件,必须从具有从com.example.my.app开始的完全限定名称的类生成日志消息

以下是有效的例子:

package com.example.my.app;


import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class Main {

    Logger logger = LogManager.getLogger();

    Logger getLogger(){
        return logger;
    }

    public static void main(String[] args) {
        Main mainObj = new Main();
        mainObj.getLogger().debug("test debig");
    }

}