1

I've been at it and all over my code, and perhaps am blinkered by it.

Following setup in my __main__.py:

if args.debug: logpath = '{}/ganalytics_mf70.dbg'.format(backend.utilities.get_module_path()) lvl = logging.DEBUG else: logpath = '{}/ganalytics_mf70.log'.format(backend.utilities.get_module_path()) lvl = logging.INFO log = logging.getLogger('GAnalytics') log.setLevel(logging.DEBUG) formatter = logging.Formatter('%(asctime)s: %(name)-12s: %(levelname)-8s %(message)s') fh = logging.FileHandler(logpath) # file logging fh.setLevel(lvl) fh.setFormatter(formatter) formatter = logging.Formatter('%(name)-12s: %(levelname)-8s %(message)s') ch = logging.StreamHandler() # console logging ch.setFormatter(formatter) ch.setLevel(logging.INFO) log.addHandler(ch) log.addHandler(fh) 

All other .py files have

import logging log = logging.GetLogger(__name__) 

at the top.

package outline:

program/ - backend/ - foo.py - bar.py - spam.py - __init__.py - __main__.py 

This logs all log.debug and higher calls in my __main__.py - but none of the log calls in spam, bar, foo or __init__ py files.

Where's the glaringly obvious mistake I'm not seeing?

1 Answer 1

1

Unless the other modules live under a top-level package (read: subdirectory of something listed in sys.path) named GAnalytics, they won't go through the logger you have configured in __main__.py, and so their log events below WARNING will be dropped by default.

Most of the time, you want to make changes to the root logger, which is given by logging.getLogger() (with no arguments, or with argument None). This is most easily accomplished with logging.basicConfig(), but you can do it manually as well.

(Side note: You probably don't want to call a module __main__.py. import __main__ always means "import the main module" (that is, the module that Python was invoked with) and never "import a file named __main__.py," so if it ever happens that __main__.py is not actually the main module, you will be very confused by the resulting behavior. Files with that name also cannot use the standard if __name__ == '__main__': guard because that condition is always true.)

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

1 Comment

Oh wow. I didn't think you can't just toss any name into GetLogger(). #whenskimreadingshootsyouinthefoot.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.