使用Java 9 JDK Logging在运行时查找日志文件名 [英] Find log file name at run time with Java 9 JDK Logging

查看:85
本文介绍了使用Java 9 JDK Logging在运行时查找日志文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我与使用JDK Logging查找实际打开的日志文件相同的问题,除了我想使用Java 9支持的API.

I have the same question as Find actual opened logfile with JDK Logging, except that I want to use an API that's supported by Java 9.

在Java 8中,除了我查看实际的日志文件名而不是解析lockFileName之外,我目前使用与该问题的答案中所述几乎相同的反射技巧.

In Java 8, I currently use nearly the same reflection hack as described in the answer to that question except that I look at the actual log file name instead of parsing the lockFileName.

这是我的代码:

private static String logFileName = null;

public static String getLogFileName() {
    // determined on demand and cached so we only need to do all of this the first time.
    if (logFileName == null) {
        for (Handler handler : Logger.getLogger("").getHandlers()) {
            if (handler.getClass().isAssignableFrom(FileHandler.class)) {
                FileHandler fileHandler = (FileHandler) handler;
                try {
                    // FileHandler.files has private access, 
                    // so I'm going to resort to reflection to get the file name.
                    Field privateFilesField = fileHandler.getClass().getDeclaredField("files");
                    privateFilesField.setAccessible(true); // allow access to this private field
                    File[] files = (File[]) privateFilesField.get(fileHandler);
                    logFileName = files[0].getCanonicalPath();
                    break;
                } catch (NullPointerException | NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException | IOException ex) {
                    logger.log(Level.SEVERE, "Unable to determine log file name.", ex);
                }
            }
        }
        if (logFileName == null) {
            logFileName = "your home directory"; // just be sure it's not null
            logger.warning("Unable to identify log file name.");
        }
    }
    return logFileName;
}

Reflection hack在Java 8和Java中均能正常工作. 9,但是在Java 9中,它会生成以下警告,我更喜欢使用--illegal-access = permit标志来修复而不是忽略.

The reflection hack works fine in both Java 8 & 9, but in Java 9, it generates the following warning which I'd prefer to fix rather than ignore with the --illegal-access=permit flag.

WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by org.foo.MyApp (file:/C:/Users/me/Documents/foo/build/classes/java/main/) to field java.util.logging.FileHandler.files
WARNING: Please consider reporting this to the maintainers of org.foo.MyApp
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release

有什么想法吗?谢谢.

推荐答案

有几种方法可以做到这一点:

There are several ways to do this:

  1. 向JDK提交更改请求以允许访问该字段(至少将其设置为protected,以便您可以在子类中读取它).
  2. 解析用于配置附加程序的属性文件.您将不得不从Java 9代码中复制一些逻辑,但是很可能该代码不会长时间更改(向后兼容).
  3. 从日志记录API中复制大量代码,以便您可以编写自己的FileHandler来访问该字段.同时,您还可以添加代码以迭代所有追加程序.
  4. 使用其他日志记录框架.
  1. File a change request with the JDK to allow access to the field (at least make it protected so you can read it in a subclass).
  2. Parse the properties file used to configure the appender. You will have to copy some of the logic out of the Java 9 code but chances are that this code won't change for a long time (backwards compatibility).
  3. Copy a lot of code out of the logging API so you can write your own FileHandler which allows access to the field. While at it, you could also add code to iterate over all appenders.
  4. Use a different logging framework.

这篇关于使用Java 9 JDK Logging在运行时查找日志文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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