0

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')

2 Answers 2

1

That's because you have not defined the logger object in submod.py, When you import a module, python runs that module to get all of its variables into the current namespace. If you want to move the class into another file, you should also initialize your logger configuration in that file and then simply import the logger into main.py

Here's what I mean:

main.py:

from threading import * # DON'T USE WILDCARD IMPORTS, IMPORT ONLY WHAT YOU NEED!
from rr import RequestReport, logger # Notice we import the class and the logger from rr

if __name__ == '__main__':
    t1 = RequestReport()
    t1.start()

    logger.info('Main')

rr.py:

from threading import * # DON'T USE WILDCARD IMPORTS, IMPORT ONLY WHAT YOU NEED!
import logging
from logging.handlers import RotatingFileHandler

logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
handler = RotatingFileHandler(
    'scratch2.log', maxBytes=10000000, backupCount=10)
logger.addHandler(handler)

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")

Hope this helps!

Sign up to request clarification or add additional context in comments.

3 Comments

this does make sense. However I am still wondering is there a way to have the logger defined in main and the classes that are called from main inherirt the logger even if they are in a differnt file? I am thinking about this from the point of what happens if main.py calls clases in rr.py and some new file zz.py.
I don't see why you need to define the logger in main, even if you create a new file zz.py you can simply import your logger defined in rr, you can import that anywhere you want. You can even create a new file called logger_configuration, define the logger there and import from there wherever you need it.
you are correct. Even though that is not how you coded it I was thinking the logger was imported from the class in the rr.py file instead of you the fact that you are importing both the class and the logger from the file.
0

Umm... you need tabs for the code in the class. Ask me if it still doesn't work.

1 Comment

i fixed the missing tabs in the stack overflow editior. that was just me not lining everthing up when bringing it in. If you look at the live code links, the indents are okay

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.