The code below will log from main and from RequestReport class just fine as is. It does not error at all and both main and RequestReport log to my file. However I want to put the class RequestReport in another file. If I move RequestReport in to another file called submod.py and uncomment the line from submod.py import RequestReport I get back an error that the logger is not defined logger.info('INFO LEVEL - REQUEST THREAD') NameError: name 'logger' is not defined.
My code in the RequestReport class is much more involved and for everthing but the logger it doesnt matter that the class lives in another file. Why does the logger not find importing the class as good as having it in the same file?
Example Works - https://repl.it/@RichZellmer/logclass
Example Fails - https://repl.it/@RichZellmer/logclassfail
from threading import *
import logging
from logging.handlers import RotatingFileHandler
#from submod import RequestReport
class RequestReport(Thread):
def __init__(self):
Thread.__init__(self)
self.reports_to_call = {}
def run(self):
logger.info('INFO LEVEL - REQUEST THREAD')
print("Requst thread Started")
if __name__ == '__main__':
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
handler = RotatingFileHandler('scratch.log', maxBytes=10000000, backupCount=10)
logger.addHandler(handler)
t1 = RequestReport()
t1.start()
logger.info('Main')