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
  • 775
  • 1
  • 10
  • 28

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
  • 36,032
  • 27
  • 135
  • 183
Yusuf Kapasi
  • 553
  • 2
  • 8