使用log4j不生成日志文件

时间:2018-05-21 15:33:59

标签: java maven log4j log4j2

项目结构 -

enter image description here

SystemFiler.java

package custom;

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

public class SystemFiler {
    private static final Logger logLogger = LogManager.getLogger(SystemFiler.class);

    public static void main(String[] args) {
        for (int i = 0;i<10;i++) {              
            String logMsg = "testcase: "+i;            
            logLogger.info(logMsg);
        }
    }

}

log4j.properties

# Root logger option
log4j.rootLogger=INFO, stdout, file

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout.ConversionPattern=%d{ISO8601} %5p [%t] %c{1}:%L - %m%n
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.threshold=warn

log4j.appender.file=org.apache.log4j.DailyRollingFileAppender
log4j.appender.file.File=some_log_message.log
log4j.appender.file.DatePattern='.'yyyy-MM-dd
log4j.appender.file.Append=true
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{ISO8601} %5p [%t] %c{1}:%L - %m%n
log4j.appender.file.threshold=INFO

pom.xml

<dependencies>    
    <dependency>
      <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-api</artifactId>
        <version>2.7</version>
    </dependency>

     <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-1.2-api</artifactId>
        <version>2.7</version>
     </dependency>
     <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
        <version>2.7</version>
      </dependency>
   </dependencies>

1 个答案:

答案 0 :(得分:0)

给出appender文件名的完整路径,即 使用log4j.appender.file.File={catalina.home}/some_path/some_log_message.log