1

I want to maximise the logging from JavaFX.

I've found and set this as a flag for the JVM: -Djavafx.pulseLogger=true (which produces a lot of information)

I'm trying to use log4j:

Are the options below valid? They don't seem to produce anything in my output file?

<logger name="com.sun.javafx">
    <level value="info" />
</logger>

<logger name="javafx">
    <level value="info" />
</logger>
NottmTony
  • 447
  • 1
  • 6
  • 28
  • 1
    JavaFX doesn’t use Log4J, so Log4J settings won’t affect JavaFX’s logging. – VGR Nov 05 '18 at 16:25
  • Ah OK thanks - how do I log from JavaFX then? – NottmTony Nov 05 '18 at 16:26
  • 2
    Use LogManager.getLogManager().[updateConfiguration](https://docs.oracle.com/en/java/javase/11/docs/api/java.logging/java/util/logging/LogManager.html#updateConfiguration(java.io.InputStream,java.util.function.Function)) to read a properties file with the overriding logging confuguration. Note that [INFO](https://docs.oracle.com/en/java/javase/11/docs/api/java.logging/java/util/logging/Level.html#INFO) is already the default logging level. If you want a lower level, you need to configure both the Logger and each of the root Logger’s Handlers. – VGR Nov 05 '18 at 16:41
  • Thank you I will try this. Place as answer and I'll accept it. – NottmTony Nov 06 '18 at 08:27

2 Answers2

2

JavaFX, like all of Java SE, does not use Log4J, but instead uses the Java logging system. Log4J configuration will not affect JavaFX’s logging.

You can use LogManager.getLogManager().updateConfiguration to read a properties file with the overriding logging configuration:

try (InputStream loggingProperties =
    MyApplication.class.getResource("logging.properties")) {

    LogManager.getLogManager().updateConfiguration(loggingProperties);
}

(Note that readConfiguration replaces the entire logging configuration, whereas updateConfiguration amends your customizations to the configuration.)

If you just want to change logging for JavaFX, you can do something like this instead:

private static final Logger javafxLogger = Logger.getLogger("javafx");
static {
    javafxLogger.setLevel(Level.FINEST);
}

The Logger needs to be retained in a field, or it will be garbage collected (quickly) and the customization will be lost.

Regardless of which of the above approaches you use, setting the level of the Logger is not enough. Handlers also have their own levels, and they too are set to Level.INFO by default.

The easiest way to address this is to set all of the Handlers’ levels:

for (Handler handler : Logger.getLogger("").getHandlers()) {
    handler.setLevel(Level.ALL);
}
VGR
  • 40,506
  • 4
  • 48
  • 63
1

Thanks to VGR's answer above, this is what I did to get javafx logging out at a detailed level:

private static final java.util.logging.Logger javafxLogger = 
    java.util.logging.Logger.getLogger("javafx");   

javafxLogger.setLevel(Level.FINEST);

FileHandler fh = new FileHandler("MyJavaFX.log", (1048576 * 30), 1000);

fh.setFormatter(new SimpleFormatter());
javafxLogger.addHandler(fh); 
NottmTony
  • 447
  • 1
  • 6
  • 28