org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger("MyClass"); // ... catch (Exception e) { log.error("message: " + e.getMessage(), new Exception()); // FP }
In this code, error(String, Throwable) will be called, and the rule suggests to use format specifier. The resulting code would look like:
catch (Exception e) { log.error("message: {}", e.getMessage(), new Exception()); }
It compiles, log.error(String, Object, Object) will be called and the resulting log will be exactly the same for SimpleLogger/Logback. However, we can not assume that any other underlying log framework will correctly handle the case where the last Object argument is a Throwable, as it is not explicitly stated in the documentation.
We should not report an issue when slf4j contains a string concatenation and a Throwable as second argument.
- relates to
-
RSPEC-3457 Printf-style format strings should be used correctly
- Active
- links to