Skip to content

Commit ea05e92

Browse files
committed
Add a NullLogger class for efficient disabling of logging
This implements the "Null Object" pattern. It allows logging to be completely and efficiently disabled without having to sprinkle "if logger" guards on every logger call (which can be error prone during maintenance).
1 parent d3885b7 commit ea05e92

File tree

2 files changed

+64
-6
lines changed

2 files changed

+64
-6
lines changed

adafruit_logging.py

+46-6
Original file line numberDiff line numberDiff line change
@@ -95,14 +95,22 @@ def emit(self, level, msg):
9595
# pylint:disable=undefined-variable
9696

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

99100

100101
def getLogger(name):
102+
global null_logger
101103
"""Create or retrieve a logger by name.
102104
103-
:param name: the name of the logger to create/retrieve
105+
:param name: the name of the logger to create/retrieve None will cause the
106+
NullLogger instance to be returned.
104107
105108
"""
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]
@@ -112,11 +120,7 @@ class Logger:
112120
"""Provide a logging api."""
113121

114122
def __init__(self):
115-
"""Create an instance.
116-
117-
:param handler: what to use to output messages. Defaults to a PrintHandler.
118-
119-
"""
123+
"""Create an instance."""
120124
self._level = NOTSET
121125
self._handler = PrintHandler()
122126

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

examples/logging_simpletest.py

+18
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,26 @@
66

77
import adafruit_logging as logging
88

9+
# This should produce an error output
10+
911
logger = logging.getLogger("test")
1012

1113
logger.setLevel(logging.ERROR)
1214
logger.info("Info message")
1315
logger.error("Error message")
16+
17+
# This should produce no output
18+
19+
null_logger = logging.getLogger(None)
20+
21+
null_logger.setLevel(logging.ERROR)
22+
null_logger.info("Info message")
23+
null_logger.error("Error message")
24+
25+
# This should produce no output
26+
27+
null_logger = logging.getLogger("")
28+
29+
null_logger.setLevel(logging.ERROR)
30+
null_logger.info("Info message")
31+
null_logger.error("Error message")

0 commit comments

Comments
 (0)