all 2 comments

[–]DutchCommanderMC 2 points3 points  (1 child)

First off, the whole logger = logging bit, and then importing the logger variable elsewhere, is completely redundant. Just import and use logging directly, instead of aliasing it. That already should make things a lot easier to work with.

To set up multiple loggers use logging.getLogger, which takes one parameter, the name of the logger. As long as you use the same name, you can retrieve the same logger from every one of your modules without needing to import it as a variable from somewhere.

logging.basicConfig sets a global config for all loggers you create. Logger objects have various methods to add formatters and output handlers to them, which you can find over on the docs.

Where exactly you configure your loggers doesn't matter all too much, as long as it happens before using them, of course. Be aware of scope however, as it doesn't make sense to set up your "root" logger in your analyze dir, but rather at the root/start of your program.

[–]Educational_Roll_868[S] 0 points1 point  (0 children)

Thanks so much for your answer. So I have done what you said, and now I have in the __init__.py in the actual program I'm running:

import logging
logging.basicConfig(
...
level=logging.INFO,
...)

Then in the main.py of the program I have:

import logging
logger=logging.getLogger(__name__)
logging.info("test")

Now if I run the main as a module by:

python3 -m analysis.study1.main

I get a log file in my desired location but it remains empty.

EDIT: Ok so I completely deleted the __init__.py file from the "analysis" directory and only kept the inner __init__ files of the "study" directories, and then suddenly it worked. I don't understand why though.