1

I'm trying the Lombok @Flogger feature which is supposed to add the below when annotating the class with @Flogger.

private static final com.google.common.flogger.FluentLogger log = com.google.common.flogger.FluentLogger.forEnclosingClass();

See the little documentation on it. https://projectlombok.org/features/log

I added the below log to a method:

log.atInfo().log("This is my info message");

And the output that I see in the console is:

Mar 21, 2019 7:35:05 PM net.domain.Class myMethod
INFO: This is my info message

I'd prefer a "YYYY-MM-DD HH:MM:SS.mmm" with 24-hour time format. Is there way to configure this? I don't have to use the Lombok annotation, it just seemed simpler.

Also, I wasn't able to find the flogger tag for this post.

jmehrens
  • 10,580
  • 1
  • 38
  • 47
  • 1
    Also, see the Google Flogger documentation and github links https://google.github.io/flogger/ and https://github.com/google/flogger – twoversionsofme Mar 22 '19 at 01:15

1 Answers1

1

From the output it looks like flogger is using SimpleFormatter from JUL. The format is controlled by setting the system property or define the key in the logging.properties. The formatter arguments are described in SimpleFormatter::format method. Keep in mind that the arguments in the documentation are off by one so the date parameter is actually %1.

The syntax of the date formatting is described in java.util.Formatter.

Here is sample test program that can used to make sure your pattern compiles correctly when applied at runtime. One pattern that should work is: %1$tF %1$tT.%1$tL %2$s%n%4$s: %5$s%6$s%n.

import java.util.logging.Level;
import java.util.logging.LogManager;
import java.util.logging.LogRecord;

public class Main {
    public static void main(String[] args) throws Exception {
        final String format = "%1$tF %1$tT.%1$tL %2$s%n%4$s: %5$s%6$s%n";
        final String key = "java.util.logging.SimpleFormatter.format";
        test(format);
        test(System.getProperty(key, format));
        test(LogManager.getLogManager().getProperty(key));
    }

    private static void test(String format) {
        if (format != null) {
            LogRecord record = new LogRecord(Level.INFO, "");
            System.out.println(String.format(format,
                             new java.util.Date(record.getMillis()),
                             record.getSourceClassName(),
                             record.getLoggerName(),
                             record.getLevel().getLocalizedName(),
                             record.getMessage(),
                             String.valueOf(record.getThrown())));
        } else {
            System.out.println("null format");
        }
    }
}

Which prints:

2019-03-21 21:51:08.604 null
INFO: null
jmehrens
  • 10,580
  • 1
  • 38
  • 47