Logging.py 1.73 KB
Newer Older
German Leon's avatar
German Leon committed
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
import time
import datetime

"""
Logging class
"""


class Logging:
    log_file = None
    debug_var = None
    unique_id = None

    def __init__(self, log_file, unique_id=''):
        self.log_file = log_file
        self.unique_id = unique_id

    def info(self, msg):
        with open(self.log_file, "a") as fp:
            d = datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S')
            fp.write("[INFO -- " + d + "]\n" + msg + "\n")
            # fp.close()

    def exception(self, msg):
        with open(self.log_file, "a") as fp:
            d = datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S')
            fp.write("[EXCEPTION -- " + d + "]\n" + msg + "\n")
            # fp.close()

    def error(self, msg):
        with open(self.log_file, "a") as fp:
            d = datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S')
            fp.write("[ERROR -- " + d + "]\n" + msg + "\n")
            # fp.close()

    def debug(self, msg):
        with open(self.log_file, "a") as fp:
            d = datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S')
            fp.write("[DEBUG -- " + d + "]\n" + msg + "\n")
            # fp.close()

    def summary(self, msg):
        with open(self.log_file, "a") as fp:
            d = datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S')
            fp.write("[SUMMARY -- " + d + "]\nFI-uniqueID=" + str(self.unique_id) + "\n" + msg + "\n")
            # fp.close()

    def search(self, find):
        with open(self.log_file, "r") as fp:
            lines = fp.readlines()
            # fp.close()
        for l in lines:
            if find in l:
                return l
        return None