N
N
Nikita072021-08-27 14:34:21
Java
Nikita07, 2021-08-27 14:34:21

Why is getFormattedMessage() needed?

Can you please tell me what the getFormattedMessage() method does in this case?

private static final MessageFactory MESSAGE_FACTORY = new ReusableMessageFactory();


    private static String determineLogMessage(String format, Object... args) {
        if (args == null || args.length == 0) {
            return forma;
        }
        return MESSAGE_FACTORY.newMessage(format, args).getFormattedMessage();
    }


I understand that you need to get a formatted string, but how exactly does he format it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Arty_Fact, 2021-08-29
@Arty_Fact

The factory creates a templated message by adding objects, and getFormattedMessage() returns a formatted string.
For example:

String date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
Message message = new ReusableMessageFactory().newMessage("{} Very important message", date);
System.out.println(message.getFormattedMessage());

Will return a
2021-08-29 16:37:24 Very important message
String date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
Message message = new ReusableMessageFactory().newMessage("{} [{}] Very important message", date, Level.WARN);
System.out.println(message.getFormattedMessage());
as you guessed it will print:
2021-08-29 16:43:57 [WARN] Very important message

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question