Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
362 views
in Technique[技术] by (71.8m points)

logging - Suppress python logger for a specific requests call

I am using session.request call multiple time at multiple places. I want to block its log for a specific call. How can I achieve that. I figured that we can block it for all the requests call using following method

logging.basicConfig(filename=filename,
                            format='%(asctime)s %(funcName)s %(levelname)s %(message)s',
                            filemode='a')

logger = logging.getLogger("requests").setLevel(logging.WARNING) # To disable requests logging info

def fun1():
    session.request('POST', _endpoint, json=data)

def fun2():
    session.request('POST', _endpoint, json=data)

I want to disable logs for fun1 but keep it running for fun2. Is it possible to customize logs in such a way that I can disable them for a particular function of call?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The following code should work. You can also turn it into a decorator.

def fun1():
    logging.disable() # globally stops all logs with level critical or lower (so all logs)
    session.request('POST', _endpoint, json=data)
    logging.disable(logging.NOTSET) # remove the override 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...