General Logging Method

Is it good to do it; to log in using this method?

public static void log(final Object... messages) {
    logger.info(Joiner.on(',').useForNull("null").join(messages));
}

// Where in other parts of the code we can call the logger method, using a comma instead of string concatenation. Note that Joiner is a guava class.

log(" Check ",obja.getXXX(),anotherObject.getMethod());

Waiting is clean code, usability and performance.

+5
source share
3 answers

You might want to join ' '(empty) rather than a comma, but a design approach is a good idea.

Performance is wise, it is not perfect. You create an additional array of objects and a joiner, as well as internal joiner buffers.

, 2 3 . , slf4j, .

+2

SLF4J Logger ( Logback), .

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Test {
    private static final Logger logger = LoggerFactory.getLogger(Test.class);

    public void method(String name, int age, Address address) {
        logger.info("Person {}, age {} years old resides at {}", name, age, address);
    }
}
+4

The SLF4J interface is better, as others say.

I do not understand how this is a performance improvement. Perhaps the OP has a specific use case, and some sample code will explain this. If the idea is to collect messages in advance and send them along with a call to the log method, then you would be better off serving a buffering application.

+2
source

All Articles