简体   繁体   中英

Why java.util.logging is not writing logs to file?

I have prepared a logger for a class. Code is as follows:

public class MyClass {

    FileHandler handler;
    Logger logger;

    // SINGLETON PATTERN
    private static final MyClass SINGLE_INSTANCE = new MyClass();         
    private MyClass() {

      try {
        handler = new FileHandler("MyClass.log", false);
        logger = Logger.getLogger(MyClass.class.getName());
        handler.setLevel(Level.ALL);
        logger.setLevel(Level.ALL);
      } catch (IOException ioe) {
        System.err.println("Initialization of logger threw");
        ioe.printStackTrace();
        throw new RuntimeException("Initialization of logger threw", ioe);
      }

    }
    public static MyClass getInstance() {
      return SINGLE_INSTANCE;
    }    

    public void doStuff() {

        Logger.getLogger(MyClass.class.getName()).log(Level.FINE, "Log Text");

    }

}

To enable the logging for Level.FINE I have invoked method .setLevel both on logger and on handler . But when I execute the code and invoke doStuff() no output appears on the log.

Notes that could be relevant:

  1. The file is there but it has size 0.
  2. I am stopping the program with CONTROL+C (it is a console application).

Add Handler

     logger = Logger.getLogger(MyClass.class.getName());
     logger.addHandler(handler);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM