[Twisted-Python] Can’t change namespace for the Logger instance
Glyph Lefkowitz
glyph at twistedmatrix.com
Fri Jan 20 22:53:18 MST 2017
> On Jan 20, 2017, at 1:33 AM, Роман Мещеряков <romanmescheryakov at yandex.ru> wrote:
>
> I expect the text between ‘[‘ and ‘#’ to be ‘ad_hoc’, but it is auto-generated name instead. Am I doing something wrong or this is a bug?
This is indeed a bug! And a pretty serious one; it's explicitly documented to work otherwise. Please file it.
The reason that this happens is that the 'namespace' field is filled out by the descriptor (__get__ method) of the `Logger` object when it is bound to `self`.
You can work around this in the meanwhile by changing your example to be like so:
import sys
from twisted.logger import (Logger, textFileLogObserver)
observer = textFileLogObserver(sys.stdout)
class MyClass:
@property
def log(self):
return Logger(namespace="ad_hoc", observer=observer, source=self)
def __init__(self):
self.log.info("MyClass.__init__ called!")
def __del__(self):
self.log.info("Bye!")
obj = MyClass()
-glyph
-------------- next part --------------
An HTML attachment was scrubbed...
URL: </pipermail/twisted-python/attachments/20170120/46db2fc7/attachment-0002.html>
More information about the Twisted-Python
mailing list