Better logging. Errors without stacktrace are now printed to stderr

This commit is contained in:
ahmadbilalkhalid 2019-12-22 13:14:42 +05:00
parent 88b4d34e1a
commit e4d2c98fb5
4 changed files with 37 additions and 13 deletions

View file

@ -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)
except Exception as err:
logger.exception(err)

View file

@ -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

View file

@ -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:

View file

@ -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