Amir Rossert
Amir Rossert

Reputation: 1053

Java single log file

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

Upvotes: 0

Views: 365

Answers (1)

Yusuf Kapasi
Yusuf Kapasi

Reputation: 587

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.

Upvotes: 2

Related Questions