Writing Log Records to a Log File - Java Java Virtual Machine

Java examples for Java Virtual Machine:Logger

Description

Writing Log Records to a Log File

Demo Code

import java.io.IOException;
import java.util.logging.FileHandler;
import java.util.logging.Logger;

public class Main {
  public void m() throws Exception {
    try {/*w  ww . j  a v  a2s .  c o m*/
      // Create a file handler that write log record to a file called my.log
      FileHandler handler = new FileHandler("my.log");

      // Add to the desired logger
      Logger logger = Logger.getLogger("com.mycompany");
      logger.addHandler(handler);
    } catch (IOException e) {
    }
    try {
      // Create an appending file handler
      boolean append = true;
      FileHandler handler = new FileHandler("my.log", append);

      // Add to the desired logger
      Logger logger = Logger.getLogger("com.mycompany");
      logger.addHandler(handler);
    } catch (IOException e) {
    }
  }
}

Related Tutorials