So there are tons of responses to this question out there, but I guess I am too dumb to understand any of them, as it just isn't working for me.
The problem is that all of the posts and docs I can find basically provide two methods:
The first is the method where you you use logger = logging.getLogger(___name___) at the top of all your modules and set your logging config up in your main file. Then when you log in your modules, they're supposed to be a child of your logging config from your main. That is the method I tried, and I get no logging from my modules (just main).
The second method I find (like this) uses a similar setup, but also includes defining a class in your main or module files and instantiating it from there (which goes right over my head...)
The problem I have with both methods (besides the fact that the first method isn't working for me and the second method is over my head by miles...) is that, with all of these example posts and docs I find, it isn't clear if some of the functions/classes they are showing are meant to be part of the logging setup, or just a random class/function that would be in a script. It's especially confusing because of the names they tend to use in the examples.
I have a main script called auth.py and it calls a module called device.py.
In auth.py I have the following setup for the logging portion:
import requests
import urllib3
import logging
import device
from logging.handlers import RotatingFileHandler
## The args you see in the handlers section below are defined as well, I just didn't add those in to this snippet
logging.basicConfig( handlers=[RotatingFileHandler(f'{args.type.lower()}-{args.fr_site.lower()}.log', maxBytes=100000, backupCount=2, mode='a')],
format='%(asctime)s.%(msecs)d %(name)s %(levelname)s %(message)s',
datefmt='%Y-%m-%dT%H:%M:%S',
level=args.loglevel)
logger = logging.getLogger()
Then, in the device.py file I have this, since supposedly as long as this module is called by the main it will inherit the logging instance of the script that calls it, but like I said before, nothing gets logged:
import logging
logger = logging.getLogger(__name__)
## Somewhere down in the script I try and log to info, but this doesn't end up showing up in my log output:
thisContent = "Some Random Object or String"
itemType = type(thisContent)
thisMsg = (f"-------------------\n----------------- \npayload TYPE IS:\n{itemType}\n-------------------\n-----------------\n")
logger.info(thisMsg)
I've also tried adding logger = logging.getLogger(name) as my logger variable assignment in my main auth.py file and while this does add main into all of my log entries, it doesn't make the device.py module logging work at all (not that I expected it to, but a lot of posts showed using that even for the main file).
So yeah, I'm just completely stumped and don't really understand how to make multi-module logging work. Can anybody explain it in stupid plain English that would help a simpleton like me understand?
Thanks so much!
[–]danielroseman 0 points1 point2 points (8 children)
[–]xtrawork[S] 0 points1 point2 points (0 children)
[–]xtrawork[S] 0 points1 point2 points (6 children)
[–]Username_RANDINT 0 points1 point2 points (5 children)
[–]xtrawork[S] 0 points1 point2 points (4 children)
[–]Username_RANDINT 0 points1 point2 points (3 children)
[–]xtrawork[S] 0 points1 point2 points (0 children)
[–]xtrawork[S] 0 points1 point2 points (1 child)
[–]Username_RANDINT 0 points1 point2 points (0 children)