CKTIS / Python
logging.error
Logs an error-level record without automatically attaching current exception details.
Version: Python 3.14; repository image, production runtime not attested
Syntax
logging.error(msg, *args, **kwargs)Logs an error-level record without automatically attaching current exception details.
An error-level message is an observation, not a raised exception. Parameter interpolation can be deferred until formatting.
ERROR has numeric level 40. The module-level call uses the root logger. Even inside an except block the default record contains no traceback. Set exc_info=True there to include the active exception. The example substitutes row number 7 in a percent-format message. exc_info can also be a retained exception instance or a (type, exception, traceback) tuple, even outside its except block.
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()
Examples
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())
Result
ERROR invalid row 7
ERROR has numeric level 40. The module-level call uses the root logger. Even inside an except block the default record contains no traceback. Set exc_info=True there to include the active exception. The example substitutes row number 7 in a percent-format message. exc_info can also be a retained exception instance or a (type, exception, traceback) tuple, even outside its except block.