2

I wanted to log exception into log file instead of console. How to configure Flogger logging.

package renameform;
import com.google.common.flogger.FluentLogger;

public class TestFlogger {
    private static final FluentLogger logger = FluentLogger.forEnclosingClass();
    public static void main(String[] args) {
        try {
            int a=2/0;
        }catch(Exception exception)
        {
            logger.atInfo().withCause(exception).log("Log message with: %s", 200);
        }   

    }

}

output:

May 30, 2019 2:16:00 PM renameform.TestFlogger main
INFO: Log message with: 200
java.lang.ArithmeticException: / by zero
    at renameform.TestFlogger.main(TestFlogger.java:8)

need so info about logs into text file & how to format.

sanyassh
  • 8,100
  • 13
  • 36
  • 70

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.

You can reference Java Logging - how to redirect output to a custom log file for a logger? as an example on configuring a FileHandler. The configuration file is explained in Java Logging Overview.

jmehrens
  • 10,580
  • 1
  • 38
  • 47