Logging from files doesn't work

Cameron Simpson cs at cskk.id.au
Wed Oct 11 23:29:49 EDT 2017


On 11Oct2017 22:27, Andrew Z <formisc at gmail.com> wrote:
>aha. So the issue is that main.py's __name__ attribute == "__main__" and
>test.py is "test1.test".

Yeah. If you invoke a module as "python -m module_name" its __name__ field is 
"__main__". That makes the boilerplate work, but breaks your expectation that 
__name__ is usually module_name.

>if i manually assign names:
>main.py - >
>
>log = logging.getLogger("MAIN")
>
>test.py - >
>log = logging.getLogger("MAIN.test1.test")
>
>then logging is working perfectly well.
>
>This brings me to the question - what is wrong with me file
>naming/structure that confuses the logging module? I'm not sure i
>really want to have each little file in it's own directory too..

I confess to not using the logging module in the module-named-based fashion 
that seems to be the default recommendation. I usually prefer to set up the 
root logger to log somewhere suitable to the main program (typically just 
stderr by default), and set up special logging for other things as needed.

As what may be a poor example, I've got a mail filing program which monitors 
maildirs for arriving messages to refile. So I've got a class associated with 
each monitored maildir with these methods:

  @property
  def logdir(self):
    ''' The pathname of the directory in which log files are written.
    '''
    varlog = cs.env.LOGDIR(self.environ)
    return os.path.join(varlog, 'mailfiler')

This essentially computes "$HOME/var/log/mailfiler" as the place where all the 
logfiles are saved. And this:

  def folder_logfile(self, folder_path):
    ''' Return path to log file associated with the named folder.
        TODO: base on relative path from folder root, not just basename.
    '''
    return os.path.join(self.logdir, '%s.log' % (os.path.basename(folder_path)))

which computes "$HOME/var/log/mailfiler/spool.log" as the logfile when working 
on my maildir "$HOME/mail/spool".

And then off it goes with a FileHandler for that path for the filing actions 
for that maildir.

So you're not contrained to drop log files all through your source tree (ewww!)

My opinion is that you should decide where your logfiles _should_ live; it 
generally has nothing (or little) to do with the module name. I keep mine, 
generally, in various subdirectories of "$HOME/var/log".

Cheers,
Cameron Simpson <cs at cskk.id.au> (formerly cs at zip.com.au)



More information about the Python-list mailing list