[Twisted-Python] Controlling twisted logging
Hello, Having just spent some time trying to "turn off" twisted logging via log.discardLogs() and/or log.startLogging(log.NullFile()) in a twisted app that initially logs to an actual file, it seems that log.startLogging(log.NullFile()) only works as expected if no previous startLogging call using a valid file object has been made. Otherwise, messages continue to be logged. Is this observation correct, or am I overlooking some way to enable logging to a logfile and subsequently disable it? In my above experiments, log.discardLogs() seemed to have no effect either once valid file logging had been initiated with log.startLogging. Win2K, Python 2.3.3, Twisted 1.3.0 Thanks for any info, Norm Petterson
Norm Petterson wrote:
Hello,
Having just spent some time trying to "turn off" twisted logging via log.discardLogs() and/or log.startLogging(log.NullFile()) in a twisted app that initially logs to an actual file, it seems that log.startLogging(log.NullFile()) only works as expected if no previous startLogging call using a valid file object has been made. Otherwise, messages continue to be logged.
It seems the best documentation is the source! startLogging will create a FileLogObserver that wraps the passed in file-object. The "emit" method of that FileLogObserver gets added to the *list* of observers. So startLogging does not stop things being logged to files that you passed in on previous calls to startLogging. If you want to be able to start logging on a given file then you need to be more explicit about what objects are getting created. For example: (untested) flo = FileLogObserver(file) startLoggingWithObserver(flo.emit) then later you can do, e.g. flo.stop() then later flo.start()
Is this observation correct, or am I overlooking some way to enable logging to a logfile and subsequently disable it?
Looks like correct behavior to me.
In my above experiments, log.discardLogs() seemed to have no effect either once valid file logging had been initiated with log.startLogging.
Yeah, have a glace at the implementation of discardLogs() -Eric
Eric Mangold wrote:
Norm Petterson wrote:
Hello,
Having just spent some time trying to "turn off" twisted logging via log.discardLogs() and/or log.startLogging(log.NullFile()) in a twisted app that initially logs to an actual file, it seems that log.startLogging(log.NullFile()) only works as expected if no previous startLogging call using a valid file object has been made. Otherwise, messages continue to be logged.
It seems the best documentation is the source!
startLogging will create a FileLogObserver that wraps the passed in file-object. The "emit" method of that FileLogObserver gets added to the *list* of observers.
So startLogging does not stop things being logged to files that you passed in on previous calls to startLogging.
If you want to be able to start logging on a given file then you need to
Err, I meant to say *stop* logging on a given file...
be more explicit about what objects are getting created. For example: (untested)
flo = FileLogObserver(file) startLoggingWithObserver(flo.emit)
then later you can do, e.g.
flo.stop()
then later
flo.start()
Is this observation correct, or am I overlooking some way to enable logging to a logfile and subsequently disable it?
Looks like correct behavior to me.
In my above experiments, log.discardLogs() seemed to have no effect either once valid file logging had been initiated with log.startLogging.
Yeah, have a glace at the implementation of discardLogs()
-Eric
participants (2)
-
Eric Mangold
-
Norm Petterson