Create custom formatter : Log Formatter « Log « Java Tutorial






import java.util.logging.ConsoleHandler;
import java.util.logging.Formatter;
import java.util.logging.Handler;
import java.util.logging.LogRecord;
import java.util.logging.Logger;

public class MainClass {
  private static Logger logger = Logger.getLogger("SimpleFormatterExample");

  private static void logMessages() {
    logger.info("Line One");
    logger.info("Line Two");
  }

  public static void main(String[] args) {
    logger.setUseParentHandlers(false);
    Handler conHdlr = new ConsoleHandler();
    conHdlr.setFormatter(new Formatter() {
      public String format(LogRecord record) {
        return record.getLevel() + "  :  " + record.getSourceClassName() + " -:- "
            + record.getSourceMethodName() + " -:- " + record.getMessage() + "\n";
      }
    });
    logger.addHandler(conHdlr);
    logMessages();
  }
}








35.4.Log Formatter
35.4.1.Set log Formatter
35.4.2.Create custom formatter
35.4.3.Define your own Custom Formatter
35.4.4.Log HTML formatter
35.4.5.Logger with XMLFormatter and FileHandler
35.4.6.XMLFormatter based Logging
35.4.7.Creating a Custom Formatter for a Logger Handler
35.4.8.Setting the Formatter of a Logger Handler