Replace debug messages with static trailing strings

Considered a good replacement for hardcoded values ​​with static final properties in the java.

However, I notice that developers usually hardcode the log message.

Does it make sense to move log messages to static final properties?

public class MyClass {

    private static final Logger LOGGER = Logger.getLogger(MyClass.class));

    public void foo() {
        LOGGER.info("My message");
    }

}

      

VS

public class MyClass {

    private static final Logger LOGGER = Logger.getLogger(MyClass.class);
    private static final String MY_MESSAGE = "My message";

    public void foo() {
        LOGGER.info(MY_MESSAGE);
    }

}

      

+3


source to share


1 answer


Yes, it is always recommended to select the content of the Message / Hard code. Create another java file (like Message.java) and define all the variables and their values.



You can also use the eclipse External String Utility.

-1


source







All Articles