Log4j FileAppender创建空文件

时间:2013-06-23 16:22:34

标签: java logging log4j fileappender

我在使用log4j进行文件记录时遇到问题。即使在网上搜索了很长时间之后,我也无法让它工作。

我的log4j.properties文件:

  log4j.rootLogger = DEBUG, FILE
  log4j.appender.FILE=org.apache.log4j.FileAppender
  log4j.appender.FILE.File=log.out
  log4j.appender.FILE.ImmediateFlush=true
  log4j.appender.FILE.Append=true
  log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
  log4j.appender.FILE.layout.conversionPattern=%m%n

我的java生产者类:

@Singleton
public class Loggers {

   @Produces
   public Logger getLogger(InjectionPoint ip) throws IOException {
    Member member = ip.getMember();
    Class<?> declaredInClass=member.getDeclaringClass();
    Logger logger=Logger.getLogger(declaredInClass.getSimpleName());
    Properties props = new Properties();
    props.load(Loggers.class.getClassLoader().getResourceAsStream(("log4j.properties")));
    PropertyConfigurator.configure(props);
    logger.info("should be in the file but it is only in console");
    return logger;
   }
}

它只是创建空文件并且不在那里登录的可能性有多大?但是,它会记录到控制台。

1 个答案:

答案 0 :(得分:0)

尝试设置记录器的级别:

logger.setLevel(Level.ALL);