[Twisted-Python] Log rotates not as expected

Lucas Taylor ltaylor.volks at gmail.com
Fri Jan 22 16:21:36 EST 2010

On 1/22/10 3:15 AM, Pet wrote:
> On Fri, Jan 22, 2010 at 12:13 AM, Lucas Taylor <ltaylor.volks at gmail.com> wrote:
>> On 1/21/10 11:17 AM, Pet wrote:
>>> On Thu, Jan 21, 2010 at 7:02 PM, Maarten ter Huurne
>>> <maarten at treewalker.org> wrote:
>>>> On Thursday 21 January 2010, Lucas Taylor wrote:
>>>>> This can occur if you have multiple instances of logfile.LogFile setup
>>>>> to manage the same file. The default rotation is 1MB, so maybe you have
>>>>> another logfile.LogFile somewhere?
>>>> Maybe twistd's log rotation?
>>> Yes, may be. I start my daemon with
>>> /usr/bin/twistd -y mydaemon.py --logfile=/var/log/my.log
>>> --pidfile=/var/lock/mydaemon.pid
>>> How do I start twistd, so it doesn't produce own log file?
>>> Thanks for help!
>>> Pet
>> You can customize the application to use your logfile and observer:
>> http://twistedmatrix.com/documents/current/core/howto/application.html#auto6
>> e.g
>> class MyLog(log.FileLogObserver):
>>    def emit(self, logEntryDict):
>>        log.FileLogObserver.timeFormat = '%Y-%m-%d %H:%M:%S'
>>        log.FileLogObserver.emit(self, logEntryDict)
>> maxLogSize = 5000000
>> logFile = logfile.LogFile("my.log", "/var/log", rotateLength=maxLogSize,
>> maxRotatedFiles=50)
>> application = service.Application("myapp")
>> application.setComponent(log.ILogObserver, MyLog(logFile).emit)
> Thanks for suggestion. I'll try it out as soon as I can. Currently I
> do it in that way:
> application = service.Application("MyService")
> myLogService = myLogService(LOG_NAME, LOG_DIR)
> myLogService.setServiceParent(application)
> what is the difference between creating service and setting
> setServiceParent and setComponent?
> Pet

The important aspect is the setComponent api. This is the part that lets
you override the default logging behavior of twistd. It has nothing to
do with the services that you register with the application using

If your MyLogService only does what you originally posted, you probably
don't need all of that machinery. Using twistd will take care of
starting and stopping logging for you.

But, if you really want to use your service (say you want to force
rotation on a restart), then you can do so. You just need to set the
ILogObserver component on the application using your observer's emit


application = service.Application("MyService")
myLogService = myLogService(LOG_NAME, LOG_DIR)
application.setComponent(log.ILogObserver, myLogService.loclog.emit)

Note that this won't work with your original MyLogService implementation
without some reorganization (move logfile and loclog creation up to

More information about the Twisted-Python mailing list