Skip to content

Commit

Permalink
Improve import time of logging
Browse files Browse the repository at this point in the history
  • Loading branch information
hugovk committed Sep 2, 2024
1 parent c3ed775 commit 50af0a0
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion Lib/logging/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
To use, simply 'import logging' and log away!
"""

import sys, os, time, io, re, traceback, warnings, weakref, collections.abc
import sys, os, time, io, re, warnings, weakref, collections.abc

from types import GenericAlias
from string import Template
Expand Down Expand Up @@ -661,6 +661,9 @@ def formatException(self, ei):
This default implementation just uses
traceback.print_exception()
"""
# Lazy import to improve module import time
import traceback

sio = io.StringIO()
tb = ei[2]
# See issues #9427, #1553375. Commented out for now.
Expand Down Expand Up @@ -1071,6 +1074,9 @@ def handleError(self, record):
if raiseExceptions and sys.stderr: # see issue 13807
exc = sys.exception()
try:
# Lazy import to improve module import time
import traceback

sys.stderr.write('--- Logging error ---\n')
traceback.print_exception(exc, limit=None, file=sys.stderr)
sys.stderr.write('Call stack:\n')
Expand Down Expand Up @@ -1615,6 +1621,9 @@ def findCaller(self, stack_info=False, stacklevel=1):
sinfo = None
if stack_info:
with io.StringIO() as sio:
# Lazy import to improve module import time
import traceback

sio.write("Stack (most recent call last):\n")
traceback.print_stack(f, file=sio)
sinfo = sio.getvalue()
Expand Down

0 comments on commit 50af0a0

Please sign in to comment.