Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to I format my log message

Tags:

java

logging

I would like to format my log message before being printed out on the console Logger.fine

e.g. How do I format "{0} has {1} apples with him", so the inputs are John and 10

I would prefer a logging framework which provides a solution to this and I don't want to format these messages separately. JDK6 specific logging classes don't seem to have these granularity.

like image 354
Jason Avatar asked Aug 12 '11 07:08

Jason


1 Answers

JUL (java.util.logging) supports curly-brace style parameters out-of-the-box. You do not need a third-party logging framework. See below for a single-class working example (spoiler, I still recommend using SLF4J instead of JUL).

import java.time.LocalDateTime;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;

class JavaUtilLoggingExample {

    private static final Logger log = Logger.getLogger(JavaUtilLoggingExample.class.getName());

    /**
     * A java.util.logging (JUL) example adapted from the JavaDoc for java.text.MessageFormat.
     * See https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/text/MessageFormat.html
     *
     * Written for the StackOverflow question: https://stackoverflow.com/q/7036765
     */
    public static void main(String[] args) {
        int planet = 7;
        String event = "a disturbance in the Force";

        log.log(Level.INFO, "At {1,time} on {1,date}, there was {2} on planet {0,number,integer}.", new Object[]{planet, new Date(), event});

        // The following is printed (pretty good!):
        //
        //        Jan 02, 2022 8:06:59 PM JavaUtilLoggingExample main
        //        INFO: At 8:06:59 PM on Jan 2, 2022, there was a disturbance in the Force on planet 7

        // However, try to modernize your code and use java.time.LocalDateTime instead of java.util.Date and you will be
        // met with a surprise. Given this log statement:
        log.log(Level.INFO, "At {1,time} on {1,date}, there was {2} on planet {0,number,integer}.", new Object[]{planet, LocalDateTime.now(), event});

        // The following is printed (not good!):
        //
        //        Jan 02, 2022 8:06:59 PM JavaUtilLoggingExample main
        //        INFO: At {1,time} on {1,date}, there was {2} on planet {0,number,integer}.

        // Why? See this discussion. https://stackoverflow.com/a/66870717

        // By contrast, a similar (but less feature-ful) version of the above log statement written using the
        // SLF4J 'org.slf4j.Logger' class is below. It is much more succinct but it doesn't offer as many formatting
        // features.
        //
        // log.info("At {}, there was {} on planet {}.", new Date(), event, planet);

        // In conclusion, I continually wind back up with SLF4J in even my trivial Java programs despite my
        // once-yearly well-intentioned "Hey, why don't I reduce external dependencies as much as feasible. Can I just use JUL?"
        // efforts. The SLF4J API fits well in practice.
    }
}
like image 185
David Groomes Avatar answered Sep 28 '22 08:09

David Groomes