CKTIS / Python
logging.exception
exception zapiše dogodek ERROR s privzetim exc_info=True.
Različica: Python 3.14; repository image, production runtime not attested
Sintaksa
logging.exception(msg, *args, exc_info=True, **kwargs)exception zapiše dogodek ERROR s privzetim exc_info=True.
Znotraj except doda sled trenutne izjeme, ne da bi jo znova sprožil. exc_info=False ta dodatek izpusti.
Pri neuspelem int('x') primer najde parse in ValueError ter izpiše True True.
import logging,io
o=io.StringIO()
logging.basicConfig(stream=o,format='%(levelname)s %(message)s',force=True)
try: int('x')
except ValueError:
logging.exception('parse')
assert 'ERROR parse' in o.getvalue() and 'Traceback' in o.getvalue() and 'ValueError' in o.getvalue()
o.seek(0);o.truncate()
logging.exception('short',exc_info=False)
assert o.getvalue()=='ERROR short\n'
Primeri
import logging
from io import StringIO
out = StringIO()
logging.basicConfig(stream=out, force=True)
try:
int("x")
except ValueError:
logging.exception("parse")
print("ValueError" in out.getvalue(), "parse" in out.getvalue())
Rezultat
True True
Pri neuspelem int('x') primer najde parse in ValueError ter izpiše True True.