LogMessage   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 45
rs 10
c 0
b 0
f 0
wmc 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __str__() 0 3 1
A __ne__() 0 2 1
A to_string_dict() 0 15 2
A __eq__() 0 4 1
B __init__() 0 15 5
1
from datetime import datetime
2
3
from coalib.output.printers.LOG_LEVEL import LOG_LEVEL
4
5
6
class LogMessage:
7
8
    def __init__(self,
9
                 log_level,
10
                 *messages,
11
                 delimiter=" ",
12
                 timestamp=None):
13
        if log_level not in LOG_LEVEL.reverse:
14
            raise ValueError("log_level has to be a valid LOG_LEVEL.")
15
16
        str_messages = [str(message) for message in messages]
17
        self.message = str(delimiter).join(str_messages).rstrip()
18
        if self.message == "":
19
            raise ValueError("Empty log messages are not allowed.")
20
21
        self.log_level = log_level
22
        self.timestamp = datetime.today() if timestamp is None else timestamp
23
24
    def __str__(self):
25
        log_level = LOG_LEVEL.reverse.get(self.log_level, "ERROR")
26
        return '[{}] {}'.format(log_level, self.message)
27
28
    def __eq__(self, other):
29
        return (isinstance(other, LogMessage) and
30
                other.log_level == self.log_level and
31
                other.message == self.message)
32
33
    def __ne__(self, other):
34
        return not self.__eq__(other)
35
36
    def to_string_dict(self):
37
        """
38
        Makes a dictionary which has all keys and values as strings and
39
        contains all the data that the LogMessage has.
40
41
        :return: Dictionary with keys and values as string.
42
        """
43
        retval = {}
44
45
        retval["message"] = str(self.message)
46
        retval["timestamp"] = ("" if self.timestamp == None
47
                               else self.timestamp.isoformat())
48
        retval["log_level"] = str(LOG_LEVEL.reverse.get(self.log_level, ""))
49
50
        return retval
51