@@ -95,28 +95,35 @@ def emit(self, level, msg):
9595# pylint:disable=undefined-variable
9696
9797logger_cache = dict ()
98+ null_logger = None
9899
99-
100+ # pylint:disable=global-statement
100101def 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+
111122class 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."""
0 commit comments