WissensdatenbankPrüfungszentrum
DEDeutsch
Sprache wählen13

CKTIS / Python

logging.warning

warning protokolliert am Root-Logger eine Warnung der Stufe WARNING (30).

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

Syntax

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

warning protokolliert am Root-Logger eine Warnung der Stufe WARNING (30).

Ohne Handler richtet die Funktion die Grundkonfiguration ein. Es ist ein Logeintrag, dessen Sichtbarkeit nicht von Filtern des Moduls warnings abhängt.

Im Beispiel unterdrückt die Schwelle ERROR die Meldung minor, lässt major durch und gibt ERROR aus.

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'

Beispiele

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())

Ergebnis

ERROR

Im Beispiel unterdrückt die Schwelle ERROR die Meldung minor, lässt major durch und gibt ERROR aus.

Quellen