22
22
* Adafruit CircuitPython firmware for the supported boards:
23
23
https://github.com/adafruit/circuitpython/releases
24
24
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
+
25
49
"""
26
50
# pylint:disable=redefined-outer-name,consider-using-enumerate,no-self-use
27
51
# pylint:disable=invalid-name
30
54
31
55
__version__ = "0.0.0-auto.0"
32
56
__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
+ ]
33
75
34
76
35
77
LEVELS = [
41
83
(50 , "CRITICAL" ),
42
84
]
43
85
44
- for value , name in LEVELS :
45
- globals ()[name ] = value
86
+ for __value , __name in LEVELS :
87
+ globals ()[__name ] = __value
46
88
47
89
48
90
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.
50
92
51
- :param value: a numeric level
93
+ :param int value: a numeric level
52
94
53
95
"""
54
96
for i in range (len (LEVELS )):
@@ -62,33 +104,33 @@ def level_for(value: int) -> str:
62
104
class LoggingHandler :
63
105
"""Abstract logging message handler."""
64
106
65
- def format (self , level : int , msg : str ) -> str :
107
+ def format (self , log_level : int , message : str ) -> str :
66
108
"""Generate a timestamped message.
67
109
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
70
112
71
113
"""
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 )
73
115
74
- def emit (self , level : int , msg : str ):
116
+ def emit (self , log_level : int , message : str ):
75
117
"""Send a message where it should go.
76
- Place holder for subclass implementations.
118
+ Placeholder for subclass implementations.
77
119
"""
78
120
raise NotImplementedError ()
79
121
80
122
81
123
class PrintHandler (LoggingHandler ):
82
124
"""Send logging messages to the console by using print."""
83
125
84
- def emit (self , level : int , msg : str ):
126
+ def emit (self , log_level : int , message : str ):
85
127
"""Send a message to the console.
86
128
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
89
131
90
132
"""
91
- print (self .format (level , msg ))
133
+ print (self .format (log_level , message ))
92
134
93
135
94
136
# The level module-global variables get created when loaded
@@ -98,22 +140,22 @@ def emit(self, level: int, msg: str):
98
140
null_logger = None
99
141
100
142
# pylint:disable=global-statement
101
- def getLogger (name : str ) -> "Logger" :
143
+ def getLogger (logger_name : str ) -> "Logger" :
102
144
"""Create or retrieve a logger by name.
103
145
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.
106
148
107
149
"""
108
150
global null_logger
109
- if not name or name == "" :
151
+ if not logger_name or logger_name == "" :
110
152
if not null_logger :
111
153
null_logger = NullLogger ()
112
154
return null_logger
113
155
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 ]
117
159
118
160
119
161
# pylint:enable=global-statement
@@ -127,13 +169,13 @@ def __init__(self):
127
169
self ._level = NOTSET
128
170
self ._handler = PrintHandler ()
129
171
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.
132
174
133
- :param value : the lowest level to output
175
+ :param int log_level : the lowest level to output
134
176
135
177
"""
136
- self ._level = value
178
+ self ._level = log_level
137
179
138
180
def getEffectiveLevel (self ) -> int :
139
181
"""Get the effective level for this logger.
@@ -143,91 +185,98 @@ def getEffectiveLevel(self) -> int:
143
185
"""
144
186
return self ._level
145
187
146
- def addHandler (self , hldr : LoggingHandler ):
188
+ def addHandler (self , handler : LoggingHandler ):
147
189
"""Sets the handler of this logger to the specified handler.
148
190
*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.
150
192
151
- :param hldr : the handler
193
+ :param LoggingHandler handler : the handler
152
194
153
195
"""
154
- self ._handler = hldr
196
+ self ._handler = handler
155
197
156
- def log (self , level : int , format_string : str , * args ):
198
+ def log (self , log_level : int , format_string : str , * args ):
157
199
"""Log a message.
158
200
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
162
205
163
206
"""
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 )
166
209
167
210
def debug (self , format_string : str , * args ):
168
211
"""Log a debug message.
169
212
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
172
216
173
217
"""
174
218
self .log (DEBUG , format_string , * args )
175
219
176
220
def info (self , format_string : str , * args ):
177
221
"""Log a info message.
178
222
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
181
226
182
227
"""
183
228
self .log (INFO , format_string , * args )
184
229
185
230
def warning (self , format_string : str , * args ):
186
231
"""Log a warning message.
187
232
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
190
236
191
237
"""
192
238
self .log (WARNING , format_string , * args )
193
239
194
240
def error (self , format_string : str , * args ):
195
241
"""Log a error message.
196
242
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
199
246
200
247
"""
201
248
self .log (ERROR , format_string , * args )
202
249
203
250
def critical (self , format_string : str , * args ):
204
251
"""Log a critical message.
205
252
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
208
256
209
257
"""
210
258
self .log (CRITICAL , format_string , * args )
211
259
212
260
213
261
class NullLogger :
214
262
"""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."""
216
265
217
266
def __init__ (self ):
218
267
"""Dummy implementation."""
219
268
220
- def setLevel (self , value : int ):
269
+ def setLevel (self , log_level : int ):
221
270
"""Dummy implementation."""
222
271
223
272
def getEffectiveLevel (self ) -> int :
224
273
"""Dummy implementation."""
225
274
return NOTSET
226
275
227
- def addHandler (self , hldr : LoggingHandler ):
276
+ def addHandler (self , handler : LoggingHandler ):
228
277
"""Dummy implementation."""
229
278
230
- def log (self , level : int , format_string : str , * args ):
279
+ def log (self , log_level : int , format_string : str , * args ):
231
280
"""Dummy implementation."""
232
281
233
282
def debug (self , format_string : str , * args ):
0 commit comments