Code example for FileHandler

Methods: close

0
public class LoggingExample1 { 
  public static void main(String args[]) {
    try { 
      LogManager lm = LogManager.getLogManager();
      Logger logger = Logger.getLogger("LoggingExample1");
      FileHandler fh = new FileHandler("log_test.txt");
 
      lm.addLogger(logger);
      logger.addHandler(fh);
       
      //logger.setLevel(Level.INFO); 
      //fh.setFormatter(new XMLFormatter()); 
 
       
      // root logger defaults to SimpleFormatter. We don't want messages 
      // logged twice. 
      //logger.setUseParentHandlers(false); 
      logger.log(Level.INFO, "test 1");
      logger.log(Level.INFO, "test 2");
      logger.log(Level.INFO, "test 3");
      fh.close();
    } catch (Exception e) {
      System.out.println("Exception thrown: " + e);
      e.printStackTrace();
    } 
  }