A logger adapter (wrapper) for :class:`.Identified` subclasses. This allows multiple instances (e.g. Engine or Pool instances) to share a logger, but have its verbosity controlled on a per-instance basis. The basic functionality is to return a logging level which is based on an
| 101 | |
| 102 | |
| 103 | class InstanceLogger: |
| 104 | """A logger adapter (wrapper) for :class:`.Identified` subclasses. |
| 105 | |
| 106 | This allows multiple instances (e.g. Engine or Pool instances) |
| 107 | to share a logger, but have its verbosity controlled on a |
| 108 | per-instance basis. |
| 109 | |
| 110 | The basic functionality is to return a logging level |
| 111 | which is based on an instance's echo setting. |
| 112 | |
| 113 | Default implementation is: |
| 114 | |
| 115 | 'debug' -> logging.DEBUG |
| 116 | True -> logging.INFO |
| 117 | False -> Effective level of underlying logger ( |
| 118 | logging.WARNING by default) |
| 119 | None -> same as False |
| 120 | """ |
| 121 | |
| 122 | # Map echo settings to logger levels |
| 123 | _echo_map = { |
| 124 | None: logging.NOTSET, |
| 125 | False: logging.NOTSET, |
| 126 | True: logging.INFO, |
| 127 | "debug": logging.DEBUG, |
| 128 | } |
| 129 | |
| 130 | _echo: _EchoFlagType |
| 131 | |
| 132 | __slots__ = ("echo", "logger") |
| 133 | |
| 134 | def __init__(self, echo: _EchoFlagType, name: str): |
| 135 | self.echo = echo |
| 136 | self.logger = logging.getLogger(name) |
| 137 | |
| 138 | # if echo flag is enabled and no handlers, |
| 139 | # add a handler to the list |
| 140 | if self._echo_map[echo] <= logging.INFO and not self.logger.handlers: |
| 141 | _add_default_handler(self.logger) |
| 142 | |
| 143 | # |
| 144 | # Boilerplate convenience methods |
| 145 | # |
| 146 | def debug(self, msg: str, *args: Any, **kwargs: Any) -> None: |
| 147 | """Delegate a debug call to the underlying logger.""" |
| 148 | |
| 149 | self.log(logging.DEBUG, msg, *args, **kwargs) |
| 150 | |
| 151 | def info(self, msg: str, *args: Any, **kwargs: Any) -> None: |
| 152 | """Delegate an info call to the underlying logger.""" |
| 153 | |
| 154 | self.log(logging.INFO, msg, *args, **kwargs) |
| 155 | |
| 156 | def warning(self, msg: str, *args: Any, **kwargs: Any) -> None: |
| 157 | """Delegate a warning call to the underlying logger.""" |
| 158 | |
| 159 | self.log(logging.WARNING, msg, *args, **kwargs) |
| 160 |