Logging   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 18
c 2
b 0
f 0
dl 0
loc 75
rs 10

12 Methods

Rating   Name   Duplication   Size   Complexity  
A debug() 0 2 1
A __call__() 0 9 4
A __logtime() 0 11 1
A error() 0 2 1
A msg_format() 0 5 1
A warning() 0 2 1
A save_msg() 0 5 3
A __init__() 0 4 1
A get_log_level() 0 4 2
A log() 0 2 1
A info() 0 2 1
A critical() 0 2 1
1
from sys import stdout
2
import time
3
4
5
def asctime(t=None):
6
    """
7
    Converts the 8-tuple which contains:
8
    (year, month, mday, hour, minute, second, weekday, yearday)
9
    into a string in the form:
10
    'Sun Sep 16 01:03:52 1973\n'
11
    """
12
13
    t = list(t or time.lcaltime())
14
    month_name = [
15
        "Jan", "Feb", "Mar", "Apr", "May", "Jun",
16
        "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
17
    ]
18
19
    day_name = [
20
        "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
21
    ]
22
23
    t[1] = month_name[t[1]]
24
    t[6] = day_name[t[6]]
25
    result = "{6} {1} {2:02} {3:02}:{4:02}:{5:02} {0}\n".format(*t)
26
    return result
27
28
29
class Logging(object):
30
    entry_format = "{time} : {pfx} : {log_lvl} : {msg}\n"
31
32
    prefix = 'OBDLIB'
33
34
    __logging_levels = (
35
        'CRITICAL',  # 0
36
        'ERROR',  # 1
37
        'WARNING',  # 2
38
        'INFO',  # 3
39
        'DEBUG',  # 4
40
        'NOTSET',  # 5,-1
41
    )
42
43
    def __logtime(self, t=None):
44
        """
45
        Converts the 8-tuple which contains:
46
        (year, month, mday, hour, minute, second, weekday, yearday)
47
        into a string in the form:
48
        '1981-05-31 01:03:59'
49
        """
50
51
        t = t or time.localtime()
52
        result = "{0:04}-{1:02}-{2:02} {3:02}:{4:02}:{5:02}".format(*t)
53
        return result
54
55
    def get_log_level(self, log_level):
56
        if log_level > len(self.__logging_levels) - 1:
57
            log_level = -1
58
        return self.__logging_levels[log_level]
59
60
    def __init__(self, log_level=1, duplicate_in_stdout=False, output=None):
61
        self.use_stdout = duplicate_in_stdout
62
        self.output_stream = output
63
        self.log_level = log_level
64
65
    def __call__(self, msg, level=5, force=False):
66
        if level > self.log_level:
67
            return
68
        out_msg = self.msg_format(msg, level)
69
70
        self.save_msg(self.output_stream, out_msg)
71
72
        if self.use_stdout or force:
73
            stdout.write(out_msg)
74
75
    def save_msg(self, stream, msg):
76
        if self.output_stream:
77
            # saves logging message
78
            with open(self.output_stream, 'wb') as stream:
79
                stream.write(msg)
80
81
    def msg_format(self, msg, level):
82
        return self.entry_format.format(time=self.__logtime(),
83
                                        pfx=self.prefix,
84
                                        log_lvl=self.get_log_level(level),
85
                                        msg=str(msg))
86
87
    def critical(self, msg):
88
        return self(msg, 0)
89
90
    def error(self, msg):
91
        return self(msg, 1)
92
93
    def warning(self, msg):
94
        return self(msg, 2)
95
96
    def info(self, msg):
97
        return self(msg, 3)
98
99
    def debug(self, msg):
100
        return self(msg, 4)
101
102
    def log(self, msg):
103
        return self(msg, 5)
104
105
106
logger = Logging(log_level=2)
107