MCPcopy
hub / github.com/Delgan/loguru / patch

Method patch

loguru/_logger.py:1486–1533  ·  view source on GitHub ↗

Attach a function to modify the record dict created by each logging call. The ``patcher`` may be used to update the record on-the-fly before it's propagated to the handlers. This allows the "extra" dict to be populated with dynamic values and also permits advanced modificati

(self, patcher)

Source from the content-addressed store, hash-verified

1484 context.reset(token)
1485
1486 def patch(self, patcher):
1487 """Attach a function to modify the record dict created by each logging call.
1488
1489 The ``patcher`` may be used to update the record on-the-fly before it's propagated to the
1490 handlers. This allows the "extra" dict to be populated with dynamic values and also permits
1491 advanced modifications of the record emitted while logging a message. The function is called
1492 once before sending the log message to the different handlers.
1493
1494 It is recommended to apply modification on the ``record["extra"]`` dict rather than on the
1495 ``record`` dict itself, as some values are used internally by `Loguru`, and modify them may
1496 produce unexpected results.
1497
1498 The logger can be patched multiple times. In this case, the functions are called in the
1499 same order as they are added.
1500
1501 Parameters
1502 ----------
1503 patcher: |callable|_
1504 The function to which the record dict will be passed as the sole argument. This function
1505 is in charge of updating the record in-place, the function does not need to return any
1506 value, the modified record object will be re-used.
1507
1508 Returns
1509 -------
1510 :class:`~Logger`
1511 A logger wrapping the core logger, but which records are passed through the ``patcher``
1512 function before being sent to the added handlers.
1513
1514 Examples
1515 --------
1516 >>> logger.add(sys.stderr, format="{extra[utc]} {message}")
1517 >>> logger = logger.patch(lambda record: record["extra"].update(utc=datetime.utcnow())
1518 >>> logger.info("That's way, you can log messages with time displayed in UTC")
1519
1520 >>> def wrapper(func):
1521 ... @functools.wraps(func)
1522 ... def wrapped(*args, **kwargs):
1523 ... logger.patch(lambda r: r.update(function=func.__name__)).info("Wrapped!")
1524 ... return func(*args, **kwargs)
1525 ... return wrapped
1526
1527 >>> def recv_record_from_network(pipe):
1528 ... record = pickle.loads(pipe.read())
1529 ... level, message = record["level"], record["message"]
1530 ... logger.patch(lambda r: r.update(record)).log(level, message)
1531 """
1532 *options, patchers, extra = self._options
1533 return Logger(self._core, *options, [*patchers, patcher], extra)
1534
1535 def level(self, name, no=None, color=None, icon=None):
1536 r"""Add, update or retrieve a logging level.

Calls 1

LoggerClass · 0.85