[py-dev] py.log and multiple keywords
Grig Gheorghiu
grig at gheorghiu.net
Fri Jun 17 19:59:04 CEST 2005
--- Grig Gheorghiu <grig at gheorghiu.net> 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.
>
> I'm thinking that the _getconsumer function could be a generator that
> yields a message-processing function for each keyword it finds in its
> registry. What do you think?
>
> Grig
Sorry if I get boring by pounding on this topic, but I've been playing
with the py.log stuff by incorporating it in a framework that I'm using
at my work, and Holger's latest refactoring + my multiple keyword idea
above works really well. Here's a section of a configuration file I
have (I'm not using the py library directly yet, instead I just
concatenated the consumer and producer modules in a module called
log.py):
###############
import os
import log as qalog
workingdir = "/data01/qa/work"
restoredir = "/data01/qa/restore"
# logging stuff
run_id = qautils.get_run_id(workingdir)
logfile = os.path.join(workingdir, "%s.out" % run_id)
print "Log file for debug messages is %s" % logfile
qalog.setconsumer('console', qalog.STDOUT)
qalog.setconsumer('logfile', open(str(logfile), 'w', buffering=1))
log = qalog.Producer('console logfile')
log.debug = qalog.Producer('logfile')
log.info = qalog.Producer('console logfile')
log.warn = qalog.Producer('console logfile')
log.error = qalog.Producer('console logfile')
log.critical = qalog.Producer('console logfile')
###############
And here's how I use it in my test code:
log.debug("Test duration:", self.result.duration)
log.info("COMMAND: " + command)
log.error("Cannot log data to DB when result is not defined")
Here's how messages are printed on the console:
[console:logfile] COMMAND: tar -xv /data01/qa/work.tar
Here's how messages are printed in the log file:
[logfile] add_memo for result_id 35415 returned:
'Memo added successfully'
So I think that the py.log stuff is on the right track. Holger, you da
man :-)
Grig
More information about the Pytest-dev
mailing list