Twisted starting/stopping factory/protocol less noisy log messages

前端 未结 3 1652
面向向阳花
面向向阳花 2020-12-19 07:26

Is there a way to tell to twistd to not log all factory and protocol start and stop. I use many type of protocols and performs a lot of connections ... and my log file grows

相关标签:
3条回答
  • 2020-12-19 07:59

    The other answers are great, but you can also do this at the logging level only:

    import logging
    logging.getLogger('twisted').setLevel(logging.CRITICAL)
    
    0 讨论(0)
  • 2020-12-19 08:00

    You can set the noisy attribute of a factory to False to prevent it from logging these messages. See also http://twistedmatrix.com/trac/ticket/4021 which will probably be resolved by the next Twisted release.

    For example, here's a program with two clients, but only one will log its start/stop messages:

    import sys
    
    from twisted.internet import reactor, protocol
    from twisted.python import log
    
    log.startLogging(sys.stdout)
    
    f = protocol.ClientFactory()
    f.protocol = protocol.Protocol
    reactor.connectTCP('localhost', 22, f)
    
    g = protocol.ClientFactory()
    g.noisy = False
    g.protocol = protocol.Protocol
    reactor.connectTCP('localhost', 25, g)
    
    reactor.run()
    

    And the output:

    2011-02-22 09:43:23-0500 [-] Log opened.
    2011-02-22 09:43:23-0500 [-] Starting factory <twisted.internet.protocol.ClientFactory instance at 0x8f422cc>
    ^C
    2011-02-22 09:43:26-0500 [-] Received SIGINT, shutting down.
    2011-02-22 09:43:26-0500 [Protocol,client] Stopping factory <twisted.internet.protocol.ClientFactory instance at 0x8f422cc>
    2011-02-22 09:43:26-0500 [-] Main loop terminated.
    
    0 讨论(0)
  • 2020-12-19 08:08

    As a more system wide solution you can try the following:

    from twisted.internet.protocol import Factory
    
    Factory.noisy = False
    
    0 讨论(0)
提交回复
热议问题