summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorMarkus Gerstel <markus.gerstel@diamond.ac.uk>2022-01-26 04:56:40 -0500
committerMike Bayer <mike_mp@zzzcomputing.com>2022-01-27 15:54:57 -0500
commitf24a34140f6007cada900a8ae5ed03fe40ce2631 (patch)
tree3e9aa699b05e8bcb875580a50728ce162dd71bcf /lib
parent46321a844f4143eed2dc03eaefc53d51b5484bc6 (diff)
downloadsqlalchemy-f24a34140f6007cada900a8ae5ed03fe40ce2631.tar.gz
Fix up Python logging metadata
Adjusted the logging for key SQLAlchemy components including :class:`_engine.Engine`, :class:`_engine.Connection` to establish an appropriate stack level parameter, so that the Python logging tokens ``funcName`` and ``lineno`` when used in custom logging formatters will report the correct information, which can be useful when filtering log output; supported on Python 3.8 and above. Pull request courtesy Markus Gerstel. Fixes: #7612 Closes: #7615 Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/7615 Pull-request-sha: cf9567beb06680df320cb12dde1f15baa68e1eb5 Change-Id: Iff23c92ef3453ac93cbd0d190e7efbf8ea4457a2
Diffstat (limited to 'lib')
-rw-r--r--lib/sqlalchemy/engine/base.py6
-rw-r--r--lib/sqlalchemy/log.py4
2 files changed, 10 insertions, 0 deletions
diff --git a/lib/sqlalchemy/engine/base.py b/lib/sqlalchemy/engine/base.py
index f7d02e3b0..b67a212c8 100644
--- a/lib/sqlalchemy/engine/base.py
+++ b/lib/sqlalchemy/engine/base.py
@@ -136,6 +136,9 @@ class Connection(ConnectionEventsTarget, inspection.Inspectable["Inspector"]):
if fmt:
message = fmt(message)
+ if util.py38:
+ kw["stacklevel"] = 2
+
self.engine.logger.info(message, *arg, **kw)
def _log_debug(self, message, *arg, **kw):
@@ -144,6 +147,9 @@ class Connection(ConnectionEventsTarget, inspection.Inspectable["Inspector"]):
if fmt:
message = fmt(message)
+ if util.py38:
+ kw["stacklevel"] = 2
+
self.engine.logger.debug(message, *arg, **kw)
@property
diff --git a/lib/sqlalchemy/log.py b/lib/sqlalchemy/log.py
index e9ab8f423..885163ecb 100644
--- a/lib/sqlalchemy/log.py
+++ b/lib/sqlalchemy/log.py
@@ -27,6 +27,7 @@ from typing import Type
from typing import TypeVar
from typing import Union
+from .util import py38
from .util.typing import Literal
_IT = TypeVar("_IT", bound="Identified")
@@ -181,6 +182,9 @@ class InstanceLogger:
selected_level = self.logger.getEffectiveLevel()
if level >= selected_level:
+ if py38:
+ kwargs["stacklevel"] = kwargs.get("stacklevel", 1) + 1
+
self.logger._log(level, msg, args, **kwargs)
def isEnabledFor(self, level: int) -> bool: