0

I am logging messages from various modules and want to change the log level dynamically. The code snippet is

import logging
logging.basicConfig(filename='my_log.log', filemode='w', encoding='utf-8', level=logging.WARNING, force=True)

# Code here that works so only worry about WARNINGs
# ....

# Code here has an issue so increase log level to DEBUG

logging.setLevel(logging.DEBUG)

This gives the error AttributeError: module 'logging' has no attribute 'setLevel'. I tried using the logger generated by basicConfig, e.g.

L = logging.basicConfig(filename='my_log.log', filemode='w', encoding='utf-8', level=logging.INFO, force=True)
L.setLevel(logging.DEBUG)

but got the error AttributeError: 'NoneType' object has no attribute 'logging'. How can I do this?

4
  • Does this help? stackoverflow.com/questions/19617355/… Commented Feb 13, 2024 at 10:29
  • Do you mean that you get AttributeError: 'NoneType' object has no attribute 'setLevel' in the last example? Commented Feb 13, 2024 at 10:35
  • Does this answer your question? Dynamically changing log level without restarting the application Commented Feb 13, 2024 at 10:39
  • Hi both - thanks for the link to the solution. I changed my code to logging.getLogger().setLevel(logging.DEBUG) and it works as expected Commented Feb 13, 2024 at 12:19

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.