I hope this is clear enough, I’m some having trouble myself in breaking
this down properly and I’m not finding my way around the logging module
well enough to properly find a good solution.
Basic

I’ve got two packages, one which defines some functionality, like “shout()”
and another which wraps the original to provide additional functionality.

>>> import original>>> original.shout()
[original] - Heyaa

What happens

The wrapper then “wraps” the original, and adds another line to shot()

>>> import wrapper>>> wrapper.shout()
[original] - Heyaa
[wrapper] - Woorld

Here, the wrapper introduces it’s own logging, distinguished by the [wrapper]
- prefix.
What I’m looking for

However, what I’d like is for the wrapper to “take over” the original
logging, and log it as its own.

>>> import wrapper>>> wrapper.shout()
[wrapper] - Heyaa
[wrapper] - Woorld

So as to fully encapsulate the original, and not leave any hint towards it
being a wrapper to begin with.
Questions

   1. Does it make sense?
   2. What are the alternatives?
   3. How do I make it work?

Implementation

Let me illustrate how I’m doing it currently, without fully encapsulating
the wrapper; 4 files @ 2 inits and 2 implementations.

*original/__init__.py*

# original/__init__.py
def setup_log():
    import logging

    formatter = logging.Formatter('[original] - %(message)s')

    log = logging.getLogger('original')
    log.setLevel(logging.INFO)

    stream_handler = logging.StreamHandler()
    stream_handler.setFormatter(formatter)
    log.addHandler(stream_handler)

*wrapper/__init__.py*

# wrapper/__init__.py
def setup_log():
    import logging

    formatter = logging.Formatter('[original] - %(message)s')

    log = logging.getLogger('wrapper')
    log.setLevel(logging.INFO)

    stream_handler = logging.StreamHandler()
    stream_handler.setFormatter(formatter)
    log.addHandler(stream_handler)

*original/player.py*

# original/player.py
import logging
log = logging.getLogger('original')
def shout():
    log.info("Heyaa")
if __name__ == '__main__':
    import original
    original.setup_log()
    shout()
# [original] - Heyaa

*wrapper/player.py*

# wrapper/player.pyimport logging
log = logging.getLogger('wrapper')
from original import player
def shout():
    player.shout()
    log.info("Woorld")
if __name__ == '__main__':
    import wrapper
    import original
    wrapper.setup_log()
    original.setup_log()
    shout()
# [original] - Heyaa# [wrapper] - Woorld

Best,
Marcus
-- 
*Marcus Ottosson*
[email protected]

-- 
You received this message because you are subscribed to the Google Groups 
"Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/python_inside_maya/CAFRtmOBN9e2Df-fVTCn49hJdrh8j174NrJgg-L370YK7P7oKBw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to