Skip to content

Commit d0e984b

Browse files
author
Ask Solem
committed
utils.compat.LoggerAdapter: Support extra argument in Python 2.4.
Also added support for several new methods: fatal, makeRecord, _log, log, isEnabledFor, addHandler, removeHandler Closes celery#166. Thanks to denton-.
1 parent c9a07ae commit d0e984b

File tree

1 file changed

+54
-19
lines changed

1 file changed

+54
-19
lines changed

celery/utils/compat.py

Lines changed: 54 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -300,46 +300,81 @@ def __init__(self, logger, extra):
300300
self.logger = logger
301301
self.extra = extra
302302

303+
def setLevel(self, level):
304+
self.level = logging._checkLevel(level)
305+
303306
def process(self, msg, kwargs):
304307
kwargs["extra"] = self.extra
305308
return msg, kwargs
306309

307310
def debug(self, msg, *args, **kwargs):
308-
msg, kwargs = self.process(msg, kwargs)
309-
self.logger.debug(msg, *args, **kwargs)
311+
self.log(logging.DEBUG, msg, args, **kwargs)
310312

311313
def info(self, msg, *args, **kwargs):
312-
msg, kwargs = self.process(msg, kwargs)
313-
self.logger.info(msg, *args, **kwargs)
314+
self.log(logging.INFO, msg, *args, **kwargs)
314315

315316
def warning(self, msg, *args, **kwargs):
316-
msg, kwargs = self.process(msg, kwargs)
317-
self.logger.warning(msg, *args, **kwargs)
318-
319-
def warn(self, msg, *args, **kwargs):
320-
msg, kwargs = self.process(msg, kwargs)
321-
self.logger.warn(msg, *args, **kwargs)
317+
self.log(logging.WARNING, msg, *args, **kwargs)
318+
warn = warning
322319

323320
def error(self, msg, *args, **kwargs):
324-
msg, kwargs = self.process(msg, kwargs)
325-
self.logger.error(msg, *args, **kwargs)
321+
self.log(logging.ERROR, msg, *args, **kwargs)
326322

327323
def exception(self, msg, *args, **kwargs):
328-
msg, kwargs = self.process(msg, kwargs)
329-
kwargs["exc_info"] = 1
330-
self.logger.error(msg, *args, **kwargs)
324+
kwargs.setdefault("exc_info", 1)
325+
self.error(msg, *args, **kwargs)
331326

332327
def critical(self, msg, *args, **kwargs):
333-
msg, kwargs = self.process(msg, kwargs)
334-
self.logger.critical(msg, *args, **kwargs)
328+
self.log(logging.CRITICAL, msg, *args, **kwargs)
329+
fatal = critical
335330

336331
def log(self, level, msg, *args, **kwargs):
337-
msg, kwargs = self.process(msg, kwargs)
338-
self.logger.log(level, msg, *args, **kwargs)
332+
if self.logger.isEnabledFor(level):
333+
msg, kwargs = self.process(msg, kwargs)
334+
self._log(level, msg, *args, **kwargs)
335+
336+
def makeRecord(self, name, level, fn, lno, msg, args, exc_info,
337+
func=None, extra=None):
338+
rv = logging.LogRecord(name, level, fn, lno,
339+
msg, args, exc_info, func)
340+
if extra is not None:
341+
for key, value in extra.items():
342+
if key in ("message", "asctime") or key in rv.__dict__:
343+
raise KeyError(
344+
"Attempt to override %r in LogRecord" % key)
345+
rv.__dict__[key] = value
346+
return rv
347+
348+
def _log(self, level, msg, args, exc_info=None, extra=None):
349+
defcaller = "(unknown file)", 0, "(unknown function)"
350+
if logging._srcfile:
351+
# IronPython doesn't track Python frames, so findCaller
352+
# throws an exception on some versions of IronPython.
353+
# We trap it here so that IronPython can use logging.
354+
try:
355+
fn, lno, func = self.logger.findCaller()
356+
except ValueError:
357+
fn, lno, func = defcaller
358+
else:
359+
fn, lno, func = defcaller
360+
if exc_info:
361+
if not isinstance(exc_info, tuple):
362+
exc_info = sys.exc_info()
363+
record = self.makeRecord(self.logger.name, level, fn, lno, msg,
364+
args, exc_info, func, extra)
365+
self.logger.handle(record)
339366

340367
def isEnabledFor(self, level, *args, **kwargs):
341368
return self.logger.isEnabledFor(level, *args, **kwargs)
342369

370+
def addHandler(self, hdlr):
371+
self.logger.addHandler(hdlr)
372+
373+
def removeHandler(self, hdlr):
374+
self.logger.removeHandler(hdlr)
375+
376+
377+
343378
############## itertools.izip_longest #######################################
344379

345380
try:

0 commit comments

Comments
 (0)