Reputation: 173
Date dir = new java.util.Date(System.currentTimeMillis());
String baseDir = "/home/gaurav/usr/logs/ESBegin/";
String newDir = createDateBasedDirectory(baseDir, dir);
Logger logger = Logger.getLogger("MyLog1");
FileHandler fh;
try {
// This block configure the logger with handler and formatter
fh = new FileHandler(newDir+"/data.log");
logger.addHandler(fh);
SimpleFormatter formatter = new SimpleFormatter();
fh.setFormatter(formatter);
// the following statement is used to log any messages
logger.info(stringifiedJson);
} catch (SecurityException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
this will create a folder of todays date but i want to create new folder for everyday and store the log file in new folder....means everyday's folder must have that day's log file i have following method to create a folder
public static String createDateBasedDirectory(String baseDirectory, Date argDate) {
String newDir = null;
if (baseDirectory != null && argDate != null) {
try {
String format = "yyyy-MM-dd";
DateFormat dateFormatter = new SimpleDateFormat(format);
String date = dateFormatter.format(argDate);
File f = new File(baseDirectory);
File files[] = f.listFiles();
String dir = null;
int baseDirLength = baseDirectory.length();
int checkingLength = baseDirLength + format.length() + 3;
int maxSeqNo = 0;
for (int i = 0; i < files.length; i++) {
if (files[i].isDirectory()) {
dir = files[i].toString();
if (dir.length() == checkingLength) {
String existingDirDate = dir.substring(baseDirLength, baseDirLength + 10);
if (date.equalsIgnoreCase(existingDirDate)) {
int dirSeqNo =
Integer.parseInt(dir.substring(baseDirLength + 11, baseDirLength + 10 + 3));
if (dirSeqNo > maxSeqNo) {
maxSeqNo = dirSeqNo;
}
}
}
}
}
String currSeq = "" + (maxSeqNo + 1);
if (currSeq.length() == 1) {
currSeq = "0" + currSeq;
}
newDir = baseDirectory + date;
new File(newDir).mkdir();
} catch (Exception e) {
e.printStackTrace();
}
}
return newDir;
}
what should i change if in want to create a new folder everyday
Upvotes: 1
Views: 6248
Reputation: 18568
Check if the directory for the current day exists, then create it if it doesn't exist or simply return it if it does exist.
/*
* to make sure everyone knows what's going on
* name this method like getOrCreateDailyLogDirectory
*/
public static String createDateBasedDirectory(String baseDirectory, Date argDate) {
String newDir = null;
if (baseDirectory != null && argDate != null) {
try {
String format = "yyyy-MM-dd";
DateFormat dateFormatter = new SimpleDateFormat(format);
String date = dateFormatter.format(argDate);
// check if the directory exists:
String todaysLogDir = baseDirectory + "\\" + date; // create the path as String
// then create a Path (java.nio, alternatives possible)
Path todaysDirectoryPath = Paths.get(todaysLogDir);
// and check if this Path exists
if (Files.exists(todaysDirectoryPath) {
// if present, just return it in order to write (into) a log file there
return todaysDirectoryPath.toUri().toString();
} else {
// create it the way you want and return the path as String
...
}
...
} catch (Exception e) {
e.printStackTrace();
}
}
return newDir;
}
Doing it this (or a similar) way always returns the directory for the current day and creates it before once a day on first log attempt.
Upvotes: 1
Reputation: 92437
You can use the functionality of your logging framework to do so. For example use Log4J's RollingFileAppender.
You can use the fileName
parameter to create new directories. From the documentation:
fileName: The name of the file to write to. If the file, or any of its parent directories, do not exist, they will be created.
Upvotes: 1