JFormDesigner / FlatLaf

FlatLaf - Swing Look and Feel (with Darcula/IntelliJ themes support)
https://www.formdev.com/flatlaf/
Apache License 2.0
3.34k stars 266 forks source link

Best way to disable FlatLaf logging? #476

Closed clausnagel closed 2 years ago

clausnagel commented 2 years ago

I'd like to suppress FlatLaf logging messages. What's the recommended way to do so? Is there a global option that I can set programmatically?

sekizou82 commented 2 years ago

There is the implementation of logging. https://github.com/JFormDesigner/FlatLaf/blob/main/flatlaf-core/src/main/java/com/formdev/flatlaf/util/LoggingFacadeImpl.java

clausnagel commented 2 years ago

Thanks, @Uguisu32J, I am aware of this class.

I was trying to use the following line of code early in the startup of my application:

Logger.getLogger(FlatLaf.class.getName()).setLevel(Level.OFF);

However, somehow this setting seems to be overriden once the LoggingFacadeImpl instance is created in LoggingFacade.java (see here) and log messages were still printed. I therefore changed my code to force the instantiation of LoggingFacadeImpl:

LoggingFacade facade = LoggingFacade.INSTANCE;
Logger.getLogger(FlatLaf.class.getName()).setLevel(Level.OFF);

It works, but I don't like the extra line of code and the implicit side effect.

I think, offering a setLevel method for LoggingFacade would be a nicer solution.

DevCharly commented 2 years ago

I wonder why you want disable FlatLaf logging, because FlatLaf does not log anything in normal operation. Only on errors.

clausnagel commented 2 years ago

I am using the method hasFound() of FlatSVGIcon to check whether a given icon can be loaded.

FlatSVGIcon icon = new FlatSVGIcon(...);
if (!icon.hasFound()) {
   // ... take some action ...
}

In case the SVG icon resource is null, the hasFound() method logs:

FlatSVGIcon: resource 'name' not found (if using Java modules, check whether icon package is opened in module-info.java)

I want to suppress this message. I'd prefer that hasFound() just returns true or false. I can then take proper actions (including log messages) in my code.

Some more background info: The app I'm working on offers a plugin mechanism. Plugin developers can optionally provide a SVG logo in the plugin JAR that is displayed in my app in an overview panel. But they can also choose to not provide a logo in which case a default logo is shown.

So, the SVG resource provided to the FlatSVGIcon constructor might be null in case no logo is provided by the plugin. That's why I use the hasFound() method as check. A null logo is therefore no error in my scenario, and for this reason I want to suppress the FlatLaf error message.

Of course, I could also check the SVG resource in my code. But the hasFound method (and the invoked update() method) performs more logic than just checking for null - for instance, it checks whether a dark theme is active and then automatically checks for another icon with a _dark suffix in the filename. I also want to support plugin logos for dark themes. But I don't want to duplicate all these checks in my code but just rely on the hasFound() method.

I hope this makes my case clearer. Maybe there is even another and better way to implement my scenario?

DevCharly commented 2 years ago

Thanks for the info.

I've changed the logging in FlatSVGIcon when icon resource is not found from "severe" to "config". So nothing is logged (by default) if the icon resource is not found.

No need to disable logging anymore 😉