Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generic Logging Method

Is this a good thing to do; to log via a method like this?

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

// Where in other parts of code we can call the logger method using commas instead of string concatenation. Please note Joiner is a guava class.

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

Expectation is clean code, convenience and performance.

like image 729
r0ast3d Avatar asked Feb 05 '13 16:02

r0ast3d


People also ask

How do you log a method in Java?

Java Math log() method with example There are various cases : If the argument is NaN or less than zero, then the result is NaN. If the argument is positive infinity, then the result is positive infinity. If the argument is positive zero or negative zero, then the result is negative infinity.

Which is the best logging framework in Java?

One of the most popular solutions for the Java world is the Apache Log4j 2 framework. Maintained by the Apache Foundation, Log4j 2 is an improvement on the original Log4j, which was the most popular logging framework in Java for many years.

What is a logging API?

The Logging APIs capture information such as security failures, configuration errors, performance bottlenecks, and/or bugs in the application or platform. The core package includes support for delivering plain text or XML-formatted log records to memory, output streams, consoles, files, and sockets.


1 Answers

I use SLF4J's Logger interface (with Logback implementation), which I find much cleaner to read.

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);
    }
}
like image 67
adarshr Avatar answered Sep 19 '22 11:09

adarshr