Logging setLevel is being ignored

前端 未结 6 1180
我寻月下人不归
我寻月下人不归 2020-12-03 00:15

The below code is copied from the documentation. I am supposed to be able to see all the info logs. But I don\'t. I am only able to see the warn and above even though I\'ve

6条回答
  •  不思量自难忘°
    2020-12-03 00:59

    You have to set the basicConfig of the root logger to DEBUG, then you can set the level of your individual loggers to more restrictive levels.

    This is not what I expected. Here is what I had to do:

    #!/usr/bin/env python3
    
    import logging
    # by default this is WARNING.  Leaving it as WARNING here overrides 
    # whatever setLevel-ing you do later so it seems they are ignored.
    logging.basicConfig(level=logging.DEBUG)
    
    l = logging.getLogger(__name__)
    l.setLevel(level=logging.INFO)
    # if I hadn't called basicConfig with DEBUG level earlier, 
    # info messages would STILL not be shown despite calling 
    # setLevel above.  However now debug messages will not be shown 
    # for l because setLevel set it to INFO
    
    l.warning('A warning message will be displayed')
    l.info('A friendly info message will be displayed')
    l.debug('A friendly debug message will not be displayed')
    

提交回复
热议问题