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.
Aucun commentaire:
Enregistrer un commentaire