From e4d2c98fb5822e510f6bb979a807765e95d8d6ec Mon Sep 17 00:00:00 2001 From: meow Date: Sun, 22 Dec 2019 13:14:42 +0500 Subject: [PATCH] Better logging. Errors without stacktrace are now printed to stderr --- scripts/ucloud | 47 +++++++++++++++++++++++-------- ucloud/common/etcd_wrapper.py | 1 - ucloud/common/network.py | 1 + ucloud/common/storage_handlers.py | 1 - 4 files changed, 37 insertions(+), 13 deletions(-) diff --git a/scripts/ucloud b/scripts/ucloud index 7f3ef3a..f741663 100755 --- a/scripts/ucloud +++ b/scripts/ucloud @@ -6,6 +6,8 @@ import importlib import multiprocessing as mp import sys +from logging.handlers import SysLogHandler + from ucloud.configure.main import configure_parser @@ -14,10 +16,20 @@ def exception_hook(exc_type, exc_value, exc_traceback): 'Uncaught exception', exc_info=(exc_type, exc_value, exc_traceback) ) - print('Error: ', end='') - print(exc_type, exc_value, exc_traceback) + # print('Error: ', end='') + # print(exc_type, exc_value, exc_traceback) +class NoTracebackStreamHandler(logging.StreamHandler): + def handle(self, record): + info, cache = record.exc_info, record.exc_text + record.exc_info, record.exc_text = None, None + try: + super().handle(record) + finally: + record.exc_info = info + record.exc_text = cache + if __name__ == '__main__': sys.excepthook = exception_hook @@ -44,12 +56,26 @@ if __name__ == '__main__': if not args.command: arg_parser.print_help() else: - logging.basicConfig( - level=logging.DEBUG, - format='%(pathname)s:%(lineno)d -- %(levelname)-8s %(message)s', - handlers=[logging.handlers.SysLogHandler(address = '/dev/log')] - ) - logger = logging.getLogger("ucloud") + # Setting up root logger + logger = logging.getLogger('ucloud') + + syslog_handler = SysLogHandler(address='/dev/log') + syslog_handler.setLevel(logging.DEBUG) + syslog_formatter = logging.Formatter('%(pathname)s:%(lineno)d -- %(levelname)-8s %(message)s') + syslog_handler.setFormatter(syslog_formatter) + + stream_handler = NoTracebackStreamHandler() + stream_handler.setLevel(logging.WARNING) + stream_formatter = logging.Formatter('%(message)s') + stream_handler.setFormatter(stream_formatter) + + logger.addHandler(syslog_handler) + logger.addHandler(stream_handler) + + # if we start etcd in seperate process with default settings + # i.e inheriting few things from parent process etcd3 module + # errors out, so the following command configure multiprocessing + # module to not inherit anything from parent. mp.set_start_method('spawn') arguments = vars(args) @@ -58,6 +84,5 @@ if __name__ == '__main__': mod = importlib.import_module("ucloud.{}.main".format(name)) main = getattr(mod, "main") main(**arguments) - except Exception as e: - logger.exception('Error') - print(e) \ No newline at end of file + except Exception as err: + logger.exception(err) diff --git a/ucloud/common/etcd_wrapper.py b/ucloud/common/etcd_wrapper.py index e249e6c..91149b8 100644 --- a/ucloud/common/etcd_wrapper.py +++ b/ucloud/common/etcd_wrapper.py @@ -33,7 +33,6 @@ def readable_errors(func): except etcd3.exceptions.ConnectionTimeoutError as err: raise etcd3.exceptions.ConnectionTimeoutError('etcd connection timeout') from err except Exception: - print('Some error occurred, most probably it is etcd that is erroring out.') logger.exception('Some etcd error occurred') return wrapper diff --git a/ucloud/common/network.py b/ucloud/common/network.py index 6a6c6e2..df7151b 100644 --- a/ucloud/common/network.py +++ b/ucloud/common/network.py @@ -36,6 +36,7 @@ def create_dev(script, _id, dev, ip=None): try: output = sp.check_output(command, stderr=sp.PIPE) except Exception as e: + logger.exception('Creation of interface %s failed.', dev) print(e) return None else: diff --git a/ucloud/common/storage_handlers.py b/ucloud/common/storage_handlers.py index d2bd452..eaad1a5 100644 --- a/ucloud/common/storage_handlers.py +++ b/ucloud/common/storage_handlers.py @@ -51,7 +51,6 @@ class ImageStorageHandler(ABC): output = sp.check_output(command, stderr=sp.PIPE) except Exception as e: if report: - print(e) logger.exception(e) return False return True