Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python logging set level in basicConfig

Tags:

python

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?

like image 839
thinker3 Avatar asked Jul 25 '13 03:07

thinker3


People also ask

How do I change the log level in Python?

Python set logging levelThe logging level is set with setLevel . It sets the threshold for this logger to lvl . Logging messages which are less severe than lvl will be ignored. In the example, we change the logging level to DEBUG .

How do you change the logger level?

To change log levels as a root user, perform the following: To enable debug logging, run the following command: /subsystem=logging/root-logger=ROOT:change-root-log-level(level=DEBUG) To disable debug logging, run the following command: /subsystem=logging/root-logger=ROOT:change-root-log-level(level=INFO)

How do I create a multiple logging level in Python?

You can set a different logging level for each logging handler but it seems you will have to set the logger's level to the "lowest". In the example below I set the logger to DEBUG, the stream handler to INFO and the TimedRotatingFileHandler to DEBUG. So the file has DEBUG entries and the stream outputs only INFO.


1 Answers

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')
    ....
like image 198
falsetru Avatar answered Oct 21 '22 12:10

falsetru