Skip to content

Commit 32bad81

Browse files
authored
Allow installation of current 'structlog' major version 21.x (#75)
* Pinned pylint minor version to 2.6 to ensure test completion. * Allow installation of current structlog version 21.x. * Updated to allow testing of latest structlog. * Added checks to see if the 'format_exc_info' processor is required. * Selective pull for merge fix. * Allow current structlog version. * Formatted with black 21.11b1. * Pylint: disable import-outside-toplevel, import-error within import try/except clause. * Restored poetry.lock from branch main. * Modifed docstring to conform to pydocstyle. * Replaced try:import:except with importlib.util.find_spec. * Use 'version.parse' to determine structlog.__version__. * Fixed import order. * Fixed extraneous line after docstring. * Added missing line: 'processors=processors,'. * Changed the import from importlib.utils to work on Python 3.9.
1 parent 92ceca9 commit 32bad81

File tree

2 files changed

+40
-10
lines changed

2 files changed

+40
-10
lines changed

diffsync/logging.py

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@
1616
"""
1717

1818
import logging
19+
from importlib.util import find_spec
1920

2021
import structlog # type: ignore
22+
from packaging import version
2123

2224

2325
def enable_console_logging(verbosity=0):
@@ -35,18 +37,46 @@ def enable_console_logging(verbosity=0):
3537
else:
3638
logging.basicConfig(format="%(message)s", level=logging.DEBUG)
3739

40+
processors = [
41+
structlog.stdlib.filter_by_level, # <-- added
42+
structlog.stdlib.add_logger_name,
43+
structlog.stdlib.add_log_level,
44+
structlog.processors.TimeStamper(fmt="%Y-%m-%d %H:%M.%S"),
45+
structlog.processors.StackInfoRenderer(),
46+
]
47+
48+
if _structlog_exception_formatter_required():
49+
processors.append(structlog.processors.format_exc_info)
50+
51+
# ConsoleRenderer must be added after format_exc_info
52+
processors.append(structlog.dev.ConsoleRenderer())
53+
3854
structlog.configure(
39-
processors=[
40-
structlog.stdlib.filter_by_level, # <-- added
41-
structlog.stdlib.add_logger_name,
42-
structlog.stdlib.add_log_level,
43-
structlog.processors.TimeStamper(fmt="%Y-%m-%d %H:%M.%S"),
44-
structlog.processors.StackInfoRenderer(),
45-
structlog.processors.format_exc_info,
46-
structlog.dev.ConsoleRenderer(),
47-
],
55+
processors=processors,
4856
context_class=dict,
4957
logger_factory=structlog.stdlib.LoggerFactory(),
5058
wrapper_class=structlog.stdlib.BoundLogger,
5159
cache_logger_on_first_use=True,
5260
)
61+
62+
63+
def _structlog_exception_formatter_required():
64+
"""Determine if structlog exception formatter is needed.
65+
66+
Return True if structlog exception formatter should be loaded
67+
into structlog processors.
68+
69+
Structlog version 21.2.0 or higher will generate a warning
70+
if either rich or better_exceptions packages are available to import
71+
when the 'format_exc_info' processor is used.
72+
73+
This code snippet will determine if we need to add 'format_exc_info'
74+
to the processors.
75+
"""
76+
if version.parse(structlog.__version__) < version.Version("21.2.0"):
77+
return True
78+
79+
# Determine if module is available for import, without importing it.
80+
rich = find_spec("rich")
81+
better_exceptions = find_spec("better_exceptions")
82+
return not (rich or better_exceptions)

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ include = [
1818
[tool.poetry.dependencies]
1919
python = "^3.6.2"
2020
pydantic = "^1.7.4,!=1.8,!=1.8.1"
21-
structlog = "^20.1.0"
21+
structlog = ">= 20.1.0, < 22.0.0"
2222
colorama = {version = "^0.4.3", optional = true}
2323
# For Pydantic
2424
dataclasses = {version = "^0.7", python = "~3.6"}

0 commit comments

Comments
 (0)