1

for a project my project group and I have created a program with a somewhat complicated algorithm. In this algorithm we use a lot of logger.finest, logger.finer etc. When running the default IntelliJ Profiler (for windows) 'Java Flight Recorder' we get the following result (Percentage is amount of total time of execution spend in said functions):

enter image description here

There is a lot of string concatination going on for logging which isn't even saved to a file because the logging is off:

Level.OFF

The string concatination doesn't stop as it's just a parameter of the functions.

We've thought of multiple solutions which all have their flaws:

  1. Using an external (Helper) Method/Class that has an if statement for logging. Something like:
// Psuedo Code
public static void log(String msg_id) {
    if (should log) {
        log(msg);
    }
}

While this specific wouldn't work either way, maybe working with ID's or Callables could. The main drawback for this is that the logging references for classes and methods are lost. Every line will say the logging came from Helper.log

  1. Another thing we thought we could try is put if statements before every log line:
if(should log) {
    log("msg");
}

This, of course would work but would clutter all files and would be a pain to maintain, constantly asking someone to write ifs for very logging line.

  1. We could write a script which removes all log lines for when we want to run the program at full speed:

But this feels hands down ridiculous...


TLDR;

I want to find a clean and somewhat proper way to stop the string concatination when logging is off as it's a massive performance decrease. I've tried a seperate helper class, many ifs and external scripts but they don't feel like a proper solutions...

Kerwin Sneijders
  • 750
  • 13
  • 33
  • 6
    "*There is a lot of string concatination going on for logging*" **Why?** You shouldn't be using any concatenation directly. You should use [parameterized logging](http://www.slf4j.org/faq.html#logging_performance) – Michael Dec 09 '19 at 13:12
  • @Michael Wow. I've put some time in this question and the first comment is a perfect answer. Thanks. I wasn't able to find the question you marked as 'already answered here'. – Kerwin Sneijders Dec 09 '19 at 13:16
  • 1
    Don't worry. It's good to have signposts. I was able to work backwards because I knew the term "parameterized logging" - it's probably much harder to find that if you don't. – Michael Dec 09 '19 at 13:17
  • Hey @Michael, not sure if you'd read this but I'm still having problems with `.toString()` methods. They still run before the parameterization. I feel like an if statement in every `toString()` wouldn't be very nice but I don't see another option. Should I open a new thread? – Kerwin Sneijders Dec 11 '19 at 10:36
  • As in you're doing this `log.info("My log {}", someObject.toString())` ? – Michael Dec 11 '19 at 10:48
  • If so, you don't need to call `toString` explicitly. Just do `log.info("My log {}", someObject)` – Michael Dec 11 '19 at 10:49
  • No not really, it's a function which makes a list of objects and calls all objects `toString()` method. like: `private String getListAsString()` – Kerwin Sneijders Dec 11 '19 at 11:02
  • 1
    I think I maybe get what you're saying but I'm not certain and don't want to guess. Make another question and feel free to post the link here – Michael Dec 11 '19 at 11:04
  • Thanks, I've asked a new question here: https://stackoverflow.com/questions/59284520/java-string-parameterization-performace-loss-on-indirect-tostring-methods – Kerwin Sneijders Dec 11 '19 at 11:11

0 Answers0