I created some Python files keeping my functions a bit separated to ease working / fixing. All files are in one directory. The structure may get broken down to something like:
- a.py (a class A with basic stuff)
- b.py (a class B with basic stuff)
- modA.py (create a class C deriving from A and B)
- modB.py (create a class D deriving from A and B)
- ...
- main_a.py (using class C)
- main_b.py (using class D)
Every module uses the logging stuff from python. An why so ever - only the root logger messages are written. And I don't see my error.
Here is a minimal example.
a.py
import logging
logger = logging.getLogger(__name__)
class A(object):
def __init__(self):
logger.debug("Instance of A")
b.py
import logging
logger = logging.getLogger(__name__)
class B(object):
def __init__(self):
logger.debug("Instance of B")
ab.py
import a
import b
import logging
logger = logging.getLogger(__name__)
class AB(a.A, b.B):
def __init__(self):
logger.debug("Instance of AB")
a.A.__init__(self)
b.B.__init__(self)
main_one.py
import sys
import ab
import logging
import logging.handlers
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
handler = logging.StreamHandler(stream=sys.stderr)
handler.setLevel(logging.DEBUG)
handler.setFormatter(logging.Formatter('%(name)s: %(message)s'))
logger.addHandler(handler)
logger.warning("The trouble starts")
ab = ab.AB()
I also tried to use something like self.logger = logging.getLogger(type(self).__name__)
to do logging on a per class base, but the result is the same. So may one of you point out where I went wrong when reading the python logging manuals?
TIA.
EDIT 1: My solution
Thanks to both, @falsetru and @Jakub M., using both answers leads to a working solution.
First I put everything in a hierarchy.
main_one.py
lib/
__init__.py
ab.py
basic/
__init__.py
a.py
b.py
Second I changed the logger = logging.getLogger(__name__)
in main_one.py
to logger = logging.getLogger()
(No name for the root logger!).
That did the trick.
Very helpful was a code snippet on GitHub.