Skip to content

Commit

Permalink
Merge branch 'Delgan:master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
tim-x-y-z authored Jul 12, 2024
2 parents c9e240b + 1922f71 commit d5d4282
Show file tree
Hide file tree
Showing 17 changed files with 295 additions and 219 deletions.
26 changes: 26 additions & 0 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: release

on:
push:
tags:
- '*'

jobs:
release:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4

- uses: actions/setup-python@v5
with:
python-version: 3.x

- run: pipx run build

- run: twine check --strict dist/*

- run: pipx run twine upload dist/* --disable-progress-bar
env:
TWINE_USERNAME: __token__
TWINE_PASSWORD: ${{ secrets.PYPI_API_TOKEN }}
16 changes: 11 additions & 5 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ on:
jobs:
tests:
if: github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name != github.repository
continue-on-error: ${{ endsWith(matrix.python-version, '-dev') }}
strategy:
fail-fast: false
matrix:
Expand All @@ -20,17 +21,19 @@ jobs:
- '3.9'
- '3.10'
- '3.11'
- 3.12-dev
- '3.12'
# Temporary disabled because of https://github.com/crate-py/rpds/issues/72
# - 3.13-dev
- pypy-3.9
include:
- os: ubuntu-20.04
python-version: '3.5'
- os: ubuntu-20.04
python-version: '3.6'
- os: windows-2022
python-version: '3.11'
- os: macos-11
python-version: '3.11'
python-version: '3.12'
- os: macos-13
python-version: '3.12'
runs-on: ${{ matrix.os }}
steps:
- name: Checkout repository
Expand All @@ -39,6 +42,9 @@ jobs:
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
env:
# Workaround for https://github.com/actions/setup-python/issues/866
PIP_TRUSTED_HOST: pypi.python.org pypi.org files.pythonhosted.org
- name: Install dependencies
run: |
python -m pip install --upgrade pip
Expand All @@ -47,7 +53,7 @@ jobs:
run: |
tox -e tests
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v3
uses: codecov/codecov-action@v4
with:
token: ${{ secrets.CODECOV_TOKEN }}
flags: ${{ matrix.os }}_${{ matrix.python-version }}
Expand Down
12 changes: 7 additions & 5 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
rev: v4.5.0
hooks:
- id: end-of-file-fixer
- id: trailing-whitespace
- id: check-added-large-files
- id: mixed-line-ending
args: [--fix=lf]
- repo: https://github.com/crate-ci/typos
rev: v1.16.19
rev: v1.18.0
hooks:
- id: typos
- repo: https://github.com/macisamuele/language-formatters-pre-commit-hooks
rev: v2.10.0
rev: v2.12.0
hooks:
- id: pretty-format-ini
args: [--autofix]
Expand All @@ -19,11 +21,11 @@ repos:
- id: pretty-format-toml
args: [--autofix]
- repo: https://github.com/ambv/black
rev: 23.7.0
rev: 24.1.1
hooks:
- id: black
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.0.286
rev: v0.2.0
hooks:
- id: ruff
args: [--fix, --exit-non-zero-on-fix]
2 changes: 0 additions & 2 deletions MANIFEST.in

This file was deleted.

1 change: 1 addition & 0 deletions docs/_extensions/autodoc_stub_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
Warning: for some reason, the docs NEEDS to be re-generated for changes in the stub file to be taken
into account: ``make clean && make html``.
"""

import os
import sys
import types
Expand Down
140 changes: 70 additions & 70 deletions docs/_static/img/sadhu.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
36 changes: 0 additions & 36 deletions docs/make.bat

This file was deleted.

26 changes: 26 additions & 0 deletions docs/resources/migration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,8 @@ The |captureWarnings| function which redirects alerts from the |warnings| module
warnings.showwarning = showwarning


.. _migration-assert-logs:

Replacing ``assertLogs()`` method from ``unittest`` library
-----------------------------------------------------------

Expand Down Expand Up @@ -316,6 +318,12 @@ It provides the list of :ref:`logged messages <message>` for each of which you c
self.assertIn("Invalid value", message)
self.assertEqual(message.record["level"].name, "ERROR")

.. seealso::

See :ref:`testing logging <recipes-testing>` for more information.


.. _migration-caplog:

Replacing ``caplog`` fixture from ``pytest`` library
----------------------------------------------------
Expand Down Expand Up @@ -367,3 +375,21 @@ Note that if you want Loguru logs to be propagated to Pytest terminal reporter,
handler_id = logger.add(logging_plugin.report_handler, format="{message}")
yield
logger.remove(handler_id)

Finally, when dealing with the ``--log-cli-level`` command-line flag, remember that this option controls the standard ``logging`` logs, not ``loguru`` ones. For this reason, you must first install a ``PropagateHandler`` for compatibility::

@pytest.fixture(autouse=True)
def propagate_logs():

class PropagateHandler(logging.Handler):
def emit(self, record):
if logging.getLogger(record.name).isEnabledFor(record.levelno):
logging.getLogger(record.name).handle(record)

logger.remove()
logger.add(PropagateHandler(), format="{message}")
yield

.. seealso::

See :ref:`testing logging <recipes-testing>` for more information.
41 changes: 40 additions & 1 deletion docs/resources/recipes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ Code snippets and recipes for ``loguru``
.. |if-name-equals-main| replace:: ``if __name__ == "__main__":``
.. _if-name-equals-main: https://docs.python.org/3/library/__main__.html#idiomatic-usage

.. |logot| replace:: ``logot``
.. _logot: https://logot.readthedocs.io/

.. |pytest| replace:: ``pytest``
.. _pytest: https://docs.pytest.org/en/latest/

.. |stackprinter| replace:: ``stackprinter``
.. _stackprinter: https://github.com/cknd/stackprinter

Expand Down Expand Up @@ -815,7 +821,7 @@ You may also capture warnings emitted by your application by replacing |warnings
showwarning_ = warnings.showwarning

def showwarning(message, *args, **kwargs):
logger.warning(message)
logger.opt(depth=2).warning(message)
showwarning_(message, *args, **kwargs)

warnings.showwarning = showwarning
Expand Down Expand Up @@ -1043,3 +1049,36 @@ Another thing to keep in mind when dealing with multiprocessing is the fact that
worker = workers_a.Worker()
with context.Pool(4, initializer=worker.set_logger, initargs=(logger, )) as pool:
results = pool.map(worker.work, [1, 10, 100])


.. _recipes-testing:

Testing logging
---------------

Logging calls can be tested using |logot|_, a high-level log testing library with built-in support for Loguru::

from logot import Logot, logged

def test_something(logot: Logot) -> None:
do_something()
logot.assert_logged(logged.info("Something was done"))

Enable Loguru log capture in your |pytest|_ configuration:

.. code:: toml
[tool.pytest.ini_options]
logot_capturer = "logot.loguru.LoguruCapturer"
.. seealso::

See `using logot with Loguru <https://logot.readthedocs.io/latest/integrations/loguru.html>`_ for more information
about `configuring pytest <https://logot.readthedocs.io/latest/integrations/loguru.html#enabling-for-pytest>`_
and `configuring unittest <https://logot.readthedocs.io/latest/integrations/loguru.html#enabling-for-unittest>`_.

.. note::

When migrating an existing project from standard :mod:`logging`, it can be useful to migrate your existing test
cases too. See :ref:`migrating assertLogs() <migration-assert-logs>` and :ref:`migrating caplog <migration-caplog>`
for more information.
1 change: 1 addition & 0 deletions loguru/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
Just ``from loguru import logger``.
"""

import atexit as _atexit
import sys as _sys

Expand Down
12 changes: 11 additions & 1 deletion loguru/_better_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@ def _format_exception(
else:
yield from self._indent(introduction + "\n", group_nesting)

frames_lines = traceback.format_list(frames) + exception_only
frames_lines = self._format_list(frames) + exception_only
if self._colorize or self._backtrace or self._diagnose:
frames_lines = self._format_locations(frames_lines, has_introduction=has_introduction)

Expand Down Expand Up @@ -526,5 +526,15 @@ def _format_exception(
if not is_exception_group(exc) or group_nesting == 10:
yield from self._indent("-" * 35, group_nesting + 1, prefix="+-")

def _format_list(self, frames):
result = []
for filename, lineno, name, line in frames:
row = []
row.append(' File "{}", line {}, in {}\n'.format(filename, lineno, name))
if line:
row.append(" {}\n".format(line.strip()))
result.append("".join(row))
return result

def format_exception(self, type_, value, tb, *, from_decorator=False):
yield from self._format_exception(value, tb, is_first=True, from_decorator=from_decorator)
10 changes: 6 additions & 4 deletions loguru/_colorizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,10 +338,12 @@ def colorize(self, ansi_level):

def make_coloring_message(self, message, *, ansi_level, colored_message):
messages = [
message
if color_tokens is None
else AnsiParser.wrap(
colored_message.tokens, ansi_level=ansi_level, color_tokens=color_tokens
(
message
if color_tokens is None
else AnsiParser.wrap(
colored_message.tokens, ansi_level=ansi_level, color_tokens=color_tokens
)
)
for color_tokens in self._messages_color_tokens
]
Expand Down
1 change: 1 addition & 0 deletions loguru/_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
.. _formatting directives: https://docs.python.org/3/library/string.html#format-string-syntax
.. _reentrant: https://en.wikipedia.org/wiki/Reentrancy_(computing)
"""

import builtins
import contextlib
import functools
Expand Down
Loading

0 comments on commit d5d4282

Please sign in to comment.