Base de connaissancesCentre d’examen
FRFrançais
Choisir la langue13

CKTIS / Python

logging.warning

warning enregistre sur le journal racine un avertissement de niveau WARNING (30).

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

Syntaxe

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

warning enregistre sur le journal racine un avertissement de niveau WARNING (30).

En l’absence de gestionnaires la fonction installe la configuration de base. Les filtres du module warnings ne contrôlent pas cet enregistrement.

Dans l’exemple le seuil ERROR masque minor mais laisse passer major, ce qui affiche ERROR.

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'

Exemples

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

Résultat

ERROR

Dans l’exemple le seuil ERROR masque minor mais laisse passer major, ce qui affiche ERROR.

Sources