Skip to content

Commit 6e122b9

Browse files
committed
deviceserver: get the module logger at global level.
Instead of having the individual functions getting the module logger themselves, get the module logger at global level as recommended by the logging documentation.
1 parent c75ed7a commit 6e122b9

File tree

1 file changed

+26
-25
lines changed

1 file changed

+26
-25
lines changed

microscope/deviceserver.py

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@
3838

3939
import microscope.devices
4040

41+
42+
_logger = logging.getLogger(__name__)
43+
44+
4145
# Pyro configuration. Use pickle because it can serialize numpy ndarrays.
4246
Pyro4.config.SERIALIZERS_ACCEPTED.add('pickle')
4347
Pyro4.config.SERIALIZER = 'pickle'
@@ -136,7 +140,6 @@ def clone(self):
136140

137141
def run(self):
138142
cls_name = self._device_def['cls'].__name__
139-
logger = logging.getLogger(__name__)
140143

141144
# If the multiprocessing start method is fork, the child
142145
# process gets a copy of the root logger. The copy is
@@ -172,8 +175,8 @@ def run(self):
172175
try:
173176
self._device.initialize()
174177
except Exception as e:
175-
logger.info("Failed to start device. Retrying in 5s.",
176-
exc_info=e)
178+
_logger.info("Failed to start device. Retrying in 5s.",
179+
exc_info=e)
177180
time.sleep(5)
178181
else:
179182
break
@@ -194,7 +197,7 @@ def run(self):
194197
log_handler.setFormatter(_create_log_formatter(cls_name))
195198
root_logger.addHandler(log_handler)
196199

197-
logger.info('Device initialized; starting daemon.')
200+
_logger.info('Device initialized; starting daemon.')
198201

199202
pyro_daemon.register(self._device, cls_name)
200203
if isinstance(self._device, microscope.devices.ControllerDevice):
@@ -216,9 +219,10 @@ def run(self):
216219
pyro_thread = Thread(target = pyro_daemon.requestLoop)
217220
pyro_thread.daemon = True
218221
pyro_thread.start()
219-
logger.info('Serving %s' % pyro_daemon.uriFor(self._device))
222+
_logger.info('Serving %s' % pyro_daemon.uriFor(self._device))
220223
if isinstance(self._device, microscope.devices.FloatingDeviceMixin):
221-
logger.info('Device UID on port %s is %s' % (port, self._device.get_id()))
224+
_logger.info('Device UID on port %s is %s'
225+
% (port, self._device.get_id()))
222226
# Wait for termination event. We should just be able to call
223227
# wait() on the exit_event, but this causes issues with locks
224228
# in multiprocessing - see http://bugs.python.org/issue30975 .
@@ -234,7 +238,6 @@ def run(self):
234238

235239

236240
def serve_devices(devices, exit_event=None):
237-
logger = logging.getLogger(__name__)
238241
root_logger = logging.getLogger()
239242

240243
log_handler = RotatingFileHandler("__MAIN__.log")
@@ -260,7 +263,7 @@ def serve_devices(devices, exit_event=None):
260263
def term_func(sig, frame):
261264
"""Terminate subprocesses cleanly."""
262265
if parent == multiprocessing.current_process ():
263-
logger.debug("Shutting down all servers.")
266+
_logger.debug("Shutting down all servers.")
264267
exit_event.set()
265268
# Join keep_alive_thread so that it can't modify the list
266269
# of servers.
@@ -280,7 +283,7 @@ def term_func(sig, frame):
280283

281284
# Group devices by class.
282285
if not by_class:
283-
logger.critical("No valid devices specified. Exiting")
286+
_logger.critical("No valid devices specified. Exiting")
284287
sys.exit()
285288

286289
for cls, devs in by_class.items():
@@ -316,27 +319,26 @@ def keep_alive():
316319
if s.is_alive():
317320
continue
318321
else:
319-
logger.info(("DeviceServer Failure. Process %s is dead with"
320-
" exitcode %s. Restarting...")
321-
% (s.pid, s.exitcode))
322+
_logger.info("DeviceServer Failure. Process %s is dead with"
323+
" exitcode %s. Restarting..."
324+
% (s.pid, s.exitcode))
322325
servers.remove(s)
323326
servers.append(s.clone())
324327

325328
try:
326329
s.join(30)
327330
except:
328-
logger.error("... could not join PID %s." % (old_pid))
331+
_logger.error("... could not join PID %s." % (old_pid))
329332
else:
330333
old_pid = s.pid
331334
del (s)
332335
servers[-1].start()
333-
logger.info(("... DeviceServer with PID %s restarted"
334-
" as PID %s.")
335-
% (old_pid, servers[-1].pid))
336+
_logger.info("... DeviceServer with PID %s restarted"
337+
" as PID %s." % (old_pid, servers[-1].pid))
336338
if len(servers) == 0:
337339
# Log and exit if no servers running. May want to change this
338340
# if we add some interface to interactively restart servers.
339-
logger.info("No servers running. Exiting.")
341+
_logger.info("No servers running. Exiting.")
340342
exit_event.set()
341343
try:
342344
time.sleep(5)
@@ -350,20 +352,20 @@ def keep_alive():
350352
try:
351353
time.sleep(5)
352354
except (KeyboardInterrupt, IOError):
353-
logger.debug("KeyboardInterrupt or IOError")
355+
_logger.debug("KeyboardInterrupt or IOError")
354356
exit_event.set()
355357

356-
logger.debug("Shutting down servers ...")
358+
_logger.debug("Shutting down servers ...")
357359
while len(servers) > 0:
358360
for s in servers:
359361
if not s.is_alive():
360362
servers.remove(s)
361363
del(s)
362364
time.sleep(1)
363-
logger.info(" ... No more servers running.")
364-
logger.debug("Joining threads ...")
365+
_logger.info(" ... No more servers running.")
366+
_logger.debug("Joining threads ...")
365367
keep_alive_thread.join()
366-
logger.debug("... Threads joined. Exiting.")
368+
_logger.debug("... Threads joined. Exiting.")
367369
return
368370

369371

@@ -413,7 +415,6 @@ def validate_devices(configfile):
413415

414416
def __console__():
415417
"""Serve devices from a console process."""
416-
logger = logging.getLogger(__name__)
417418
root_logger = logging.getLogger()
418419
if __debug__:
419420
root_logger.setLevel(logging.DEBUG)
@@ -427,13 +428,13 @@ def __console__():
427428
root_logger.addFilter(Filter())
428429

429430
if len(sys.argv) < 2:
430-
logger.critical("No config file specified. Exiting.")
431+
_logger.critical("No config file specified. Exiting.")
431432
devices = []
432433
else:
433434
try:
434435
devices = validate_devices(sys.argv[1])
435436
except Exception as e:
436-
logger.critical(e)
437+
_logger.critical(e)
437438
devices = []
438439

439440
if not devices:

0 commit comments

Comments
 (0)