Skip to content

Logging improvements #1004

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
logging: Move exc_info to common log.
The keyword parameters are populated to common log and exc_info should be
common to all methods anyway.

This change the default to be exc_info=False for all cases similar to the
standard python.

Signed-off-by: Alon Bar-Lev <[email protected]>
  • Loading branch information
alonbl committed Apr 30, 2025
commit 686f1ca90fc1115c1e7f1d47f397e4daa053b155
23 changes: 12 additions & 11 deletions python-stdlib/logging/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ def isEnabledFor(self, level):
def getEffectiveLevel(self):
return self.level or getLogger().level or _DEFAULT_LEVEL

def log(self, level, msg, *args, extra=None):
def log(self, level, msg, *args, exc_info=False, extra=None):
if self.isEnabledFor(level):
if args:
if isinstance(args[0], dict):
Expand All @@ -138,6 +138,16 @@ def log(self, level, msg, *args, extra=None):
for h in handlers:
h.emit(record)

tb = None
if isinstance(exc_info, BaseException):
tb = exc_info
elif hasattr(sys, "exc_info"):
tb = sys.exc_info()[1]
if tb:
buf = io.StringIO()
sys.print_exception(tb, buf)
self.log(ERROR, buf.getvalue())

def debug(self, msg, *args, **kwargs):
self.log(DEBUG, msg, *args, **kwargs)

Expand All @@ -153,17 +163,8 @@ def error(self, msg, *args, **kwargs):
def critical(self, msg, *args, **kwargs):
self.log(CRITICAL, msg, *args, **kwargs)

def exception(self, msg, *args, exc_info=True, **kwargs):
def exception(self, msg, *args, **kwargs):
self.log(ERROR, msg, *args, **kwargs)
tb = None
if isinstance(exc_info, BaseException):
tb = exc_info
elif hasattr(sys, "exc_info"):
tb = sys.exc_info()[1]
if tb:
buf = io.StringIO()
sys.print_exception(tb, buf)
self.log(ERROR, buf.getvalue())

def addHandler(self, handler):
self.handlers.append(handler)
Expand Down
Loading