-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve Logging #45
Comments
Hi Tom, can you please explain this. Usually I use module-level logger like it is described here: https://docs.python.org/3/howto/logging.html#advanced-logging-tutorial. What logger should I use instead? Should I use a single package logger? |
hi Jürgen, 👋 well, I guess you can initialize the logger instance in # log.py
# create logger
log = logging.getLogger("rng2doc")
log.setLevel(logging.DEBUG) # Not sure if this is needed
# create console handler and set level to debug
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG) # This should be set in cli.py
# create formatter
formatter = CustomConsoleFormatter('')
# add formatter to ch
ch.setFormatter(formatter)
# add ch to logger
log.addHandler(ch) Whenever you need this logger, import it as I haven't tested it, but that would be the idea. If we don't need the |
Thanks for the explanation. I thought |
By the way: Great to see you!
Not always, it depends where you call it. See this StackOverflow article:
In other words, Hierarchical loggers can be used if you want to distinguish between different modules inside your project. For example, you can configure a transformation and an analyze logger, but with different log levels. One could log into a file, the other prints the message to the console. Just a matter of configuration. 😉
The same you get when you split something into a new file: separation. 🙂 It's probably a matter of taste where you put it. As you need to access your logger in different places of the project, I found it convenient to add it to a dedicated Additionally, if I want to switch from Python native setup to a INI configuration file setup, I think it's easier to add everything into this file. However, that's my taste and how I prefer to do it. It doesn't mean it's a standard (although I've found it in several projects). |
My tomschr/example-cli-argparse.py gist contains an example of setting up the logger with a dictionary object. |
Thanks :-) I added another commit which bundles the setup and the logger in |
Looking at your draft, I think the function import logging
logger = logging.getLogger("rng2doc")
_handler = logging.StreamHandler()
_handler.setFormatter(logging.Formatter('[%(levelname)s] %(message)s'))
logger.addHandler(_handler) As the |
I have added the function because I wanted to prevent this: ᐅ ipython
Python 3.6.12 (default, Dec 02 2020, 09:44:23) [GCC]
Type 'copyright', 'credits' or 'license' for more information
IPython 7.16.1 -- An enhanced Interactive Python. Type '?' for help.
In [1]: import logging
In [2]: logger = logging.getLogger("rng2doc")
In [3]: logger.handlers
Out[3]: []
In [4]: from rng2doc import rng
In [5]: logger.handlers
Out[5]: [<StreamHandler <stderr> (NOTSET)>] I can add a NullHandler instead. |
That would be a good idea. 👍 It's also recommended in the Hitchhiker's Guide to Python. |
Problem
In most modules there is this line:
However, we have already a logging module: rng2doc/log.py.
Solution
Use the "right" logging module. 😉
The text was updated successfully, but these errors were encountered: