Last active
February 14, 2017 07:10
-
-
Save fty4/02abfaf6bf99b471ea805bacc1a37641 to your computer and use it in GitHub Desktop.
Prueft ob die alten Logfiles verschoben werden muessen, wenn heute nicht modifiziert
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* | |
* Prueft ob die alten Logfiles verschoben werden muessen | |
* Bedingung: Logdatei wurde heute nicht geaendert | |
* | |
* @param p_logFilepath Pfad zum Logfileordner | |
* @param p_logFilename Dateiname (ohne .log & nummerierung) | |
* @param p_maxLogfiles Maximale Anzahl an Logfiles (wie lange Backupen) | |
* | |
* @throws Exception Fehlermeldung bei Zugriffsprobleme | |
*/ | |
public static void moveOldLogsIfNeeded(String p_logFilepath, String p_logFilename, int p_maxLogfiles) throws Exception | |
{ | |
// Logfile-Handle | |
File f0 = new File(p_logFilepath + p_logFilename + ".0.log"); | |
if (f0.exists()) | |
{ | |
// Pruefen ob heut schon bearbeitet | |
if (!wasModifiedToday(f0)) | |
{ | |
// Move old files | |
// Delete oldest one | |
File f = new File(p_logFilepath + p_logFilename + "." + String.valueOf(p_maxLogfiles - 1) + ".log"); | |
if (f.exists()) | |
{ | |
if (!f.delete()) | |
throw new Exception("File not deleted"); | |
} | |
// Durchlaufen nach Anzahl der erlaubten Logfiles | |
for (int i = p_maxLogfiles - 2; i >= 0; i--) | |
{ | |
f = new File(p_logFilepath + p_logFilename + "." + String.valueOf(i) + ".log"); | |
if (f.exists()) | |
{ | |
// Verschieben | |
if (!f.renameTo(new File(p_logFilepath + p_logFilename + "." + String.valueOf(i + 1) + ".log"))) | |
throw new Exception("File not able to move (" + i + ")"); | |
} | |
} | |
} | |
} | |
} | |
/** | |
* | |
* | |
* @param p_file | |
* Get File, which to check | |
* @return Returns true if it was modified, false if not | |
*/ | |
public static boolean wasModifiedToday(File p_file) | |
{ | |
// Datum abrufen | |
Date DateFile = new Date(p_file.lastModified()); | |
Date DateNow = new Date(); | |
// Datums-Formatierer | |
SimpleDateFormat formatterDay = new SimpleDateFormat("dd"); | |
SimpleDateFormat formatterMonth = new SimpleDateFormat("MM"); | |
SimpleDateFormat formatterYear = new SimpleDateFormat("yyyy"); | |
// Pruefen ob sich der Tag geaendert hat | |
int dayDiff = (Integer.valueOf(formatterYear.format(DateNow)) - Integer.valueOf(formatterYear.format(DateFile))) | |
+ (Integer.valueOf(formatterMonth.format(DateNow)) - Integer.valueOf(formatterMonth.format(DateFile))) | |
+ (Integer.valueOf(formatterDay.format(DateNow)) - Integer.valueOf(formatterDay.format(DateFile))); | |
// Wenn dayDiff == 0 wurde die Datei heute nicht geaendert | |
return (dayDiff == 0) ? true : false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment