@@ -132,7 +132,7 @@ class SubprocessServer(object):
132132 with SubprocessServer(GrpcStubClass, [executable, arg, ...]) as stub:
133133 stub.CallService(...)
134134 """
135- def __init__ (self , stub_class , cmd , port = None ):
135+ def __init__ (self , stub_class , cmd , port = None , logger = None ):
136136 """Creates the server object.
137137
138138 :param stub_class: the auto-generated GRPC client stub class used for
@@ -143,12 +143,21 @@ def __init__(self, stub_class, cmd, port=None):
143143 service. If not given, one will be randomly chosen and the special
144144 string "{{PORT}}" will be substituted in the command line arguments
145145 with the chosen port.
146+ :param logger: (optional) The logger or logger name to use for the
147+ subprocess's stderr and stdout. If not given, the current module logger
148+ would be used.
146149 """
147150 self ._owner_id = None
148151 self ._stub_class = stub_class
149152 self ._cmd = [str (arg ) for arg in cmd ]
150153 self ._port = port
151154 self ._grpc_channel = None
155+ if isinstance (logger , str ):
156+ self ._logger = logging .getLogger (logger )
157+ elif isinstance (logger , logging .Logger ):
158+ self ._logger = logger
159+ else :
160+ self ._logger = _LOGGER
152161
153162 @classmethod
154163 @contextlib .contextmanager
@@ -203,9 +212,9 @@ def start_process(self):
203212 if self ._owner_id is not None :
204213 self ._cache .purge (self ._owner_id )
205214 self ._owner_id = self ._cache .register ()
206- return self ._cache .get (tuple (self ._cmd ), self ._port )
215+ return self ._cache .get (tuple (self ._cmd ), self ._port , self . _logger )
207216
208- def _really_start_process (cmd , port ):
217+ def _really_start_process (cmd , port , logger ):
209218 if not port :
210219 port , = pick_port (None )
211220 cmd = [arg .replace ('{{PORT}}' , str (port )) for arg in cmd ] # pylint: disable=not-an-iterable
@@ -220,7 +229,7 @@ def log_stdout():
220229 while line :
221230 # The log obtained from stdout is bytes, decode it into string.
222231 # Remove newline via rstrip() to not print an empty line.
223- _LOGGER .info (line .decode (errors = 'backslashreplace' ).rstrip ())
232+ logger .info (line .decode (errors = 'backslashreplace' ).rstrip ())
224233 line = process .stdout .readline ()
225234
226235 t = threading .Thread (target = log_stdout )
@@ -283,15 +292,17 @@ def __init__(
283292 path_to_jar ,
284293 java_arguments ,
285294 classpath = None ,
286- cache_dir = None ):
295+ cache_dir = None ,
296+ logger = None ):
287297 self ._java_path = JavaHelper .get_java ()
288298 if classpath :
289299 # java -jar ignores the classpath, so we make a new jar that embeds
290300 # the requested classpath.
291301 path_to_jar = self .make_classpath_jar (path_to_jar , classpath , cache_dir )
292302 super ().__init__ (
293303 stub_class ,
294- [self ._java_path , '-jar' , path_to_jar ] + list (java_arguments ))
304+ [self ._java_path , '-jar' , path_to_jar ] + list (java_arguments ),
305+ logger = logger )
295306 self ._existing_service = path_to_jar if is_service_endpoint (
296307 path_to_jar ) else None
297308
0 commit comments