@@ -49,7 +49,7 @@ class LogProducer:
49
49
"""
50
50
51
51
transport = attr .ib (type = ITransport )
52
- _handler = attr .ib (type = "RemoteHandler" )
52
+ _handler = attr .ib (type = "Optional[ RemoteHandler] " )
53
53
_paused = attr .ib (default = True , type = bool , init = False )
54
54
55
55
def pauseProducing (self ):
@@ -63,6 +63,9 @@ def resumeProducing(self):
63
63
# If we're already producing, nothing to do.
64
64
self ._paused = False
65
65
66
+ # _handler should always be set while this is resumed.
67
+ assert self ._handler
68
+
66
69
# Loop until paused.
67
70
while self ._paused is False and (
68
71
self ._handler ._buffer and self .transport .connected
@@ -84,7 +87,7 @@ def resumeProducing(self):
84
87
85
88
class RemoteHandler (logging .Handler ):
86
89
"""
87
- An IObserver that writes JSON logs to a TCP target.
90
+ An logging handler that writes logs to a TCP target.
88
91
89
92
Args:
90
93
host: The host of the logging target.
@@ -98,7 +101,7 @@ def __init__(
98
101
port : int ,
99
102
maximum_buffer : int = 1000 ,
100
103
level = logging .NOTSET ,
101
- reactor = None ,
104
+ _reactor = None ,
102
105
):
103
106
super ().__init__ (level = level )
104
107
self .host = host
@@ -111,22 +114,24 @@ def __init__(
111
114
self ._producer = None # type: Optional[LogProducer]
112
115
113
116
# Connect without DNS lookups if it's a direct IP.
114
- if reactor is None :
117
+ if _reactor is None :
115
118
from twisted .internet import reactor
116
119
120
+ _reactor = reactor
121
+
117
122
try :
118
123
ip = ip_address (self .host )
119
124
if isinstance (ip , IPv4Address ):
120
- endpoint = TCP4ClientEndpoint (reactor , self .host , self .port )
125
+ endpoint = TCP4ClientEndpoint (_reactor , self .host , self .port )
121
126
elif isinstance (ip , IPv6Address ):
122
- endpoint = TCP6ClientEndpoint (reactor , self .host , self .port )
127
+ endpoint = TCP6ClientEndpoint (_reactor , self .host , self .port )
123
128
else :
124
129
raise ValueError ("Unknown IP address provided: %s" % (self .host ,))
125
130
except ValueError :
126
131
endpoint = HostnameEndpoint (reactor , self .host , self .port )
127
132
128
133
factory = Factory .forProtocol (Protocol )
129
- self ._service = ClientService (endpoint , factory , clock = reactor )
134
+ self ._service = ClientService (endpoint , factory , clock = _reactor )
130
135
self ._service .startService ()
131
136
self ._connect ()
132
137
0 commit comments