Pushing logging on to another thread was a ridiculous idea. it means CRITICAL
logs are in a race condition to write to file before the main thread exits.
This commit is contained in:
parent
6405ee2582
commit
470f85b009
1 changed files with 2 additions and 16 deletions
|
@ -1,4 +1,4 @@
|
||||||
import logging, logging.handlers, os, queue, sys, threading, time, typing
|
import logging, logging.handlers, os, queue, sys, time, typing
|
||||||
|
|
||||||
LEVELS = {
|
LEVELS = {
|
||||||
"trace": logging.DEBUG-1,
|
"trace": logging.DEBUG-1,
|
||||||
|
@ -54,16 +54,6 @@ class Log(object):
|
||||||
warn_handler.setFormatter(formatter)
|
warn_handler.setFormatter(formatter)
|
||||||
self.logger.addHandler(warn_handler)
|
self.logger.addHandler(warn_handler)
|
||||||
|
|
||||||
self._queue = queue.Queue() # type: queue.Queue[typing.Tuple[str, typing.List, int, typing.Dict]]
|
|
||||||
self._thread = threading.Thread(target=self._loop)
|
|
||||||
self._thread.daemon = True
|
|
||||||
self._thread.start()
|
|
||||||
|
|
||||||
def _loop(self):
|
|
||||||
while True:
|
|
||||||
message, params, level, kwargs = self._queue.get(block=True)
|
|
||||||
self.logger.log(level, message, *params, **kwargs)
|
|
||||||
|
|
||||||
def trace(self, message: str, params: typing.List, **kwargs):
|
def trace(self, message: str, params: typing.List, **kwargs):
|
||||||
self._log(message, params, LEVELS["trace"], kwargs)
|
self._log(message, params, LEVELS["trace"], kwargs)
|
||||||
def debug(self, message: str, params: typing.List, **kwargs):
|
def debug(self, message: str, params: typing.List, **kwargs):
|
||||||
|
@ -77,8 +67,4 @@ class Log(object):
|
||||||
def critical(self, message: str, params: typing.List, **kwargs):
|
def critical(self, message: str, params: typing.List, **kwargs):
|
||||||
self._log(message, params, logging.CRITICAL, kwargs)
|
self._log(message, params, logging.CRITICAL, kwargs)
|
||||||
def _log(self, message: str, params: typing.List, level: int, kwargs: dict):
|
def _log(self, message: str, params: typing.List, level: int, kwargs: dict):
|
||||||
if kwargs.get("exc_info") == True:
|
self.logger.log(level, message, *params, **kwargs)
|
||||||
# because we're doing the actual logging on another thread,
|
|
||||||
# we need to catch actual exception information here.
|
|
||||||
kwargs["exc_info"] = sys.exc_info()
|
|
||||||
self._queue.put((message, params, level, kwargs))
|
|
||||||
|
|
Loading…
Reference in a new issue