forked from uncloud/uncloud
Better logging. Errors without stacktrace are now printed to stderr
This commit is contained in:
parent
88b4d34e1a
commit
e4d2c98fb5
4 changed files with 37 additions and 13 deletions
|
@ -6,6 +6,8 @@ import importlib
|
||||||
import multiprocessing as mp
|
import multiprocessing as mp
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
|
from logging.handlers import SysLogHandler
|
||||||
|
|
||||||
from ucloud.configure.main import configure_parser
|
from ucloud.configure.main import configure_parser
|
||||||
|
|
||||||
|
|
||||||
|
@ -14,10 +16,20 @@ def exception_hook(exc_type, exc_value, exc_traceback):
|
||||||
'Uncaught exception',
|
'Uncaught exception',
|
||||||
exc_info=(exc_type, exc_value, exc_traceback)
|
exc_info=(exc_type, exc_value, exc_traceback)
|
||||||
)
|
)
|
||||||
print('Error: ', end='')
|
# print('Error: ', end='')
|
||||||
print(exc_type, exc_value, exc_traceback)
|
# 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__':
|
if __name__ == '__main__':
|
||||||
sys.excepthook = exception_hook
|
sys.excepthook = exception_hook
|
||||||
|
|
||||||
|
@ -44,12 +56,26 @@ if __name__ == '__main__':
|
||||||
if not args.command:
|
if not args.command:
|
||||||
arg_parser.print_help()
|
arg_parser.print_help()
|
||||||
else:
|
else:
|
||||||
logging.basicConfig(
|
# Setting up root logger
|
||||||
level=logging.DEBUG,
|
logger = logging.getLogger('ucloud')
|
||||||
format='%(pathname)s:%(lineno)d -- %(levelname)-8s %(message)s',
|
|
||||||
handlers=[logging.handlers.SysLogHandler(address = '/dev/log')]
|
syslog_handler = SysLogHandler(address='/dev/log')
|
||||||
)
|
syslog_handler.setLevel(logging.DEBUG)
|
||||||
logger = logging.getLogger("ucloud")
|
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')
|
mp.set_start_method('spawn')
|
||||||
|
|
||||||
arguments = vars(args)
|
arguments = vars(args)
|
||||||
|
@ -58,6 +84,5 @@ if __name__ == '__main__':
|
||||||
mod = importlib.import_module("ucloud.{}.main".format(name))
|
mod = importlib.import_module("ucloud.{}.main".format(name))
|
||||||
main = getattr(mod, "main")
|
main = getattr(mod, "main")
|
||||||
main(**arguments)
|
main(**arguments)
|
||||||
except Exception as e:
|
except Exception as err:
|
||||||
logger.exception('Error')
|
logger.exception(err)
|
||||||
print(e)
|
|
||||||
|
|
|
@ -33,7 +33,6 @@ def readable_errors(func):
|
||||||
except etcd3.exceptions.ConnectionTimeoutError as err:
|
except etcd3.exceptions.ConnectionTimeoutError as err:
|
||||||
raise etcd3.exceptions.ConnectionTimeoutError('etcd connection timeout') from err
|
raise etcd3.exceptions.ConnectionTimeoutError('etcd connection timeout') from err
|
||||||
except Exception:
|
except Exception:
|
||||||
print('Some error occurred, most probably it is etcd that is erroring out.')
|
|
||||||
logger.exception('Some etcd error occurred')
|
logger.exception('Some etcd error occurred')
|
||||||
return wrapper
|
return wrapper
|
||||||
|
|
||||||
|
|
|
@ -36,6 +36,7 @@ def create_dev(script, _id, dev, ip=None):
|
||||||
try:
|
try:
|
||||||
output = sp.check_output(command, stderr=sp.PIPE)
|
output = sp.check_output(command, stderr=sp.PIPE)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
logger.exception('Creation of interface %s failed.', dev)
|
||||||
print(e)
|
print(e)
|
||||||
return None
|
return None
|
||||||
else:
|
else:
|
||||||
|
|
|
@ -51,7 +51,6 @@ class ImageStorageHandler(ABC):
|
||||||
output = sp.check_output(command, stderr=sp.PIPE)
|
output = sp.check_output(command, stderr=sp.PIPE)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
if report:
|
if report:
|
||||||
print(e)
|
|
||||||
logger.exception(e)
|
logger.exception(e)
|
||||||
return False
|
return False
|
||||||
return True
|
return True
|
||||||
|
|
Loading…
Reference in a new issue