1

I want to add exceptions in a single log file, In my source code following code is used multiple times... i want to store all exceptions in single file, but it creates multiple files like exception.log, exception.log.1, exception.log.1.lck, exception.log.2 and so on...

Date dir1 = new java.util.Date(System.currentTimeMillis());
String baseDir1 = "/home/gaurav/usr/logs/ESBegin/";
String newDir1 = createDateBasedDirectory(baseDir1, dir1);
System.out.println("Exception :: " + e.getMessage());
Logger logger = Logger.getLogger("MyLog");  
FileHandler fh;  
try {  
    // This block configure the logger with handler and formatter  
    fh = new FileHandler(newDir1+"/exception.log");  
    logger.addHandler(fh);
    SimpleFormatter formatter = new SimpleFormatter();
    fh.setFormatter(formatter);  
    // the following statement is used to log any messages  
    logger.info(e.getMessage()); 
} catch (SecurityException ex) {  
    ex.printStackTrace();  
} catch (IOException ex) {  
    ex.printStackTrace();  
}  
Coder-Man
  • 2,391
  • 3
  • 11
  • 19
Gaurav
  • 173
  • 1
  • 13

1 Answers1

2

It's because you create a new FileHandler each time you call that method, instead make it a class instance variable. I tested it and it works:

import java.io.IOException;
import java.util.Date;
import java.util.logging.FileHandler;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;

public class Main {

    private static String newDir1 = "C:\\Users\\pavel.orekhov\\Desktop";
    private static FileHandler fh;

    static {
        try {
            fh = new FileHandler(newDir1 + "\\exception.log", 0, 1, true);
        } catch (IOException | SecurityException e) {
        }
    }

    static void test() {
        Date dir1 = new java.util.Date(System.currentTimeMillis());
        Logger logger = Logger.getLogger("MyLog");
        logger.addHandler(fh);
        SimpleFormatter formatter = new SimpleFormatter();
        fh.setFormatter(formatter);
        logger.info("test");

    }

    public static void main(String[] args) {
        // all these write to the same file
        test();
        test();
        test();
        test();
    }
}

The code that you had was something like this:

import java.io.IOException;
import java.util.Date;
import java.util.logging.FileHandler;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;

public class Main {

    static void test() {
        Date dir1 = new java.util.Date(System.currentTimeMillis());
        String newDir1 = "C:\\Users\\pavel.orekhov\\Desktop";
        Logger logger = Logger.getLogger("MyLog");
        FileHandler fh;
        try {
            fh = new FileHandler(newDir1 + "\\exception.log", 0, 1, true);
            logger.addHandler(fh);
            SimpleFormatter formatter = new SimpleFormatter();
            fh.setFormatter(formatter);
            logger.info("test");
        } catch (SecurityException | IOException ex) {
            ex.printStackTrace();
        }
    }

    public static void main(String[] args) {
        test(); // creates file 1
        test(); // creates file 2
        test(); // creates file 3
        test(); // creates file 4
    }
}

this does create as many files as you have calls to the test() method.

Coder-Man
  • 2,391
  • 3
  • 11
  • 19