Logger: addHandler(Handler handler) : Logger « java.util.logging « Java by API






Logger: addHandler(Handler handler)

  
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

1.Logger: config(String msg)
2.Logger: exiting(String sourceClass, String sourceMethod, Object result)
3.Logger: fine(String msg)
4.Logger: finer(String msg)
5.Logger: finest(String msg)
6.Logger: getAnonymousLogger()
7.Logger: getLogger(String name)
8.Logger: info(String msg)
9.Logger: isLoggable(Level level)
10.Logger: logp(Level level, String sourceClass, String sourceMethod, String msg)
11.Logger: severe(String msg)
12.Logger: setFilter(Filter newFilter)
13.Logger: setUseParentHandlers(boolean useParentHandlers)
14.Logger: warning(String msg)