Bind attributes to the ``extra`` dict of each logged message record. This is used to add custom context to each logging call. Parameters ---------- **kwargs Mapping between keys and values that will be added to the ``extra`` dict. Returns
(__self, **kwargs)
| 1403 | return Logger(self._core, exception, depth, record, lazy, colors, raw, capture, *args) |
| 1404 | |
| 1405 | def bind(__self, **kwargs): # noqa: N805 |
| 1406 | """Bind attributes to the ``extra`` dict of each logged message record. |
| 1407 | |
| 1408 | This is used to add custom context to each logging call. |
| 1409 | |
| 1410 | Parameters |
| 1411 | ---------- |
| 1412 | **kwargs |
| 1413 | Mapping between keys and values that will be added to the ``extra`` dict. |
| 1414 | |
| 1415 | Returns |
| 1416 | ------- |
| 1417 | :class:`~Logger` |
| 1418 | A logger wrapping the core logger, but which sends record with the customized ``extra`` |
| 1419 | dict. |
| 1420 | |
| 1421 | Examples |
| 1422 | -------- |
| 1423 | >>> logger.add(sys.stderr, format="{extra[ip]} - {message}") |
| 1424 | >>> class Server: |
| 1425 | ... def __init__(self, ip): |
| 1426 | ... self.ip = ip |
| 1427 | ... self.logger = logger.bind(ip=ip) |
| 1428 | ... def call(self, message): |
| 1429 | ... self.logger.info(message) |
| 1430 | ... |
| 1431 | >>> instance_1 = Server("192.168.0.200") |
| 1432 | >>> instance_2 = Server("127.0.0.1") |
| 1433 | >>> instance_1.call("First instance") |
| 1434 | 192.168.0.200 - First instance |
| 1435 | >>> instance_2.call("Second instance") |
| 1436 | 127.0.0.1 - Second instance |
| 1437 | """ |
| 1438 | *options, extra = __self._options |
| 1439 | return Logger(__self._core, *options, {**extra, **kwargs}) |
| 1440 | |
| 1441 | @contextlib.contextmanager |
| 1442 | def contextualize(__self, **kwargs): # noqa: N805 |