CKTIS / Python
logging.exception
exception registra un evento ERROR con exc_info=True per impostazione predefinita.
Versione: Python 3.14; repository image, production runtime not attested
Sintassi
logging.exception(msg, *args, exc_info=True, **kwargs)exception registra un evento ERROR con exc_info=True per impostazione predefinita.
Dentro except aggiunge il traceback dell’eccezione attiva senza sollevarla nuovamente. exc_info=False omette tale dettaglio.
Quando int('x') fallisce, l’esempio trova sia parse sia ValueError e stampa 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'
Esempi
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())
Risultato
True True
Quando int('x') fallisce, l’esempio trova sia parse sia ValueError e stampa True True.