Skip to content

Commit

Permalink
Allow "patch()" to be called multiple times on same logger (#462)
Browse files Browse the repository at this point in the history
  • Loading branch information
Delgan committed Apr 29, 2022
1 parent a1989d3 commit 9c9d137
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 11 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
`Unreleased`_
=============

- Make ``patch()`` calls cumulative instead of overriding the possibly existing patching function (`#462 <https://github.com/Delgan/loguru/issues/462>`_).


`0.6.0`_ (2022-01-29)
=====================

Expand Down
2 changes: 1 addition & 1 deletion loguru/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
colors=False,
raw=False,
capture=True,
patcher=None,
patchers=[],
extra={},
)

Expand Down
23 changes: 16 additions & 7 deletions loguru/_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,13 @@
from ._get_frame import get_frame
from ._handler import Handler
from ._locks_machinery import create_logger_lock
from ._recattrs import RecordException, RecordFile, RecordLevel, RecordProcess, RecordThread
from ._recattrs import (
RecordException,
RecordFile,
RecordLevel,
RecordProcess,
RecordThread,
)
from ._simple_sinks import AsyncSink, CallableSink, StandardSink, StreamSink

if sys.version_info >= (3, 6):
Expand Down Expand Up @@ -209,9 +215,9 @@ class Logger:
You should not instantiate a |Logger| by yourself, use ``from loguru import logger`` instead.
"""

def __init__(self, core, exception, depth, record, lazy, colors, raw, capture, patcher, extra):
def __init__(self, core, exception, depth, record, lazy, colors, raw, capture, patchers, extra):
self._core = core
self._options = (exception, depth, record, lazy, colors, raw, capture, patcher, extra)
self._options = (exception, depth, record, lazy, colors, raw, capture, patchers, extra)

def __repr__(self):
return "<loguru.logger handlers=%r>" % list(self._core.handlers.values())
Expand Down Expand Up @@ -1426,6 +1432,9 @@ def patch(self, patcher):
``record`` dict itself, as some values are used internally by `Loguru`, and modify them may
produce unexpected results.
The logger can be patched multiple times. In this case, the functions are called in the
same order as they are added.
Parameters
----------
patcher: |callable|_
Expand Down Expand Up @@ -1457,8 +1466,8 @@ def patch(self, patcher):
... level, message = record["level"], record["message"]
... logger.patch(lambda r: r.update(record)).log(level, message)
"""
*options, _, extra = self._options
return Logger(self._core, *options, patcher, extra)
*options, patchers, extra = self._options
return Logger(self._core, *options, patchers + [patcher], extra)

def level(self, name, no=None, color=None, icon=None):
"""Add, update or retrieve a logging level.
Expand Down Expand Up @@ -1853,7 +1862,7 @@ def _log(self, level_id, static_level_no, from_decorator, options, message, args
if not core.handlers:
return

(exception, depth, record, lazy, colors, raw, capture, patcher, extra) = options
(exception, depth, record, lazy, colors, raw, capture, patchers, extra) = options

frame = get_frame(depth + 2)

Expand Down Expand Up @@ -1961,7 +1970,7 @@ def _log(self, level_id, static_level_no, from_decorator, options, message, args
if core.patcher:
core.patcher(log_record)

if patcher:
for patcher in patchers:
patcher(log_record)

for handler in core.handlers.values():
Expand Down
5 changes: 2 additions & 3 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@
import traceback
import warnings

import pytest

import loguru
import pytest

if sys.version_info < (3, 5, 3):

Expand Down Expand Up @@ -83,7 +82,7 @@ def reset_logger():
def reset():
loguru.logger.remove()
loguru.logger.__init__(
loguru._logger.Core(), None, 0, False, False, False, False, True, None, {}
loguru._logger.Core(), None, 0, False, False, False, False, True, [], {}
)
loguru._logger.context.set({})

Expand Down
16 changes: 16 additions & 0 deletions tests/test_patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,19 @@ def test_override_configured(writer):
logger2.debug("!")

assert writer.read() == "456 678 !\n"


def test_multiple_patches(writer):
def patch_1(record):
record["extra"]["a"] = 5

def patch_2(record):
record["extra"]["a"] += 1

def patch_3(record):
record["extra"]["a"] *= 2

logger.add(writer, format="{extra[a]} {message}")
logger.patch(patch_1).patch(patch_2).patch(patch_3).info("Test")

assert writer.read() == "12 Test\n"

0 comments on commit 9c9d137

Please sign in to comment.