我想在单个日志文件中添加异常,在我的源代码中多次使用以下代码......我想将所有异常存储在单个文件中,但它会创建多个文件,如 exception.log、exception.log.1、 exception.log.1.lck、exception.log.2 等等……
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();
}