Hey Marcus,
My opinion is that if its a library (I am not exactly sure from the small
examples, if they are) should not install handlers on the logger. They
should just establish one and log to it. Then the entry point / application
can set up the handlers. Something like this?
## wrapper.py ##import loggingimport original
log = logging.getLogger(__name__)# Prevent library messages about not
having a log handler
log.addHandler(logging.NullHandler())
def shout():
original.shout()
log.info("Woorld")
def setup_log():
formatter = logging.Formatter('[wrapper] - %(message)s')
root = logging.getLogger()
root.setLevel(logging.INFO)
stream_handler = logging.StreamHandler()
stream_handler.setFormatter(formatter)
root.addHandler(stream_handler)
if __name__ == '__main__':
setup_log()
shout()
*## original.py*import logging
log = logging.getLogger(__name__)# Prevent library messages about not
having a log handler
log.addHandler(logging.NullHandler())
def shout():
log.info("Heyaa")
def setup_log():
formatter = logging.Formatter('[original] - %(message)s')
root = logging.getLogger()
root.setLevel(logging.INFO)
stream_handler = logging.StreamHandler()
stream_handler.setFormatter(formatter)
root.addHandler(stream_handler)
if __name__ == '__main__':
setup_log()
shout()
$ python wrapper.py
[wrapper] - Heyaa
[wrapper] - Woorld
$ python original.py
[original] - Heyaa
There are probably other ways to go about it I am sure.
(By the way, that Markdown thing is pretty awesome. Thanks)
-- justin
On Wed, May 14, 2014 at 9:44 PM, Marcus Ottosson <[email protected]>wrote:
> 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<https://groups.google.com/d/msgid/python_inside_maya/CAFRtmOBN9e2Df-fVTCn49hJdrh8j174NrJgg-L370YK7P7oKBw%40mail.gmail.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>
--
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/CAPGFgA0smspo0DEFRaFCSvuDhkb1o3HFdDwTpZ%3DD5AHWpYoyzw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.