0

I'm writing a log file in my Java program using the code from here

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");  
    logger.addHandler(fh);
    SimpleFormatter formatter = new SimpleFormatter();  
    fh.setFormatter(formatter);  

    // the following statement is used to log any messages  
    logger.info("My first log");  

    } catch (SecurityException e) {  
        e.printStackTrace();  
    } catch (IOException e) {  
        e.printStackTrace();  
    }  

    logger.info("Hi How r u?");  

}

My problem is that i'm getting a multiple log files

Community
  • 1
  • 1
Amir Rossert
  • 1,003
  • 2
  • 13
  • 33

1 Answers1

2

Change the code as below

fh = new FileHandler("C:/temp/test/MyLogFile.log", true);

This will not create multiple files and will append to the same file.

Jean-François Corbett
  • 37,420
  • 30
  • 139
  • 188
Yusuf Kapasi
  • 577
  • 2
  • 9
  • I had the same issue, it is due to new object creation of FileHandler which happens to create new log files everytime the application is run. – iltaf khalid Mar 13 '17 at 20:49