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();
}
这将创建一个今天的文件夹,但是我想为每天创建一个新文件夹并将日志文件存储在新文件夹中....意味着每天的文件夹必须具有当天的日志文件,我具有以下创建文件夹的方法
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);
如果要每天创建一个新文件夹,我应该更改什么?
相关分类