2016-10-04 138 views
0

我想在沒有標準結構的maven項目中配置log4j2,但即使創建的文件沒有任何附加內容。Log4j2,沒有日誌文件

我在pom.xml以下依賴性

<dependency> 
     <groupId>org.slf4j</groupId> 
     <artifactId>slf4j-api</artifactId> 
     <version>${slf4j-version}</version> 
    </dependency> 

    <dependency> 
     <groupId>org.apache.logging.log4j</groupId> 
     <artifactId>log4j-slf4j-impl</artifactId> 
     <version>${log4j-version}</version> 
    </dependency> 

    <dependency> 
     <groupId>org.apache.logging.log4j</groupId> 
     <artifactId>log4j-api</artifactId> 
     <version>${log4j-version}</version> 
    </dependency> 

    <dependency> 
     <groupId>org.apache.logging.log4j</groupId> 
     <artifactId>log4j-core</artifactId> 
     <version>${log4j-version}</version> 
    </dependency> 

    <dependency> 
     <groupId>com.fasterxml.jackson.core</groupId> 
     <artifactId>jackson-core</artifactId> 
     <version>${jackson-version}</version> 
    </dependency> 

    <dependency> 
     <groupId>com.fasterxml.jackson.core</groupId> 
     <artifactId>jackson-databind</artifactId> 
     <version>${jackson-version}</version> 
    </dependency> 

我使用以下版本

<properties> 
     <slf4j-version>1.7.21</slf4j-version> 
     <log4j-version>2.6.2</log4j-version> 
     <jackson-version>2.8.2</jackson-version> 
    </properties> 

及以下log4j2.properties

status = info 
rootLogger.level = info 

name = PropertiesConfig 
property.fileName = ./log/log.txt 

appender.rolling.type = RollingFile 
appender.rolling.name = RollingFile 
appender.rolling.fileName = ${fileName} 
appender.rolling.filePattern = ./log/log_%d{dd-MM-yy}.txt 
appender.rolling.append = true 

appender.rolling.layout.type = JSONLayout 
appender.rolling.layout.complete = true 

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 = 5 

而且,因爲該項目的結構我正在設置這樣的屬性文件

LoggerContext context = (LoggerContext) LogManager.getContext(false); 
    File file = new File(CONFIGURATION_DIR + File.separator + "log4j2.properties"); 

    // this will force a reconfiguration 
    context.setConfigLocation(file.toURI()); 

即使文件被創建,也沒有任何內容被添加到文件中,並且我沒有錯誤。我錯過了什麼,或者做錯了什麼?

回答

1

它看起來像我缺少的log4j2.properties文件進行如下配置:

logger.rolling.name = name 
logger.rolling.level = debug 
logger.rolling.additivity = false 
logger.rolling.appenderRef.rolling.ref = RollingFile 

現在,它的工作原理。