当前位置:  开发笔记 > 编程语言 > 正文

设置日志文件名以在Log4j中包含当前日期

如何解决《设置日志文件名以在Log4j中包含当前日期》经验,为你挑选了5个好方法。

我想将log4j和log4net appender的日志文件名设置为具有当前日期.我们正在进行每日翻转,但当前日志文件没有日期.日志文件名称格式为

logname.2008-10-10.log

有人知道我这样做的最好方法吗?

编辑:我忘了提到我们也想在log4net中这样做.此外,任何解决方案都需要在JBoss中使用.



1> gedevan..:

DailyRollingFileAppender是您正在搜索的内容.


    
    
     
      
    
  


这不会简单地创建一个名为"application.log"的日志文件,只将日期模式放在滚动的日志文件中吗?
因此,您将获得每天的单独日志文件.但是今天的日志文件将被命名为application.log,没有日期.在大多数情况下都可以.
从log4j docs:DailyRollingFileAppender被观察到表现出同步问题和数据丢失.(http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/DailyRollingFileAppender.html).你应该真的关注@shinds的答案

2> shinds..:

使用log4j.properties文件,并在我的POM中包含apache-log4j-extras 1.1和log4j 1.2.16

log4j.appender.LOGFILE=org.apache.log4j.rolling.RollingFileAppender
log4j.appender.LOGFILE.RollingPolicy=org.apache.log4j.rolling.TimeBasedRollingPolicy
log4j.appender.LOGFILE.RollingPolicy.FileNamePattern=/logs/application_%d{yyyy-MM-dd}.log


^ --- ^专业级别:99`log4j.appender.out.RollingPolicy.FileNamePattern = logs /%d {yyyy-MM-dd HH-mm-ss} cron-script-x/out.log log4j.appender.err .RollingPolicy.FileNamePattern = logs /%d {yyyy-MM-dd HH-mm-ss} cron-script-x/err.log`

3> matt b..:

我99%确定RollingFileAppender/DailyRollingFileAppender,虽然它为您提供了所需的日期滚动功能,但没有任何方法可以指定当前日志文件也应该使用它DatePattern.

您可能只是简单地子类化RollingFileAppender(或DailyRollingFileAppender,我忘记了log4net中的哪个)并修改命名逻辑.



4> James A. N. ..:

我创建了一个可以做到这一点的追加器. http://stauffer.james.googlepages.com/DateFormatFileAppender.java

/*
 * Copyright (C) The Apache Software Foundation. All rights reserved.
 *
 * This software is published under the terms of the Apache Software
 * License version 1.1, a copy of which has been included with this
 * distribution in the LICENSE.txt file.  */

package sps.log.log4j;

import java.io.IOException;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.log4j.*;
import org.apache.log4j.helpers.LogLog;
import org.apache.log4j.spi.LoggingEvent;

/**
 * DateFormatFileAppender is a log4j Appender and extends 
 * {@link FileAppender} so each log is 
 * named based on a date format defined in the File property.
 *
 * Sample File: 'logs/'yyyy/MM-MMM/dd-EEE/HH-mm-ss-S'.log'
 * Makes a file like: logs/2004/04-Apr/13-Tue/09-45-15-937.log
 * @author James Stauffer
 */
public class DateFormatFileAppender extends FileAppender {

  /**
   * The default constructor does nothing.
   */
  public DateFormatFileAppender() {
  }

  /**
   * Instantiate a DailyRollingFileAppender and open the
   * file designated by filename. The opened filename will
   * become the ouput destination for this appender.
   */
  public DateFormatFileAppender (Layout layout, String filename) throws IOException {
    super(layout, filename, true);
  }

  private String fileBackup;//Saves the file pattern
  private boolean separate = false;

  public void setFile(String file) {
    super.setFile(file);
    this.fileBackup = getFile();
  }

  /**
   * If true each LoggingEvent causes that file to close and open.
   * This is useful when the file is a pattern that would often
   * produce a different filename.
   */
  public void setSeparate(boolean separate) {
    this.separate = separate;
  }

  protected void subAppend(LoggingEvent event) {
    if(separate) {
        try {//First reset the file so each new log gets a new file.
            setFile(getFile(), getAppend(), getBufferedIO(), getBufferSize());
        } catch(IOException e) {
            LogLog.error("Unable to reset fileName.");
        }
    }
    super.subAppend(event);
  }


  public
  synchronized
  void setFile(String fileName, boolean append, boolean bufferedIO, int bufferSize)
                                                            throws IOException {
    SimpleDateFormat sdf = new SimpleDateFormat(fileBackup);
    String actualFileName = sdf.format(new Date());
    makeDirs(actualFileName);
    super.setFile(actualFileName, append, bufferedIO, bufferSize);
  }

  /**
   * Ensures that all of the directories for the given path exist.
   * Anything after the last / or \ is assumed to be a filename.
   */
  private void makeDirs (String path) {
    int indexSlash = path.lastIndexOf("/");
    int indexBackSlash = path.lastIndexOf("\\");
    int index = Math.max(indexSlash, indexBackSlash);
    if(index > 0) {
        String dirs = path.substring(0, index);
//        LogLog.debug("Making " + dirs);
        File dir = new File(dirs);
        if(!dir.exists()) {
            boolean success = dir.mkdirs();
            if(!success) {
                LogLog.error("Unable to create directories for " + dirs);
            }
        }
    }
  }

}



5> Lars Corneli..:

我不知道在Java中是否可行,但在.NET中,RollingFileAppender上的属性StaticLogFileName可以为您提供所需的内容.默认值为true.


完整配置:


  
  
  
  
  
  
    
  

".log" 是为了不让dateformat在日志中识别全局日期模式'g'.

推荐阅读
wangtao
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有