Comments (9)
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.
from rng2doc.
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?
from rng2doc.
hi JΓΌrgen, π
well, I guess you can initialize the logger instance in rng2doc/log.py
. Under Configuring the logger you can see an example. To adapt that to our code, I could imagine something like this:
# 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 from log import log
and use it like log.debug("Hi")
.
I haven't tested it, but that would be the idea. If we don't need the CustomConsoleFormatter
class anymore, the whole setup would be a bit simpler.
from rng2doc.
Thanks for the explanation. I thought logging.getLogger("rng2doc")
/logging.getLogger(__package__)
will return always the same object. What advantage do we get importing it from log.py
? Draft: https://github.com/openSUSE/rng2doc/pull/61/files
from rng2doc.
By the way: Great to see you!
Thanks for the explanation. I thought
logging.getLogger("rng2doc")
/logging.getLogger(__package__)
will return always the same object.
Not always, it depends where you call it. See this StackOverflow article:
"So, for a module located in
foo/bar/baz.py
,__name__
is set tofoo.bar.baz
, and__package__
is set tofoo.bar
, whilefoo/bar/__init__.py
will havefoo.bar
for both the__name__
and__package__
attributes."
In other words, __package__
can be used to create hierarchical loggers, whereas "rng2doc"
will always return the parent logger instance, regardless of the module level.
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. π
What advantage do we get importing it from log.py?
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 log.py
file and import it wherever I need it. That way I can bundle all the necessary setup steps in one file and only import the logger instance.
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).
from rng2doc.
My tomschr/example-cli-argparse.py gist contains an example of setting up the logger with a dictionary object.
from rng2doc.
By the way: Great to see you!
Thanks :-)
I added another commit which bundles the setup and the logger in log.py
again.
from rng2doc.
I added another commit which bundles the setup and the logger in
log.py
again.
Looking at your draft, I think the function setup_logging()
is unnecessary. Why? Because it's enough to just import the logger
instance. I envision something like this:
import logging
logger = logging.getLogger("rng2doc")
_handler = logging.StreamHandler()
_handler.setFormatter(logging.Formatter('[%(levelname)s] %(message)s'))
logger.addHandler(_handler)
As the logger.setLevel(level)
call is done after CLI parsing, you don't need that in the above code.
from rng2doc.
I can add a NullHandler instead.
That would be a good idea. π It's also recommended in the Hitchhiker's Guide to Python.
from rng2doc.
Related Issues (20)
- dummy issue
- dummy issue
- dummy issue
- dummy issue
- Add timing functionality
- Add manpage
- Use English Text for HTML output HOT 1
- Create HTML filenames which refer to the element name
- Include RELAX NG schema?
- Pass xsl:message to logging
- Improve consistency of --output/--output-format options HOT 1
- Output Sphinx/RST HOT 1
- Idea: Add TDG style in addition to SVG graphics
- When called from a different directory, rng2doc fails to find relaxng.rng HOT 5
- SVG display issues in Chrome HOT 1
- Allowing adding custom HTML <title/> / navbar-header text
- Directly allow using RNC schemas
- When there are two alternative regex patterns, allowed attribute values are displayed incorrectly in HTML attributes table
- Markdown support for a:documentation content
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. πππ
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from rng2doc.