0

I saw Java Logging - where is my log file?, but in my case I would like to find out in my Java application which logfile the FileHandler actually opened, i.e. if I start multiple processes and have a pattern set as

java.util.logging.FileHandler.pattern=MyApp.%g.%u.log

Then the %g and %u will be replaced with numbers depending on how many processes are running.

I can iterate over all logging Handlers and find the File-based one. But as far as I see there is no method in FileHandler to get the currently opened file.

Is there a way I can do this?

Community
  • 1
  • 1
centic
  • 15,565
  • 9
  • 68
  • 125
  • Related: [JDK-4798814 getFiles() needed for java.util.logging.FileHandler](https://bugs.openjdk.java.net/browse/JDK-4798814). – jmehrens Jun 15 '15 at 13:42

1 Answers1

2

Yes, you can break the private protection of the field using reflection. See this question: How do I read a private field in Java?

The next option is to override the class and write your own handler which exposes this field.

The last option is to use a different logging framework like logback or log4j2. Writing your own appender for them would be more simple than for JUL.

Community
  • 1
  • 1
Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
  • I actually used a combination of these, an extended FileHandler which actually needs to use the "lockName" as the original filename is not even available as member any more, so I ended up doing: Field f = FileHandler.class.getDeclaredField("lockFileName"); f.setAccessible(true); return StringUtils.removeEnd((String) f.get(this), ".lck"); – centic Jun 09 '15 at 14:07
  • I just posted a similar question, but I'm looking for a Java 9 solution that doesn't require reflection. See https://stackoverflow.com/questions/48697915/find-log-file-name-at-run-time-with-java-9-jdk-logging – David Feb 09 '18 at 02:37