简体   繁体   中英

Python logging: how to disable warnings inside StreamHandler

I use 2 handlers and I don't want to write all the libraries warnings inside StremHandler like this

See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
  return super().rename(
/home/anaconda3/envs/test_pr/lib/python3.8/site-packages/pandas/core/frame.py:4441: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame

I tried to use logging.disable(logging.WARNING) but it doesn't work.

And my StreamHandler is

logger = logging.getLogger()
logger.setLevel(logging.INFO)
# create console handler
ch = logging.StreamHandler(sys.stdout)
ch.setLevel(logging.INFO)
logger.addHandler(ch)

Attach a filter to your handler which filters out things you don't want output:

def filter(record):
    """Filter out warnings - this is just an example"""
    return record.levelno != logging.WARNING

ch = logging.StreamHandler(sys.stdout)
ch.addFilter(filter)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM