CKTIS / Python
logging.error
error invia al logger radice un evento di livello ERROR (40).
Versione: Python 3.14; repository image, production runtime not attested
Sintassi
logging.error(msg, *args, **kwargs)error invia al logger radice un evento di livello ERROR (40).
exc_info accetta anche un’istanza di eccezione conservata o la terna (tipo, eccezione, traceback), anche fuori da except. L’argomento separato 7 produce il messaggio ERROR invalid row 7. La registrazione non solleva l’eccezione segnalata e normalmente restituisce None.
Dentro except, exc_info=True aggiunge il traceback dell’eccezione attiva.
import logging,io
o=io.StringIO()
logging.basicConfig(stream=o,format='%(levelno)d %(message)s',force=True)
try: raise ValueError('bad')
except ValueError:
assert logging.error('invalid row %d',7) is None
assert o.getvalue()=='40 invalid row 7\n'
logging.error('details',exc_info=True)
assert 'ValueError: bad' in o.getvalue()
import sys
try: raise LookupError('remember')
except LookupError as ex: saved=ex;triple=sys.exc_info()
for detail in (saved,triple):
o.seek(0);o.truncate()
logging.error('later',exc_info=detail)
assert 'LookupError: remember' in o.getvalue()
Esempi
import logging
from io import StringIO
out = StringIO()
logging.basicConfig(stream=out, format="%(levelname)s %(message)s", force=True)
logging.error("invalid row %d", 7)
print(out.getvalue().strip())
Risultato
ERROR invalid row 7
Dentro except, exc_info=True aggiunge il traceback dell’eccezione attiva.