create a logger for an instance that implements :class:`.Identified`.
(
instance: Identified, echoflag: _EchoFlagType = None
)
| 221 | |
| 222 | |
| 223 | def instance_logger( |
| 224 | instance: Identified, echoflag: _EchoFlagType = None |
| 225 | ) -> None: |
| 226 | """create a logger for an instance that implements :class:`.Identified`.""" |
| 227 | |
| 228 | if instance.logging_name: |
| 229 | name = "%s.%s" % ( |
| 230 | _qual_logger_name_for_cls(instance.__class__), |
| 231 | instance.logging_name, |
| 232 | ) |
| 233 | else: |
| 234 | name = _qual_logger_name_for_cls(instance.__class__) |
| 235 | |
| 236 | instance._echo = echoflag # type: ignore |
| 237 | |
| 238 | logger: Union[logging.Logger, InstanceLogger] |
| 239 | |
| 240 | if echoflag in (False, None): |
| 241 | # if no echo setting or False, return a Logger directly, |
| 242 | # avoiding overhead of filtering |
| 243 | logger = logging.getLogger(name) |
| 244 | else: |
| 245 | # if a specified echo flag, return an EchoLogger, |
| 246 | # which checks the flag, overrides normal log |
| 247 | # levels by calling logger._log() |
| 248 | logger = InstanceLogger(echoflag, name) |
| 249 | |
| 250 | instance.logger = logger # type: ignore |
| 251 | |
| 252 | |
| 253 | class echo_property: |
no test coverage detected