-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple_logging.py
77 lines (49 loc) · 1.42 KB
/
simple_logging.py
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import datetime
import inspect
import os
ERROR = 0
WARNING = 1
INFO = 2
DEBUG = 3
KINDS = ['ERROR', 'WARNING', 'INFO', 'DEBUG']
LINE = '=' * 120
LOG_LEVEL = INFO
def log_impl(msg):
print(msg)
def log(level, *msg):
# type(int, str) -> None
if level <= LOG_LEVEL:
when = datetime.datetime.utcnow().strftime('%H:%M:%S')
log_impl('%s %s %s: %s' % (caller_info(), when, KINDS[level], ' '.join(map(str,msg))))
def warning(*msg):
# type(str) -> None
log(WARNING, *msg)
def error(*msg):
# type(str) -> None
log(ERROR, *msg)
def info(*msg):
# type(str) -> None
log(INFO, *msg)
def debug(*msg):
# type(str) -> None
log(DEBUG, *msg)
def caller_info(skip=3):
frame = inspect.stack()[skip][0]
path = inspect.getfile(frame)
base = os.path.dirname(__file__)
return '%s:%d' % (path[len(base) + 1:], inspect.getlineno(frame))
def set_level(level):
global LOG_LEVEL
LOG_LEVEL = level
def get_level():
return LOG_LEVEL
if __name__ == '__main__':
set_level(WARNING)
warning('Tell the user about something potentially harmful.')
set_level(DEBUG)
debug('Show a highly verbose internal debugging message.')
set_level(INFO)
info('Report end-user level events.')
info('Arg1', 'Arg2', 'Arg3', 1, 2, 3, True, '- Just some multiple args')
set_level(ERROR)
error('Something seriously wrong happened, execution probably ends now.')