Last active
February 14, 2017 07:10
-
-
Save fty4/5ea9a44b231d0f57912f10297a7fe135 to your computer and use it in GitHub Desktop.
Logging-Class um das schreiben von Log-Dateien zu vereinfachen
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
import java.io.File; | |
import java.io.FileWriter; | |
import java.io.PrintWriter; | |
import java.io.StringWriter; | |
import java.text.SimpleDateFormat; | |
import java.time.LocalDateTime; | |
import java.time.format.DateTimeFormatter; | |
import java.util.Date; | |
/** | |
* Klasse bietet Methoden um das Loggen zu vereinfachen | |
* | |
*/ | |
public class Logging | |
{ | |
/** Trennzeichen fuer Ausgaben / Logs */ | |
public static final char SEPERATOR = '|'; | |
public static final String LOG_PATH = "C:\\"; | |
public static final String LOG_NAME = "file";//.0.log | |
/** | |
* | |
* | |
* /** | |
* Erstellt eine Lognachricht mit Zeitstempel | |
* | |
* @param p_path Pfad zur Datei, in die geloggt werden soll | |
* @param p_filename Dateiname, in die geloggt werden soll | |
* @param p_message Nachricht die geloggt werden soll | |
*/ | |
public static void log(String p_path, String p_filename, String p_message) | |
{ | |
try | |
{ | |
moveOldLogsIfNeeded(p_path, p_filename, 10); | |
// Aktuelle Zeit auslesen | |
String timestamp = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS")); | |
// Erstellt Ordner wenn nicht vorhanden | |
if(!new File(p_path).exists()) | |
{ | |
File file = new File(p_path); | |
file.mkdirs(); | |
} | |
FileWriter fw = new FileWriter(p_path + p_filename + ".0.log", true); | |
PrintWriter pw = new PrintWriter(fw); | |
pw.write(timestamp + SEPERATOR + p_message + "\n"); | |
pw.close(); | |
fw.close(); | |
} | |
catch (Exception e) | |
{ | |
e.printStackTrace(); | |
} | |
} | |
/** | |
* Exception-Logging mit Consoleoutput | |
* | |
* @param p_e | |
* - Exception | |
*/ | |
public static void exceptionLog(Exception p_e) | |
{ | |
// Auslesen der exakten Fehlermeldung für Logdatei | |
StringWriter sw = new StringWriter(); | |
p_e.printStackTrace(new PrintWriter(sw)); | |
String error = sw.toString(); //.replace("\n", "").replace("\r", ""); | |
// Exception in der Konsole ausgeben | |
p_e.printStackTrace(); | |
// Exeption loggen | |
log(LOG_PATH, LOG_NAME, | |
"Line:" + String.valueOf(p_e.getStackTrace()[0].getLineNumber()) + SEPERATOR + "Class:" | |
+ p_e.getStackTrace()[0].getClassName() + SEPERATOR + "Method:" | |
+ p_e.getStackTrace()[0].getMethodName() + SEPERATOR + "Exception:" + error); | |
} | |
/** | |
* | |
* 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 und Nummerierung) | |
* @param p_maxLogfiles Maximale Anzahl an Logfiles (wie lange Backupen) | |
* | |
* @throws Exception Fehlermeldung bei Zugriffsprobleme | |
*/ | |
private 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)) | |
{ | |
// Aelterste Datei entfernen | |
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"); | |
} | |
// Verschieben-Durchlauf (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 | |
*/ | |
private 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