java获取系统运行日志文件,使用Java 9 JDK Logging在运行时查找日志文件名

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.

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.

Here's my code:

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;

}

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

Any ideas? Thanks.

解决方案

There are several ways to do this:

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).

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).

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.

Use a different logging framework.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值