[py-dev] py.log and multiple keywords
holger krekel
hpk at trillke.net
Fri Jun 17 23:04:58 CEST 2005
Hi Grig,
On Fri, Jun 17, 2005 at 09:15 -0700, Grig Gheorghiu wrote:
> Holger and others interested in this topic,
>
> I was thinking that in many situations it would be helpful to be able
> to specify multiple keywords that would each point to a different
> consumer and that would be all 'kick in' on a call to the producer.
> Basically, I'd want this test to pass:
>
> def test_multiple_keywords(self):
> log = py.log.Producer('console logfile db')
> py.log.setconsumer('console', py.log.STDOUT)
> logfile = tempdir.join('mylogfile.out')
> py.log.setconsumer('logfile', open(str(logfile), 'w',
> buffering=1))
> dbfile = tempdir.join('mydbfile.out')
> py.log.setconsumer('db', open(str(dbfile), 'w', buffering=1))
>
> res, out, err = callcapture(log, "hello")
> assert out.strip() == "[console:logfile:db] hello"
>
> assert logfile.readlines() == ['[console:logfile:db] hello\n']
> assert dbfile.readlines() == ['[console:logfile:db] hello\n']
>
> Right now the last 2 assert statements fail, because _getconsumer
> returns as soon as it finds a match for the first keyword, so only the
> 'console' consumer gets a chance to consume the message.
Yes, however with the current svn version you can simply do:
def test_multiple_keywords(self):
log = py.log.Producer('console logfile db')
logfile = open(tempdir.join('mylogfile.out'), 'a', 1)
dbfile = open(tempdir.join('mydbfile.out'), 'a', 1)
def multilogger(msg):
if 'logfile' in msg.keywords:
print >>logfile, str(msg)
if 'db' in msg.keywords:
print >>dbfile, str(msg)
py.log.setconsumer('console', multilogger)
res, out, err = callcapture(log, "hello")
assert out.strip() == "[console:logfile:db] hello"
assert logfile.readlines() == ['[console:logfile:db] hello\n']
assert dbfile.readlines() == ['[console:logfile:db] hello\n']
So it seems that with the simple ability to associate functions
to keywords you can basically do a lot of stuff without knowing
any big API or intricate details.
cheers,
holger
More information about the Pytest-dev
mailing list