Skip to content

Commit 7a188fe

Browse files
authored
Merge pull request #19 from garrettheath4/document-dynamic-levels
Patch: Document metaprogrammed module attributes
2 parents 34bcc68 + a31c6a8 commit 7a188fe

File tree

2 files changed

+102
-53
lines changed

2 files changed

+102
-53
lines changed

adafruit_logging/__init__.py

+98-49
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,30 @@
2222
* Adafruit CircuitPython firmware for the supported boards:
2323
https://github.com/adafruit/circuitpython/releases
2424
25+
Attributes
26+
----------
27+
LEVELS : list
28+
A list of tuples representing the valid logging levels used by
29+
this module. Each tuple contains exactly two elements: one int and one
30+
str. The int in each tuple represents the relative severity of that
31+
level (00 to 50). The str in each tuple is the string representation of
32+
that logging level ("NOTSET" to "CRITICAL"; see below).
33+
NOTSET : int
34+
The NOTSET logging level, which is a dummy logging level that can be
35+
used to indicate that a `Logger` should not print any logging messages,
36+
regardless of how severe those messages might be (including CRITICAL).
37+
DEBUG : int
38+
The DEBUG logging level, which is the lowest (least severe) real level.
39+
INFO : int
40+
The INFO logging level for informative/informational messages.
41+
WARNING : int
42+
The WARNING logging level for warnings that should be addressed/fixed.
43+
ERROR : int
44+
The ERROR logging level for Python exceptions that occur during runtime.
45+
CRITICAL : int
46+
The CRITICAL logging level, which is the highest (most severe) level for
47+
unrecoverable errors that have caused the code to halt and exit.
48+
2549
"""
2650
# pylint:disable=redefined-outer-name,consider-using-enumerate,no-self-use
2751
# pylint:disable=invalid-name
@@ -30,6 +54,24 @@
3054

3155
__version__ = "0.0.0-auto.0"
3256
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_Logger.git"
57+
# pylint:disable=undefined-all-variable
58+
__all__ = [
59+
"LEVELS",
60+
"NOTSET",
61+
"DEBUG",
62+
"INFO",
63+
"WARNING",
64+
"ERROR",
65+
"CRITICAL",
66+
"level_for",
67+
"LoggingHandler",
68+
"PrintHandler",
69+
"logger_cache",
70+
"null_logger",
71+
"getLogger",
72+
"Logger",
73+
"NullLogger",
74+
]
3375

3476

3577
LEVELS = [
@@ -41,14 +83,14 @@
4183
(50, "CRITICAL"),
4284
]
4385

44-
for value, name in LEVELS:
45-
globals()[name] = value
86+
for __value, __name in LEVELS:
87+
globals()[__name] = __value
4688

4789

4890
def level_for(value: int) -> str:
49-
"""Convert a numberic level to the most appropriate name.
91+
"""Convert a numeric level to the most appropriate name.
5092
51-
:param value: a numeric level
93+
:param int value: a numeric level
5294
5395
"""
5496
for i in range(len(LEVELS)):
@@ -62,33 +104,33 @@ def level_for(value: int) -> str:
62104
class LoggingHandler:
63105
"""Abstract logging message handler."""
64106

65-
def format(self, level: int, msg: str) -> str:
107+
def format(self, log_level: int, message: str) -> str:
66108
"""Generate a timestamped message.
67109
68-
:param level: the logging level
69-
:param msg: the message to log
110+
:param int log_level: the logging level
111+
:param str message: the message to log
70112
71113
"""
72-
return "{0}: {1} - {2}".format(time.monotonic(), level_for(level), msg)
114+
return "{0}: {1} - {2}".format(time.monotonic(), level_for(log_level), message)
73115

74-
def emit(self, level: int, msg: str):
116+
def emit(self, log_level: int, message: str):
75117
"""Send a message where it should go.
76-
Place holder for subclass implementations.
118+
Placeholder for subclass implementations.
77119
"""
78120
raise NotImplementedError()
79121

80122

81123
class PrintHandler(LoggingHandler):
82124
"""Send logging messages to the console by using print."""
83125

84-
def emit(self, level: int, msg: str):
126+
def emit(self, log_level: int, message: str):
85127
"""Send a message to the console.
86128
87-
:param level: the logging level
88-
:param msg: the message to log
129+
:param int log_level: the logging level
130+
:param str message: the message to log
89131
90132
"""
91-
print(self.format(level, msg))
133+
print(self.format(log_level, message))
92134

93135

94136
# The level module-global variables get created when loaded
@@ -98,22 +140,22 @@ def emit(self, level: int, msg: str):
98140
null_logger = None
99141

100142
# pylint:disable=global-statement
101-
def getLogger(name: str) -> "Logger":
143+
def getLogger(logger_name: str) -> "Logger":
102144
"""Create or retrieve a logger by name.
103145
104-
:param name: the name of the logger to create/retrieve None will cause the
105-
NullLogger instance to be returned.
146+
:param str logger_name: The name of the `Logger` to create/retrieve. `None`
147+
will cause the `NullLogger` instance to be returned.
106148
107149
"""
108150
global null_logger
109-
if not name or name == "":
151+
if not logger_name or logger_name == "":
110152
if not null_logger:
111153
null_logger = NullLogger()
112154
return null_logger
113155

114-
if name not in logger_cache:
115-
logger_cache[name] = Logger()
116-
return logger_cache[name]
156+
if logger_name not in logger_cache:
157+
logger_cache[logger_name] = Logger()
158+
return logger_cache[logger_name]
117159

118160

119161
# pylint:enable=global-statement
@@ -127,13 +169,13 @@ def __init__(self):
127169
self._level = NOTSET
128170
self._handler = PrintHandler()
129171

130-
def setLevel(self, value: int):
131-
"""Set the logging cuttoff level.
172+
def setLevel(self, log_level: int):
173+
"""Set the logging cutoff level.
132174
133-
:param value: the lowest level to output
175+
:param int log_level: the lowest level to output
134176
135177
"""
136-
self._level = value
178+
self._level = log_level
137179

138180
def getEffectiveLevel(self) -> int:
139181
"""Get the effective level for this logger.
@@ -143,91 +185,98 @@ def getEffectiveLevel(self) -> int:
143185
"""
144186
return self._level
145187

146-
def addHandler(self, hldr: LoggingHandler):
188+
def addHandler(self, handler: LoggingHandler):
147189
"""Sets the handler of this logger to the specified handler.
148190
*NOTE* this is slightly different from the CPython equivalent which adds
149-
the handler rather than replaceing it.
191+
the handler rather than replacing it.
150192
151-
:param hldr: the handler
193+
:param LoggingHandler handler: the handler
152194
153195
"""
154-
self._handler = hldr
196+
self._handler = handler
155197

156-
def log(self, level: int, format_string: str, *args):
198+
def log(self, log_level: int, format_string: str, *args):
157199
"""Log a message.
158200
159-
:param level: the priority level at which to log
160-
:param format_string: the core message string with embedded formatting directives
161-
:param args: arguments to ``format_string.format()``, can be empty
201+
:param int log_level: the priority level at which to log
202+
:param str format_string: the core message string with embedded
203+
formatting directives
204+
:param args: arguments to ``format_string.format()``; can be empty
162205
163206
"""
164-
if level >= self._level:
165-
self._handler.emit(level, format_string % args)
207+
if log_level >= self._level:
208+
self._handler.emit(log_level, format_string % args)
166209

167210
def debug(self, format_string: str, *args):
168211
"""Log a debug message.
169212
170-
:param format_string: the core message string with embedded formatting directives
171-
:param args: arguments to ``format_string.format()``, can be empty
213+
:param str format_string: the core message string with embedded
214+
formatting directives
215+
:param args: arguments to ``format_string.format()``; can be empty
172216
173217
"""
174218
self.log(DEBUG, format_string, *args)
175219

176220
def info(self, format_string: str, *args):
177221
"""Log a info message.
178222
179-
:param format_string: the core message string with embedded formatting directives
180-
:param args: arguments to ``format_string.format()``, can be empty
223+
:param str format_string: the core message string with embedded
224+
formatting directives
225+
:param args: arguments to ``format_string.format()``; can be empty
181226
182227
"""
183228
self.log(INFO, format_string, *args)
184229

185230
def warning(self, format_string: str, *args):
186231
"""Log a warning message.
187232
188-
:param format_string: the core message string with embedded formatting directives
189-
:param args: arguments to ``format_string.format()``, can be empty
233+
:param str format_string: the core message string with embedded
234+
formatting directives
235+
:param args: arguments to ``format_string.format()``; can be empty
190236
191237
"""
192238
self.log(WARNING, format_string, *args)
193239

194240
def error(self, format_string: str, *args):
195241
"""Log a error message.
196242
197-
:param format_string: the core message string with embedded formatting directives
198-
:param args: arguments to ``format_string.format()``, can be empty
243+
:param str format_string: the core message string with embedded
244+
formatting directives
245+
:param args: arguments to ``format_string.format()``; can be empty
199246
200247
"""
201248
self.log(ERROR, format_string, *args)
202249

203250
def critical(self, format_string: str, *args):
204251
"""Log a critical message.
205252
206-
:param format_string: the core message string with embedded formatting directives
207-
:param args: arguments to ``format_string.format()``, can be empty
253+
:param str format_string: the core message string with embedded
254+
formatting directives
255+
:param args: arguments to ``format_string.format()``; can be empty
208256
209257
"""
210258
self.log(CRITICAL, format_string, *args)
211259

212260

213261
class NullLogger:
214262
"""Provide an empty logger.
215-
This can be used in place of a real logger to more efficiently disable logging."""
263+
This can be used in place of a real logger to more efficiently disable
264+
logging."""
216265

217266
def __init__(self):
218267
"""Dummy implementation."""
219268

220-
def setLevel(self, value: int):
269+
def setLevel(self, log_level: int):
221270
"""Dummy implementation."""
222271

223272
def getEffectiveLevel(self) -> int:
224273
"""Dummy implementation."""
225274
return NOTSET
226275

227-
def addHandler(self, hldr: LoggingHandler):
276+
def addHandler(self, handler: LoggingHandler):
228277
"""Dummy implementation."""
229278

230-
def log(self, level: int, format_string: str, *args):
279+
def log(self, log_level: int, format_string: str, *args):
231280
"""Dummy implementation."""
232281

233282
def debug(self, format_string: str, *args):

adafruit_logging/extensions.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,18 @@ def close(self):
3434
"""Closes the file"""
3535
self.logfile.close()
3636

37-
def format(self, level: int, msg: str):
37+
def format(self, log_level: int, message: str):
3838
"""Generate a string to log
3939
4040
:param level: The level of the message
4141
:param msg: The message to format
4242
"""
43-
return super().format(level, msg) + "\r\n"
43+
return super().format(log_level, message) + "\r\n"
4444

45-
def emit(self, level: int, msg: str):
45+
def emit(self, log_level: int, message: str):
4646
"""Generate the message and write it to the UART.
4747
4848
:param level: The level of the message
4949
:param msg: The message to log
5050
"""
51-
self.logfile.write(self.format(level, msg))
51+
self.logfile.write(self.format(log_level, message))

0 commit comments

Comments
 (0)