new ConsoleHandler() : ConsoleHandler « java.util.logging « Java by API






new ConsoleHandler()

import java.io.IOException;
import java.util.logging.ConsoleHandler;
import java.util.logging.FileHandler;
import java.util.logging.Handler;
import java.util.logging.Logger;
import java.util.logging.XMLFormatter;

public class MainClass {
  public static void main(String args[]) {

    Logger logger = Logger.getLogger("my.log");

    Handler handler = null;

    try {
      handler = new FileHandler("messages.log");
    } catch (IOException e) {
      System.out.println("Could not create file. Using the console handler");
      handler = new ConsoleHandler();
    }

    logger.addHandler(handler);

    handler.setFormatter(new XMLFormatter());

    logger.info("Our first logging message");
    logger.severe("Something terrible happened");
  }
}

           
       








Related examples in the same category