| Total Complexity | 10 |
| Total Lines | 45 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | from datetime import datetime |
||
| 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 |
Cyclic imports may cause partly loaded modules to be returned. This might lead to unexpected runtime behavior which is hard to debug.