Outils pour utilisateurs

Outils du site


dev:python:logging

Différences

Ci-dessous, les différences entre deux révisions de la page.

Lien vers cette vue comparative

Prochaine révision
Révision précédente
dev:python:logging [2014/10/31 16:16] – créée rootdev:python:logging [2024/10/15 14:03] (Version actuelle) yoann
Ligne 72: Ligne 72:
    # générer le  message de log    # générer le  message de log
    log.debug('execution de la fonction ma_fonction')    log.debug('execution de la fonction ma_fonction')
 +</code>
 +
 +===== Exemple =====
 +
 +<code python>
 +import logging
 +
 +logger = logging.getLogger('simple_example')
 +logger.setLevel(logging.DEBUG)
 +# create file handler that logs debug and higher level messages
 +fh = logging.FileHandler('spam.log')
 +fh.setLevel(logging.DEBUG)
 +# create console handler with a higher log level
 +ch = logging.StreamHandler()
 +ch.setLevel(logging.ERROR)
 +# create formatter and add it to the handlers
 +formatter = logging.Formatter(
 +    '%(asctime)s - %(name)s - %(levelname)s - %(message)s')
 +ch.setFormatter(formatter)
 +fh.setFormatter(formatter)
 +# add the handlers to logger
 +logger.addHandler(ch)
 +logger.addHandler(fh)
 +
 +# 'application' code
 +logger.debug('debug message')
 +logger.info('info message')
 +logger.warn('warn message')
 +logger.error('error message')
 +logger.critical('critical message')
 </code> </code>
  
Ligne 77: Ligne 107:
  
   * http://pingbacks.wordpress.com/2010/12/21/python-logging-tutorial/   * http://pingbacks.wordpress.com/2010/12/21/python-logging-tutorial/
 +  * https://stackoverflow.com/questions/11581794/how-do-i-change-the-format-of-a-python-log-message-on-a-per-logger-basis
 +  * https://docs.python.org/3/library/logging.html
dev/python/logging.1414772185.txt.gz · Dernière modification : 2021/02/01 21:51 (modification externe)