Configure a formatter from a dictionary.
(self, config)
| 686 | 'logger') from e |
| 687 | |
| 688 | def configure_formatter(self, config): |
| 689 | """Configure a formatter from a dictionary.""" |
| 690 | if '()' in config: |
| 691 | factory = config['()'] # for use in exception handler |
| 692 | try: |
| 693 | result = self.configure_custom(config) |
| 694 | except TypeError as te: |
| 695 | if "'format'" not in str(te): |
| 696 | raise |
| 697 | # logging.Formatter and its subclasses expect the `fmt` |
| 698 | # parameter instead of `format`. Retry passing configuration |
| 699 | # with `fmt`. |
| 700 | config['fmt'] = config.pop('format') |
| 701 | config['()'] = factory |
| 702 | result = self.configure_custom(config) |
| 703 | else: |
| 704 | fmt = config.get('format', None) |
| 705 | dfmt = config.get('datefmt', None) |
| 706 | style = config.get('style', '%') |
| 707 | cname = config.get('class', None) |
| 708 | defaults = config.get('defaults', None) |
| 709 | |
| 710 | if not cname: |
| 711 | c = logging.Formatter |
| 712 | else: |
| 713 | c = _resolve(cname) |
| 714 | |
| 715 | kwargs = {} |
| 716 | |
| 717 | # Add defaults only if it exists. |
| 718 | # Prevents TypeError in custom formatter callables that do not |
| 719 | # accept it. |
| 720 | if defaults is not None: |
| 721 | kwargs['defaults'] = defaults |
| 722 | |
| 723 | # A TypeError would be raised if "validate" key is passed in with a formatter callable |
| 724 | # that does not accept "validate" as a parameter |
| 725 | if 'validate' in config: # if user hasn't mentioned it, the default will be fine |
| 726 | result = c(fmt, dfmt, style, config['validate'], **kwargs) |
| 727 | else: |
| 728 | result = c(fmt, dfmt, style, **kwargs) |
| 729 | |
| 730 | return result |
| 731 | |
| 732 | def configure_filter(self, config): |
| 733 | """Configure a filter from a dictionary.""" |