配置Java FileHandler日志记录以创建不存在的目录 [英] Configuring Java FileHandler Logging to create directories if they do not exist

查看:209
本文介绍了配置Java FileHandler日志记录以创建不存在的目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试配置 Java记录 API的 FileHandler 将我的服务器记录到我的主目录中的文件夹中的文件,但我不想在它运行的每台机器上创建这些目录。

I'm trying to configure the Java Logging API's FileHandler to log my server to a file within a folder in my home directory, but I don't want to have to create those directories on every machine it's running.

例如,在logging.properties文件中,我指定:

For example in the logging.properties file I specify:

java.util.logging.FileHandler
java.util.logging.FileHandler.pattern=%h/app-logs/MyApplication/MyApplication_%u-%g.log



<这将允许我在MyApplication的主目录(%h)中收集日志并将它们旋转(使用%u和%g变量)。

This would allow me to collect logs in my home directory (%h) for MyApplication and would rotate them (using the %u, and %g variables).

当我在log4j.properties中指定时,Log4j支持这一点:

Log4j supports this when I specify in my log4j.properties:

log4j.appender.rolling.File=${user.home}/app-logs/MyApplication-log4j/MyApplication.log

看起来Logging FileHandler存在一个错误:
错误6244047:无法指定driectorys记录FileHandler,除非它们存在

It looks like there is a bug against the Logging FileHandler: Bug 6244047: impossible to specify driectorys to logging FileHandler unless they exist

听起来他们不打算修复它或暴露任何属性来解决这个问题(除了让你的应用程序解析logging.properties或硬代码所需的路径):

It sounds like they don't plan on fixing it or exposing any properties to work around the issue (beyond having your application parse the logging.properties or hard code the path needed):


看起来
java.util.logging.FileHandler没有
期望指定的目录
可能不存在。通常情况下,它必须以
检查这种情况。另外,
也必须检查编写
权限的目录。另一个问题
是如果其中一个检查
没有通过该怎么办。

It looks like the java.util.logging.FileHandler does not expect that the specified directory may not exist. Normally, it has to check this condition anyway. Also, it has to check the directory writing permissions as well. Another question is what to do if one of these check does not pass.

一种可能性是创建
缺少的目录如果
用户具有适当的权限,则在路径中。另一个
是抛出一个带有
清除消息的IOException错误。
后一种方法看起来更一致。

One possibility is to create the missing directories in the path if the user has proper permissions. Another is to throw an IOException with a clear message what is wrong. The latter approach looks more consistent.


推荐答案

看起来像log4j 1.2版.15这样做。

It seems like log4j version 1.2.15 does it.

以下是执行此操作的代码片段

Here is the snippet of the code which does it

public
 synchronized
 void setFile(String fileName, boolean append, boolean bufferedIO, int bufferSize)
                                                        throws IOException {
    LogLog.debug("setFile called: "+fileName+", "+append);

    // It does not make sense to have immediate flush and bufferedIO.
    if(bufferedIO) {
      setImmediateFlush(false);
    }

    reset();
    FileOutputStream ostream = null;
    try {
          //
          //   attempt to create file
          //
          ostream = new FileOutputStream(fileName, append);
    } catch(FileNotFoundException ex) {
          //
          //   if parent directory does not exist then
          //      attempt to create it and try to create file
          //      see bug 9150
          //
          String parentName = new File(fileName).getParent();
          if (parentName != null) {
             File parentDir = new File(parentName);
             if(!parentDir.exists() && parentDir.mkdirs()) {
                ostream = new FileOutputStream(fileName, append);
             } else {
                throw ex;
             }
          } else {
             throw ex;
          }
    }
    Writer fw = createWriter(ostream);
    if(bufferedIO) {
      fw = new BufferedWriter(fw, bufferSize);
    }
    this.setQWForFiles(fw);
    this.fileName = fileName;
    this.fileAppend = append;
    this.bufferedIO = bufferedIO;
    this.bufferSize = bufferSize;
    writeHeader();
    LogLog.debug("setFile ended");
}

这段代码来自FileAppender,RollingFileAppender扩展了FileAppender。

This piece of code is from FileAppender, RollingFileAppender extends FileAppender.

这里没有检查我们是否有权创建父文件夹,但如果父文件夹不存在,那么它将尝试创建父文件夹。

Here it is not checking whether we have permission to create the parent folders, but if the parent folders is not existing then it will try to create the parent folders.

EDITED

如果你想要一些额外的功能,你可以随时扩展RollingFileAppender并覆盖setFile()方法。

If you want some additional functionalily, you can always extend RollingFileAppender and override the setFile() method.

这篇关于配置Java FileHandler日志记录以创建不存在的目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆