0

I have written a code in twisted .I need to write the log information in when we have call d.addErrback(on_failure).

from twisted.python import log log.startLogging(open('/home/crytek.etl/foo.log', 'w'))

def on_failure(failure): log.msg(failure)

d.addErrback(on_failure)

Is this the correct way of implementing this.I don't get any values written to the file.Can someone suggest on how this can be implemented

1 Answer 1

0

You probably want to consider opening your log file in append mode. Otherwise, every time your application starts you'll wipe out all your old logs. This could make it appear as though the log messages you're expecting to see aren't being logged.

from twisted.python import log
log.startLogging(open('/home/crytek.etl/foo.log', 'a'))

You should also log failures using log.err instead of log.msg

def on_failure(failure):
    log.err(failure)

And you can do this more easily since on_failure has exactly the same signature as log.err. Just write:

d.addErrback(log.err)

Also, I liked, log.err doesn't have exactly the same signature as on_failure. It is better, it accepts a 2nd argument which is used to present a header for the failure in the log file. You can use it like this:

d.addErrback(log.err, "Frobbing the widget failed")

This will present "Frobbing the widget failed" together with the failure in the log file.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.