How to write logs in text file when using java.util.logging.Logger

Try this sample. It works for me.

public static void main(String[] args) {  

    Logger logger = Logger.getLogger("MyLog");  
    FileHandler fh;  

    try {  

        // This block configure the logger with handler and formatter  
        fh = new FileHandler("C:/temp/test/MyLogFile.log");  
        SimpleFormatter formatter = new SimpleFormatter();  

        // the following statement is used to log any messages "My first log");  

    } catch (SecurityException e) {  
    } catch (IOException e) {  
    }"Hi How r u?");  


Produces the output at MyLogFile.log

Apr 2, 2013 9:57:08 AM testing.MyLogger main  
INFO: My first log  
Apr 2, 2013 9:57:08 AM testing.MyLogger main  
INFO: Hi How r u?


To remove the console handler, use


since the ConsoleHandler is registered with the parent logger from which all the loggers derive.

Leave a Comment