I am writing code the below code
Logger.error("Error in x file :",+ e.getMessage());
But for the above line of code I am getting the error to use format specifiers instead of string concatenation. How should I fix it?
I am writing code the below code
Logger.error("Error in x file :",+ e.getMessage());
But for the above line of code I am getting the error to use format specifiers instead of string concatenation. How should I fix it?
Add to your class:
private static final Logger logger = LoggerFactory.getLogger(YourClassName.class);
and try this:
logger.error("Error in x file {}", e.getMessage());
Logging this way, you avoid performance overhead for strings concatenation.
More about spring-boot logging configuration Zero Configuration Logging
Instead of concatenating the string with +
(in "Error in x file :",+ e.getMessage()
), you can use String.format
to build the string instead:
Logger.error(String.format("Error in x file :%s", e.getMessage()));
Documentation on how Java String formatting works can be found here.