Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Python

logging.warning

Emits a warning-level record for an unexpected situation that still permits progress.

Version: Python 3.14; repository image, production runtime not attested

Syntax

logging.warning(msg, *args, **kwargs)

Emits a warning-level record for an unexpected situation that still permits progress.

A root threshold of ERROR discards warnings. A later higher-severity record can still reach the same handler.

WARNING is level 30. It normally reaches an unconfigured root logger because the module shortcut installs a default handler when necessary. Raising the root threshold to ERROR suppresses logging.warning but leaves logging.error eligible. The shortcut first checks the root threshold. At ERROR it returns before creating a WARNING LogRecord. The warnings module does not filter logging messages.

import logging,io,warnings
out=io.StringIO()
logging.basicConfig(stream=out,level=logging.WARNING,format='%(levelno)d %(message)s',force=True)
with warnings.catch_warnings(record=True) as caught:
    warnings.simplefilter('always')
    logging.warning('slow')
    assert not caught
assert out.getvalue()=='30 slow\n'
logging.getLogger().setLevel(logging.ERROR)
factory=logging.getLogRecordFactory()
created=[]
def record(*args,**kwargs):
    created.append(1)
    return factory(*args,**kwargs)
logging.setLogRecordFactory(record)
try: logging.warning('hidden')
finally: logging.setLogRecordFactory(factory)
assert not created
assert out.getvalue()=='30 slow\n'

Examples

import logging
from io import StringIO

out = StringIO()
logging.basicConfig(stream=out, level=logging.ERROR, format="%(levelname)s", force=True)
logging.warning("minor")
logging.error("major")
print(out.getvalue().strip())

Result

ERROR

WARNING is level 30. It normally reaches an unconfigured root logger because the module shortcut installs a default handler when necessary. Raising the root threshold to ERROR suppresses logging.warning but leaves logging.error eligible. The shortcut first checks the root threshold. At ERROR it returns before creating a WARNING LogRecord. The warnings module does not filter logging messages.

Sources