Note that there are some explanatory texts on larger screens.

plurals
  1. POpython logging with multiple modules does not work
    primarykey
    data
    text
    <p>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:</p> <ul> <li>a.py (a class A with basic stuff)</li> <li>b.py (a class B with basic stuff)</li> <li>modA.py (create a class C deriving from A and B)</li> <li>modB.py (create a class D deriving from A and B)</li> <li>...</li> <li>main_a.py (using class C)</li> <li>main_b.py (using class D)</li> </ul> <p>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.</p> <p>Here is a minimal example.</p> <p><code>a.py</code></p> <pre><code>import logging logger = logging.getLogger(__name__) class A(object): def __init__(self): logger.debug("Instance of A") </code></pre> <p><code>b.py</code></p> <pre><code>import logging logger = logging.getLogger(__name__) class B(object): def __init__(self): logger.debug("Instance of B") </code></pre> <p><code>ab.py</code></p> <pre><code>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) </code></pre> <p><code>main_one.py</code></p> <pre><code>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() </code></pre> <p>I also tried to use something like <code>self.logger = logging.getLogger(type(self).__name__)</code> 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?</p> <p>TIA.</p> <p><strong>EDIT 1: My solution</strong></p> <p>Thanks to both, @falsetru and @Jakub M., using both answers leads to a working solution.</p> <p>First I put everything in a hierarchy.</p> <pre><code>main_one.py lib/ __init__.py ab.py basic/ __init__.py a.py b.py </code></pre> <p>Second I changed the <code>logger = logging.getLogger(__name__)</code> in <code>main_one.py</code> to <code>logger = logging.getLogger()</code> (<em>No name for the root logger!</em>).</p> <p>That did the trick.</p> <p>Very helpful was a code snippet <a href="https://github.com/mapio/sdt/tree/master/1_observing/active/logging" rel="nofollow">on GitHub</a>.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload