-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlog.py
More file actions
172 lines (143 loc) · 5.22 KB
/
log.py
File metadata and controls
172 lines (143 loc) · 5.22 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import time
import logging
import threading
class Log(object):
_loggers = {}
CRITICAL=logging.CRITICAL
ERROR=logging.ERROR
WARNING=logging.WARNING
INFO=logging.INFO
DEBUG=logging.DEBUG
NOTSET=logging.NOTSET
chosen_level=logging.INFO
COLOURS = {
"critical": "\033[1;31;40m", # red
"error": "\033[1;33;40m", # yellow
"warning": "\033[1;35;40m", # purple
"info": "\033[1;37;40m", # white
"debug": "\033[1;37;40m" # grey
}
@staticmethod
def debug(tag, message=None):
"""
Post a debug-level message.
:param String tag: tag for the log message.
:param String message: message to post, if one is not provided, the tag
is used as the message instead.
:return: None
"""
Log._post("debug", tag, message)
@staticmethod
def info(tag, message=None):
"""
Post an info-level message.
:param String tag: tag for the log message.
:param String message: message to post, if one is not provided, the tag
is used as the message instead.
:return: None
"""
Log._post("info", tag, message)
@staticmethod
def warning(tag, message=None):
"""
Post a warning-level message.
:param String tag: tag for the log message.
:param String message: message to post, if one is not provided, the tag
is used as the message instead.
:return: None
"""
Log._post("warning", tag, message)
@staticmethod
def error(tag, message=None):
"""
Post an error-level message.
:param String tag: tag for the log message.
:param String message: message to post, if one is not provided, the tag
is used as the message instead.
:return: None
"""
Log._post("error", tag, message)
@staticmethod
def critical(tag, message=None):
"""
Post a critical-level message.
:param String tag: tag for the log message.
:param String message: message to post, if one is not provided, the tag
is used as the message instead.
:return: None
"""
Log._post("critical", tag, message)
@staticmethod
def log(tag, message=None):
"""
Post a log-level message.
:param String tag: tag for the log message.
:param String message: message to post, if one is not provided, the tag
is used as the message instead.
:return: None
"""
Log._post("log", tag, message)
@staticmethod
def exception(tag, message=None):
"""
Post an exception-level message.
:param String tag: tag for the log message.
:param String message: message to post, if one is not provided, the tag
is used as the message instead.
:return: None
"""
Log._post("exception", tag, message)
@staticmethod
def init(level):
"""
Initiate the logging system.
:param int level: level to set for the logger (only applies on first
call, can't be changed once logger is created).
:return: Logger
"""
Log.chosen_level = level
logging.basicConfig(
format="%(levelname)s\t%(name)s\t%(asctime)s\t%(message)s",
level=level)
@staticmethod
def _post(level, tag, message=None):
"""
Post a message to a logger of a given tag at the given level.
:param String tag: tag for the log message.
:param String level: level of the log message, as a lowercase String,
:param String message: message to post, the message is posted as-is, but
in the right colour.
:return: None
"""
if message == None:
message = tag
tag = "hotword"
message = "%s%s\033[0;37;40m" % (Log.COLOURS[level], message)
logger = Log._get_logger(level, tag)
method = getattr(logger, level)
method(Log._message(message))
@staticmethod
def _get_logger(level, tag):
"""
Retrieve a Logger for a given tag.
:param int level: level to set for the logger (only applies on first
call, can't be changed once logger is created).
:param String tag: tag for the log message.
:return: Logger
"""
try:
return Log._loggers[tag]
except KeyError:
Log._loggers[tag] = logging.getLogger(tag)
Log._loggers[tag].setLevel(Log.chosen_level)
return Log._loggers[tag]
@staticmethod
def _message(message):
"""
Augment a message by including Thread information.
:param String message: message to post, adds time and Thread information
to the String.
:return: String
"""
str_thread = "Thread-%d" % threading.current_thread().ident
return "%s\t%s" % (str_thread, message)