Skip to content

Commit d61dc59

Browse files
authored
Merge pull request #18 from dastels/master
Null Logger
2 parents d3885b7 + 055a8c0 commit d61dc59

File tree

2 files changed

+71
-8
lines changed

2 files changed

+71
-8
lines changed

adafruit_logging.py

Lines changed: 50 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -95,28 +95,35 @@ def emit(self, level, msg):
9595
# pylint:disable=undefined-variable
9696

9797
logger_cache = dict()
98+
null_logger = None
9899

99-
100+
# pylint:disable=global-statement
100101
def getLogger(name):
101102
"""Create or retrieve a logger by name.
102103
103-
:param name: the name of the logger to create/retrieve
104+
:param name: the name of the logger to create/retrieve None will cause the
105+
NullLogger instance to be returned.
104106
105107
"""
108+
global null_logger
109+
if not name or name == "":
110+
if not null_logger:
111+
null_logger = NullLogger()
112+
return null_logger
113+
106114
if name not in logger_cache:
107115
logger_cache[name] = Logger()
108116
return logger_cache[name]
109117

110118

119+
# pylint:enable=global-statement
120+
121+
111122
class Logger:
112123
"""Provide a logging api."""
113124

114125
def __init__(self):
115-
"""Create an instance.
116-
117-
:param handler: what to use to output messages. Defaults to a PrintHandler.
118-
119-
"""
126+
"""Create an instance."""
120127
self._level = NOTSET
121128
self._handler = PrintHandler()
122129

@@ -201,3 +208,39 @@ def critical(self, format_string, *args):
201208
202209
"""
203210
self.log(CRITICAL, format_string, *args)
211+
212+
213+
class NullLogger:
214+
"""Provide an empty logger.
215+
This can be used in place of a real logger to more efficiently disable logging."""
216+
217+
def __init__(self):
218+
"""Dummy implementation."""
219+
220+
def setLevel(self, value):
221+
"""Dummy implementation."""
222+
223+
def getEffectiveLevel(self):
224+
"""Dummy implementation."""
225+
return NOTSET
226+
227+
def addHandler(self, hldr):
228+
"""Dummy implementation."""
229+
230+
def log(self, level, format_string, *args):
231+
"""Dummy implementation."""
232+
233+
def debug(self, format_string, *args):
234+
"""Dummy implementation."""
235+
236+
def info(self, format_string, *args):
237+
"""Dummy implementation."""
238+
239+
def warning(self, format_string, *args):
240+
"""Dummy implementation."""
241+
242+
def error(self, format_string, *args):
243+
"""Dummy implementation."""
244+
245+
def critical(self, format_string, *args):
246+
"""Dummy implementation."""

examples/logging_simpletest.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,32 @@
22
# SPDX-License-Identifier: MIT
33

44
# pylint:disable=undefined-variable,wildcard-import,no-name-in-module
5-
# pylint:disable=no-member
5+
# pylint:disable=no-member,invalid-name
6+
7+
"""Briefly exercise the logger and null logger."""
68

79
import adafruit_logging as logging
810

11+
# This should produce an error output
12+
913
logger = logging.getLogger("test")
1014

1115
logger.setLevel(logging.ERROR)
1216
logger.info("Info message")
1317
logger.error("Error message")
18+
19+
# This should produce no output
20+
21+
null_logger = logging.getLogger(None)
22+
23+
null_logger.setLevel(logging.ERROR)
24+
null_logger.info("Info message")
25+
null_logger.error("Error message")
26+
27+
# This should produce no output
28+
29+
null_logger = logging.getLogger("")
30+
31+
null_logger.setLevel(logging.ERROR)
32+
null_logger.info("Info message")
33+
null_logger.error("Error message")

0 commit comments

Comments
 (0)