Hey JP - we were trying to do something very similar ourselves, where we
were trying to create a logger-per-instance.  The trick is not to use
logging.getLogger, but to just create an instance of the class directly.
 Here's a small wrapper method we use:

def uniqueLogger(name, parent=None):
    '''Retrieve a unique logger.

    Using logging.getLogger will reuse loggers if the same name is given;
    however, in some cases (for instance, we want a logger per instance of a
    class) this is not desirable, and we always want a unique logger.'''
    logger = logging.getLoggerClass()(name)
    if parent is not None:
        # Since we're not using getLogger, need to set parent ourselves...
        logger.parent = parent
    return logger


On Mon, Jul 15, 2013 at 8:21 PM, John Patrick <[email protected]> wrote:

> Hey all,
>
> Let's say I have an class called Model, and a class called View.  The view
> instance collects lots of Model instances and as the user manipulates the
> models, the view is updated.  In this case, one thing I want to have in the
> view is a text area where my model objects can display messages as they are
> manipulated, both for debugging and for user feedback.
>
> I wanted to set this up by adding a logger instance to each Model instance
> in it's __init__ method, so that other methods can call it to output
> messages:
>
> Model(object):
>   def __init__(self):
>     self.logger = logging.getLogger(self.__class__.__name__) #or whatever
>
> inst_a = Model()
> inst_b = Model()
>
>
> The problem is that I want to connect and disconnect the different model
> object's loggers to different text widgets (via a special handler),
> filehandlers, etc.   Loggers are cached by name, though, so now each of my
> instances are sharing pointers to the same logger.
>
> So -  AFAIK, I have to do something like use a uuid in my getLogger call
> to use the Logger class.  This feels like an abuse of how loggers are
> designed to work, and since the logging module seems to cache instances,
> I'm concerned about memory leaks.  Especially in a Maya situation where a
> user has a long-running Python process.
>
> Has anyone had a similar experience and found a clean solution for this
> problem?  Really, I'd just like a non-cached logger instance.  The docs
> lead me to believe it's not possible, but this seems like it would be a
> fairly common scenario.
>
> Thanks!
> JP
>
>
>
>
>
>
> --
> John Patrick
> 404-242-2675
> [email protected]
> http://www.canyourigit.com
>
> --
> 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 post to this group, send email to [email protected].
> For more options, visit https://groups.google.com/groups/opt_out.
>
>
>

-- 
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 post to this group, send email to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to