How to log an exception with a message that has a parameter? Placeholder parameters are not possible?!

Hi, I want to log an exception like this:

  } catch (Exception e) {
      if (logger.isErrorEnabled()) {
          final String msg = "Unexpected error while listening on port " + receivePort;
          logger.error(msg, e);
      }
  }

But when building the code, I get this error:

[ERROR] org.openhab.binding.anel.internal.AnelHandler.java:[270]
Format should be constant. Use placeholder to reduce the needless cost of parameter construction. see http://www.slf4j.org/faq.html#logging_performance

However, there is no method available to log a throwable with a message with placeholder parameters?
This is why I use isErrorEnabled(), but the error is still reported. What do I miss?

Nevermind, I found the answer: https://stackoverflow.com/a/6374166

I had expected though that this information is part of the Javadoc…

In General you should not log on error level in openhab

That aside:

logger.error("Umexpected exception while connections to port {}", port, e);

Should work. Without checking the error level before.

Is there a reason why I should not log on error level?
How else should I report such exceptions?

That depends. If user interactions is required, probably WARN. If user interaction is required, but the thing status is updated, no need to log at all, set the thing status with the message. If there is nothing the user can do (except waiting) but it may reduce user experience, then INFO would be a good choice. If it „just happens“ and has no consequences, log to DEBUG.

Okay, thanks. I’ll consider that.

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.