Mercurial > libervia-backend
comparison src/core/log.py @ 1010:73a0b7f94674
primitivus: use of new logging system:
- default output is \\memory
- logs can be seen with :messages command
- now useless writeLog method has been removed
author | Goffi <goffi@goffi.org> |
---|---|
date | Mon, 05 May 2014 20:12:19 +0200 |
parents | d70d4fe5c5f8 |
children | c8771279497e |
comparison
equal
deleted
inserted
replaced
1009:d1084f7e56a5 | 1010:73a0b7f94674 |
---|---|
167 pass | 167 pass |
168 log_record = LogRecord() | 168 log_record = LogRecord() |
169 log_record.name = dict_record['name'] | 169 log_record.name = dict_record['name'] |
170 return self.filter(log_record) == 1 | 170 return self.filter(log_record) == 1 |
171 | 171 |
172 def memoryGet(size=None): | |
173 """Return buffered logs | |
174 | |
175 @param size: number of logs to return | |
176 """ | |
177 if not C.LOG_OPT_OUTPUT_MEMORY in _handlers: | |
178 raise ValueError('memory output is not used') | |
179 if _backend == C.LOG_BACKEND_STANDARD: | |
180 mem_handler = _handlers[C.LOG_OPT_OUTPUT_MEMORY] | |
181 return (log_msg for log_msg in mem_handler.buffer[size if size is None else -size:]) | |
182 else: | |
183 raise NotImplementedError | |
172 | 184 |
173 def _getProfile(): | 185 def _getProfile(): |
174 """Try to find profile value using introspection""" | 186 """Try to find profile value using introspection""" |
175 import inspect | 187 import inspect |
176 stack = inspect.stack() | 188 stack = inspect.stack() |
507 self.log_publisher._originalAddObserver(twistedObserver) | 519 self.log_publisher._originalAddObserver(twistedObserver) |
508 | 520 |
509 | 521 |
510 class ConfigureStandard(Configure): | 522 class ConfigureStandard(Configure): |
511 | 523 |
512 def __init__(self, level=None, fmt=C.LOG_OPT_FORMAT[1], output=C.LOG_OPT_OUTPUT[1], logger=None, colors=False, force_colors=False): | 524 def __init__(self, level=None, fmt=None, output=None, logger=None, colors=False, force_colors=False): |
525 if fmt is None: | |
526 fmt = C.LOG_OPT_FORMAT[1] | |
527 if output is None: | |
528 output = C.LOG_OPT_OUTPUT[1] | |
513 super(ConfigureStandard, self).__init__(level, fmt, output, logger, colors, force_colors) | 529 super(ConfigureStandard, self).__init__(level, fmt, output, logger, colors, force_colors) |
514 | 530 |
515 def preTreatment(self): | 531 def preTreatment(self): |
516 """We use logging methods directly, instead of using Logger""" | 532 """We use logging methods directly, instead of using Logger""" |
517 global getLogger | 533 global getLogger |
557 def configureLogger(self, logger): | 573 def configureLogger(self, logger): |
558 self.name_filter = FilterName(logger) if logger else None | 574 self.name_filter = FilterName(logger) if logger else None |
559 | 575 |
560 def configureColors(self, colors, force_colors): | 576 def configureColors(self, colors, force_colors): |
561 import sys | 577 import sys |
562 self.with_color = colors & (sys.stdout.isatty() or force_colors) | 578 self.formatter.with_color = colors & (sys.stdout.isatty() or force_colors) |
563 if not colors and force_colors: | 579 if not colors and force_colors: |
564 raise ValueError("force_colors can't be used if colors is False") | 580 raise ValueError("force_colors can't be used if colors is False") |
565 | 581 |
566 def _addHandler(self, root_logger, hdlr): | 582 def _addHandler(self, root_logger, hdlr): |
567 hdlr.setFormatter(self.formatter) | 583 hdlr.setFormatter(self.formatter) |
577 for handler, options in _handlers.items(): | 593 for handler, options in _handlers.items(): |
578 if handler == C.LOG_OPT_OUTPUT_DEFAULT: | 594 if handler == C.LOG_OPT_OUTPUT_DEFAULT: |
579 hdlr = logging.StreamHandler() | 595 hdlr = logging.StreamHandler() |
580 self._addHandler(root_logger, hdlr) | 596 self._addHandler(root_logger, hdlr) |
581 elif handler == C.LOG_OPT_OUTPUT_MEMORY: | 597 elif handler == C.LOG_OPT_OUTPUT_MEMORY: |
582 import logging.handlers | 598 from logging.handlers import BufferingHandler |
583 hdlr = logging.handlers.BufferingHandler(options) | 599 class SatMemoryHandler(BufferingHandler): |
600 def emit(self, record): | |
601 super(SatMemoryHandler, self).emit(self.format(record)) | |
602 hdlr = SatMemoryHandler(options) | |
584 _handlers[handler] = hdlr # we keep a reference to the handler to read the buffer later | 603 _handlers[handler] = hdlr # we keep a reference to the handler to read the buffer later |
585 self._addHandler(root_logger, hdlr) | 604 self._addHandler(root_logger, hdlr) |
586 elif handler == C.LOG_OPT_OUTPUT_FILE: | 605 elif handler == C.LOG_OPT_OUTPUT_FILE: |
587 import os.path | 606 import os.path |
588 for path in options: | 607 for path in options: |
638 level = options[LEVEL].upper() | 657 level = options[LEVEL].upper() |
639 if level not in C.LOG_LEVELS: | 658 if level not in C.LOG_LEVELS: |
640 level = C.LOG_LVL_INFO | 659 level = C.LOG_LVL_INFO |
641 options[LEVEL] = level | 660 options[LEVEL] = level |
642 | 661 |
643 def satConfigure(backend=C.LOG_BACKEND_TWISTED): | 662 def satConfigure(backend=C.LOG_BACKEND_STANDARD, const=None): |
644 """Configure logging system for SàT, can be used by frontends | 663 """Configure logging system for SàT, can be used by frontends |
645 | 664 |
646 logs conf is read in SàT conf, then in environment variables. It must be done before Memory init | 665 logs conf is read in SàT conf, then in environment variables. It must be done before Memory init |
666 @param backend: backend to use, it can be: | |
667 - C.LOG_BACKEND_BASIC: print based backend | |
668 - C.LOG_BACKEND_TWISTED: Twisted logging backend | |
669 - C.LOG_BACKEND_STANDARD: standard logging backend | |
670 @param const: Const class to use instead of sat.core.constants.Const (mainly used to change default values) | |
647 """ | 671 """ |
672 if const is not None: | |
673 global C | |
674 C = const | |
648 import ConfigParser | 675 import ConfigParser |
649 import os | 676 import os |
650 log_conf = {} | 677 log_conf = {} |
651 config = ConfigParser.SafeConfigParser() | 678 config = ConfigParser.SafeConfigParser() |
652 config.read(C.CONFIG_FILES) | 679 config.read(C.CONFIG_FILES) |
653 for opt_name, opt_default in C.LOG_OPTIONS: | 680 for opt_name, opt_default in C.LOG_OPTIONS(): |
654 try: | 681 try: |
655 log_conf[opt_name] = os.environ[''.join((C.ENV_PREFIX, C.LOG_OPT_PREFIX.upper(), opt_name.upper()))] | 682 log_conf[opt_name] = os.environ[''.join((C.ENV_PREFIX, C.LOG_OPT_PREFIX.upper(), opt_name.upper()))] |
656 except KeyError: | 683 except KeyError: |
657 try: | 684 try: |
658 log_conf[opt_name] = config.get('DEFAULT', C.LOG_OPT_PREFIX + opt_name) | 685 log_conf[opt_name] = config.get(C.LOG_OPT_SECTION, C.LOG_OPT_PREFIX + opt_name) |
659 except ConfigParser.NoOptionError: | 686 except (ConfigParser.NoOptionError, ConfigParser.NoSectionError): |
660 log_conf[opt_name] = opt_default | 687 log_conf[opt_name] = opt_default |
661 | 688 |
662 _parseOptions(log_conf) | 689 _parseOptions(log_conf) |
663 configure(backend, **log_conf) | 690 configure(backend, **log_conf) |
664 | 691 |