日志文件不在log4j中生成

时间:2017-02-20 06:53:12

标签: java eclipse logging log4j

我是log4j的新手。我已经创建了一个实现log4j的示例java程序。

以下是java程序:

package logging;
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;
public class Logging {
    /**
     * @param args the command line arguments
     */
    private static Logger logger = Logger.getLogger(Logging.class);
    public static void main(String[] args) {
        BasicConfigurator.configure();

         logger.trace("This is a Trace");
         logger.debug("This is a Debug");
         logger.info("This is an Info");
         logger.warn("This is a Warn");
         logger.error("This is an Error");
         logger.fatal("This is a Fatal");

    }

}

我在控制台屏幕中获取输出。但 日志文件不是 生成。我还使用以下链接在Eclipse neon中配置了我的项目: Configuration

我做的一切都很好。但是日志文件没有生成。当我以编程方式实现log4j时,文件生成了。以下是我的属性文件:

#root

log4j.logger.com.apress.logging.log4j=debug,dest

log4j.additivity.com.apress.logging.log4j=false

#define the appender
log4j.appender.dest = org.apache.log4j.DailyRollingFileAppender

#set the name of the file 
log4j.appender.dest.File=${user.home}/log.out

#setting the immediate flush to true (default) 
log4j.appender.dest.ImmediateFlush=true

#setting the threshold
log4j.appender.dest.Threshold=ERROR

#setting the append to false, overwrite 
log4j.appender.dest.Append=true

#set the DatePattern 
log4j.appender.dest.DatePattern='.' yyyy-MM-dd

将Log4J写入日志文件需要做什么?

2 个答案:

答案 0 :(得分:3)

确保log4j.properties位于默认包

# Root logger option
    log4j.rootLogger=DEBUG, stdout, file
    log4j.logger.infoLogger=DEBUG, infoLogger

    log4j.additivity.infoLogger = false
    # Redirect log messages to console
    log4j.appender.stdout=org.apache.log4j.ConsoleAppender
    log4j.appender.stdout.Target=System.out
    log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
    log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

    # Redirect log messages to a log file, support file rolling.
    log4j.appender.file=org.apache.log4j.RollingFileAppender
    log4j.appender.file.File=E:\\LOG\\ConvertorLogger.log
    log4j.appender.file.MaxFileSize=5MB
    log4j.appender.file.MaxBackupIndex=10
    log4j.appender.file.layout=org.apache.log4j.PatternLayout
    log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

    # Redirect log messages to a log file, support file rolling.
    log4j.appender.infoLogger=org.apache.log4j.RollingFileAppender
    log4j.appender.infoLogger.File=E:\\LOG\\ConvertorInfoLogger.log
    log4j.appender.infoLogger.MaxFileSize=5MB
    log4j.appender.infoLogger.MaxBackupIndex=10
    log4j.appender.infoLogger.layout=org.apache.log4j.PatternLayout
    log4j.appender.infoLogger.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

答案 1 :(得分:0)

您可以参考此文件:log4j.properties

log4j.rootLogger=WARN,stdout,R
  log4j.appender.stdout=org.apache.log4j.ConsoleAppender
  log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
  log4j.appender.stdout.layout.ConversionPattern=%d [%t] %-5p %c -%m%n
  log4j.appender.R=org.apache.log4j.RollingFileAppender
  log4j.appender.R.File=${user.home}/log.out
  log4j.appender.MaxFileSize=100KB
  log4j.appender.R.MaxBackupIndex=1
  log4j.appender.R.layout=org.apache.log4j.PatternLayout
  log4j.appender.R.layout.ConversionPattern = %p %t %c -%m%n
  log4j.logger.com.foo=WARN