summaryrefslogtreecommitdiffstats
path: root/src/main/java/derms/replica3/Logger.java
blob: 15124c561dccd8de98166ac9183234aed3b63560 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package derms.replica3;


import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.Field;
import java.text.DateFormat;
import java.util.Date;

public class Logger {
    private FileWriter fileWriter = null;
    private BufferedWriter bufferedWriter = null;
    private PrintWriter printWriter = null;

    public Logger(final String activityLoggerFile) throws IOException {
        fileWriter = new FileWriter(activityLoggerFile, true);
        bufferedWriter = new BufferedWriter(fileWriter);
        printWriter = new PrintWriter(bufferedWriter);
    }

    public synchronized void log( String action, String status, String res) {
        try {
            final String dataLog =  DateFormat.getDateTimeInstance().format(new Date()) + " [" +
                    action + "] : [" + status + "] - " + res;

            printWriter.println(dataLog);
            System.out.println(dataLog);
            bufferedWriter.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public synchronized void clientLog(final String userId, final String action, final String message) {
        try {
            final String dataLog = DateFormat.getDateTimeInstance().format(new Date()) + " [" + userId + "] [" +
                    action + "] - " + message;
            printWriter.println(dataLog);
            System.out.println(dataLog);
            bufferedWriter.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public synchronized void clientLog(final String userId, final String action) {
        try {
            final String dataLog = DateFormat.getDateTimeInstance().format(new Date()) +  " [" + userId + "] " + " [" +
                    action + "] ";
            printWriter.println(dataLog);
            System.out.println(dataLog);
            bufferedWriter.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}