0

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?

fatCop
  • 2,556
  • 10
  • 35
  • 57
ritu4
  • 11
  • 1
  • 2

2 Answers2

1

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

-1

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.

DanielGibbs
  • 9,910
  • 11
  • 76
  • 121
  • For this it is not working showing the error can't make a static reference to non-static method. – ritu4 Aug 07 '20 at 10:36