Browse Source

improve log class

Arnaud Vergnet 3 years ago
parent
commit
e7dd395d29
1 changed files with 49 additions and 57 deletions
  1. 49
    57
      src/main/java/fr/insa/clavardator/util/Log.java

+ 49
- 57
src/main/java/fr/insa/clavardator/util/Log.java View File

@@ -1,67 +1,59 @@
1 1
 package fr.insa.clavardator.util;
2 2
 
3
+import org.jetbrains.annotations.Nullable;
4
+
3 5
 import java.text.SimpleDateFormat;
4 6
 import java.util.Date;
5 7
 
6 8
 public class Log {
7
-    /**
8
-     * Verbose level: 0 = nothing,
9
-     *                1: errors,
10
-     *                2: errors & warnings,
11
-     *                3: errors & warnings & debug,
12
-     *                4: all
13
-     */
14
-    public static int verboseLevel = 0;
15
-    
16
-    public static void v(String prefix, String message) {
17
-        v(prefix, message, null);
18
-    }
19
-    public static void v(String prefix, String message, Exception e) {
20
-        if (verboseLevel >= 4) {
21
-            Date date = new Date();
22
-            SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss.SSS");
23
-            System.out.println(sdf.format(date) + " | V | "+ prefix + ": " + message);
24
-            if (e != null)
25
-                e.printStackTrace();
26
-        }
27
-    }
9
+	/**
10
+	 * Verbose level: 0 = nothing,
11
+	 * 1: errors,
12
+	 * 2: errors & warnings,
13
+	 * 3: errors & warnings & debug,
14
+	 * 4: all
15
+	 */
16
+	public static int verboseLevel = 0;
17
+
18
+	private static void print(String prefix, String message, String mode, int requiredLevel, @Nullable Exception e) {
19
+			if (verboseLevel >= requiredLevel) {
20
+					Date date = new Date();
21
+					SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss.SSS");
22
+					System.out.println(sdf.format(date) + " | " + mode + " | " + prefix + ": " + message);
23
+					if (e != null)
24
+							e.printStackTrace();
25
+			}
26
+	}
27
+
28
+	public static void v(String prefix, String message) {
29
+		v(prefix, message, null);
30
+	}
31
+
32
+	public static void v(String prefix, String message, Exception e) {
33
+		print(prefix, message, "V", 4, e);
34
+	}
35
+
36
+	public static void d(String prefix, String message) {
37
+		d(prefix, message, null);
38
+	}
39
+
40
+	public static void d(String prefix, String message, Exception e) {
41
+		print(prefix, message, "D", 3, e);
42
+	}
43
+
44
+	public static void w(String prefix, String message) {
45
+		w(prefix, message, null);
46
+	}
28 47
 
29
-    public static void d(String prefix, String message) {
30
-        d(prefix, message, null);
31
-    }
32
-    public static void d(String prefix, String message, Exception e) {
33
-        if (verboseLevel >= 3) {
34
-            Date date = new Date();
35
-            SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss.SSS");
36
-            System.out.println(sdf.format(date) + " | D | "+ prefix + ": " + message);
37
-            if (e != null)
38
-                e.printStackTrace();
39
-        }
40
-    }
48
+	public static void w(String prefix, String message, Exception e) {
49
+		print(prefix, message, "W", 2, e);
50
+	}
41 51
 
42
-    public static void w(String prefix, String message) {
43
-        w(prefix, message, null);
44
-    }
45
-    public static void w(String prefix, String message, Exception e) {
46
-        if (verboseLevel >= 2) {
47
-            Date date = new Date();
48
-            SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss.SSS");
49
-            System.out.println(sdf.format(date) + " | W | "+ prefix + ": " + message);
50
-            if (e != null)
51
-                e.printStackTrace();
52
-        }
53
-    }
52
+	public static void e(String prefix, String message) {
53
+		e(prefix, message, null);
54
+	}
54 55
 
55
-    public static void e(String prefix, String message) {
56
-        e(prefix, message, null);
57
-    }
58
-    public static void e(String prefix, String message, Exception e) {
59
-        if (verboseLevel >= 1) {
60
-            Date date = new Date();
61
-            SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss.SSS");
62
-            System.out.println(sdf.format(date) + " | E | "+ prefix + " " + message);
63
-            if (e != null)
64
-                e.printStackTrace();
65
-        }
66
-    }
56
+	public static void e(String prefix, String message, Exception e) {
57
+		print(prefix, message, "E", 1, e);
58
+	}
67 59
 }

Loading…
Cancel
Save