8

python logging set level in basicConfig:

import logging

def show(level):
    logging.basicConfig(level=level)
    logging.info('info')
    logging.debug('debug')
    logging.warn('warn')
    logging.error('error')
    logging.fatal('fatal')
    logging.warning('warning')
    logging.critical('critical')
    logging.exception('exception')

show(logging.WARNING)
show(logging.DEBUG)

The two results are the same, how to get what I expects?

thinker3
  • 12,771
  • 5
  • 30
  • 36

1 Answers1

14

According to logging.basicConfig documentation, the second call to logging.basicConfig does not take effect.

This function does nothing if the root logger already has handlers configured for it.

def show(level):
    logger = logging.getLogger()
    logger.setLevel(level)
    logging.info('info')
    ....
falsetru
  • 357,413
  • 63
  • 732
  • 636