配置log4j2没有创建日志文件?

ezykj2lf  于 2021-07-08  发布在  Java
关注(0)|答案(0)|浏览(332)

最近我从log4j1.x转到了2.x,每件事都发生了变化,我现在不知道如何记录。
我已经配置了属性,但是没有创建文件。有什么需要帮忙的吗?谢谢
如果需要,我会张贴我的旧配置。知道我哪里做错了吗
log4jtest.java文件

package com;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.config.Configuration;
import org.apache.logging.log4j.core.config.ConfigurationSource;
import org.apache.logging.log4j.core.config.Configurator;
import org.apache.logging.log4j.core.config.properties.PropertiesConfigurationBuilder;

public class Log4jTest {
    public static Logger logger = LogManager.getLogger(Log4jTest.class);
    public static void main(String args[]) {
    Properties props = new Properties();
          File loggerFile = new File("C:\\Users\\207596\\TestRuns\\src\\Samples\\log4j2.properties");
          try {
            InputStream logInputStream = new FileInputStream(loggerFile);
            props.load(logInputStream);
        } catch (IOException e1) {
            e1.printStackTrace();
        }
          LoggerContext context = (LoggerContext)LogManager.getContext(false);
          Configuration config = new PropertiesConfigurationBuilder()
                      .setConfigurationSource(ConfigurationSource.NULL_SOURCE)
                      .setRootProperties(props)
                      .setLoggerContext(context)
                      .build();
           context.setConfiguration(config);
           Configurator.initialize(config);

           logger.info("file has been configured");
    }

}

log4j2.properties属性

status = error
dest = err
name = PropertiesConfig

property.filename =C:\Users\207596\Desktop\Test.log

filter.threshold.type = ThresholdFilter
filter.threshold.level = info

appender.console.type = Console
appender.console.name = STDOUT
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = %m%n
appender.console.filter.threshold.type = ThresholdFilter
appender.console.filter.threshold.level = info

appender.rolling.type = RollingFile
appender.rolling.name = RollingFile
appender.rolling.fileName = ${filename}
appender.rolling.filePattern = C:\Users\207596\Desktop\test1-%d{MM-dd-yy-HH-mm-ss}-%i.log.gz
appender.rolling.layout.type = PatternLayout
appender.rolling.layout.pattern = %d{yyyy:dd:MM hh:mm:ss} %-5p [%c] - %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=2MB
appender.rolling.strategy.type = DefaultRolloverStrategy
appender.rolling.strategy.max = 5

logger.rolling.name = com.Log4jTest
logger.rolling.level = info
logger.rolling.additivity = false
logger.rolling.appenderRef.rolling.ref = RollingFile

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

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题