@@ -95,28 +95,35 @@ def emit(self, level, msg):
95
95
# pylint:disable=undefined-variable
96
96
97
97
logger_cache = dict ()
98
+ null_logger = None
98
99
99
-
100
+ # pylint:disable=global-statement
100
101
def getLogger (name ):
101
102
"""Create or retrieve a logger by name.
102
103
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.
104
106
105
107
"""
108
+ global null_logger
109
+ if not name or name == "" :
110
+ if not null_logger :
111
+ null_logger = NullLogger ()
112
+ return null_logger
113
+
106
114
if name not in logger_cache :
107
115
logger_cache [name ] = Logger ()
108
116
return logger_cache [name ]
109
117
110
118
119
+ # pylint:enable=global-statement
120
+
121
+
111
122
class Logger :
112
123
"""Provide a logging api."""
113
124
114
125
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."""
120
127
self ._level = NOTSET
121
128
self ._handler = PrintHandler ()
122
129
@@ -201,3 +208,39 @@ def critical(self, format_string, *args):
201
208
202
209
"""
203
210
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