comparison src/core/log.py @ 1017:0ea97f483464

core (log): added "custom" backend
author Goffi <goffi@goffi.org>
date Thu, 08 May 2014 19:11:20 +0200
parents 0c361fdc76af
children a836b6da2c5c
comparison
equal deleted inserted replaced
1016:0c361fdc76af 1017:0ea97f483464
240 out = message 240 out = message
241 return ''.join(out) 241 return ''.join(out)
242 242
243 243
244 class Configure(object): 244 class Configure(object):
245 LOGGER_CLASS = None 245 LOGGER_CLASS = Logger
246 246
247 def __init__(self, level=None, fmt=None, output=None, logger=None, colors=False, force_colors=False): 247 def __init__(self, level=None, fmt=None, output=None, logger=None, colors=False, force_colors=False):
248 """Configure backend 248 """Configure backend
249 @param level: one of C.LOG_LEVELS 249 @param level: one of C.LOG_LEVELS
250 @param fmt: format string, pretty much as in std logging. Accept the following keywords (maybe more depending on backend): 250 @param fmt: format string, pretty much as in std logging. Accept the following keywords (maybe more depending on backend):
520 twisted_logger = getLogger(C.LOG_TWISTED_LOGGER) 520 twisted_logger = getLogger(C.LOG_TWISTED_LOGGER)
521 log_method = twisted_logger.error if event.get('isError', False) else twisted_logger.info 521 log_method = twisted_logger.error if event.get('isError', False) else twisted_logger.info
522 log_method(text.decode('utf-8')) 522 log_method(text.decode('utf-8'))
523 523
524 self.log_publisher._originalAddObserver(twistedObserver) 524 self.log_publisher._originalAddObserver(twistedObserver)
525
526
527 class ConfigureCustom(ConfigureBasic):
528 LOGGER_CLASS = None
529
530 def __init__(self, logger_class, *args, **kwargs):
531 ConfigureCustom.LOGGER_CLASS = logger_class
525 532
526 533
527 class ConfigureStandard(Configure): 534 class ConfigureStandard(Configure):
528 535
529 def __init__(self, level=None, fmt=None, output=None, logger=None, colors=False, force_colors=False): 536 def __init__(self, level=None, fmt=None, output=None, logger=None, colors=False, force_colors=False):
630 """Configure logging behaviour 637 """Configure logging behaviour
631 @param backend: can be: 638 @param backend: can be:
632 C.LOG_BACKEND_STANDARD: use standard logging module 639 C.LOG_BACKEND_STANDARD: use standard logging module
633 C.LOG_BACKEND_TWISTED: use twisted logging module (with standard logging observer) 640 C.LOG_BACKEND_TWISTED: use twisted logging module (with standard logging observer)
634 C.LOG_BACKEND_BASIC: use a basic print based logging 641 C.LOG_BACKEND_BASIC: use a basic print based logging
642 C.LOG_BACKEND_CUSTOM: use a given Logger subclass
635 """ 643 """
636 global _backend 644 global _backend
637 if _backend is not None: 645 if _backend is not None:
638 raise exceptions.InternalError("Logging can only be configured once") 646 raise exceptions.InternalError("Logging can only be configured once")
639 _backend = backend 647 _backend = backend
644 elif backend == C.LOG_BACKEND_TWISTED: 652 elif backend == C.LOG_BACKEND_TWISTED:
645 ConfigureTwisted(**options) 653 ConfigureTwisted(**options)
646 654
647 elif backend == C.LOG_BACKEND_STANDARD: 655 elif backend == C.LOG_BACKEND_STANDARD:
648 ConfigureStandard(**options) 656 ConfigureStandard(**options)
657
658 elif backend == C.LOG_BACKEND_CUSTOM:
659 logger_class = options.pop('logger_class')
660 ConfigureCustom(logger_class, **options)
649 661
650 else: 662 else:
651 raise ValueError("unknown backend") 663 raise ValueError("unknown backend")
652 664
653 def _parseOptions(options): 665 def _parseOptions(options):
702 _parseOptions(log_conf) 714 _parseOptions(log_conf)
703 configure(backend, **log_conf) 715 configure(backend, **log_conf)
704 716
705 def getLogger(name=C.LOG_BASE_LOGGER): 717 def getLogger(name=C.LOG_BASE_LOGGER):
706 if _backend in (None, C.LOG_BACKEND_BASIC): 718 if _backend in (None, C.LOG_BACKEND_BASIC):
707 logger_class = Logger 719 logger_class = ConfigureBasic.LOGGER_CLASS
708 elif _backend == C.LOG_BACKEND_TWISTED: 720 elif _backend == C.LOG_BACKEND_TWISTED:
709 logger_class = TwistedLogger 721 logger_class = ConfigureTwisted.LOGGER_CLASS
722 elif _backend == C.LOG_BACKEND_CUSTOM:
723 logger_class = ConfigureCustom.LOGGER_CLASS
710 else: 724 else:
711 raise ValueError("This method should not be called with backend [%s]" % _backend) 725 raise ValueError("This method should not be called with backend [%s]" % _backend)
712 return _loggers.setdefault(name, logger_class(name)) 726 return _loggers.setdefault(name, logger_class(name))
713 727
714 _root_logger = getLogger() 728 _root_logger = getLogger()