Just adding SLF4J dependency made my Swing app dead slow.
Before proper logging was implemented:
Earlier I was using java.util.Logger to output the logs, and some of them were even using System.out.println.
Getting Log4j2 / Slf4j setup
Then I added log4j2 maven dependencies and log4j2.xml in src/main/resources and then it was not picking up the settings as I was still writing to java.util.Logger.
I found out that I have to get Logger from log4j LogManager, or to make it more configurable get a Logger from slf4j LoggerFactory so that client can plugin any Logger implementation later.
So, I started getting Logger from slf4j LoggerFactory and logging to it. With the log4j2 dependencies in place and log4j2.xml in resources folder, all is well, and only the packages which I want to set to DEBUG level are getting logged.
**The performance issue **
All is well - except for performance! My app uses 125% CPU, and this causes Macbook Pro to start heating. MBP has an issue where once it starts heating kernel_task hits 500% CPU within couple of minutes.
Long story short - once I fixed my logging, my app has become unusable.
Removed even log4j2 from classpath
Not only that, I even removed log4j2 dependencies, and it stopped logging. Slf4j alone remains and I am writing to the Logger which I get from slf4j LoggerFactory. Still my app hits 125% CPU and the MBP kernel_task does the rest to freeze the system.
What could be the problem here? Memory leak in Slf4j? Does Java Swing write too much to Slf4j?
Sequence of trials I am planning next:
- Upgrade slf4j to latest and greatest version (I can check which one works best with Java 1.8 which the app is using)
- Avoid slf4j and write directly to Log4j2
- Revert to java.util.Logger
Update:
This was the log4j2.xml which I have. (But note: I removed log4j2 dependency and nothing is logged now.)
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn" name="tradez" packages="">
<Appenders>
<Console name="app_stdout" target="SYSTEM_OUT">
<!-- PatternLayout pattern="