From cc33fbe2a4cc62b3d66f74d0a62e3b7eed68b4f1 Mon Sep 17 00:00:00 2001 From: Varad Meru Date: Thu, 23 Jun 2022 05:44:14 +0000 Subject: [PATCH 1/2] Adding a traceback to be logged --- azure_functions_worker/dispatcher.py | 6 ++++-- azure_functions_worker/logging.py | 6 ++++++ azure_functions_worker/main.py | 8 +++++--- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/azure_functions_worker/dispatcher.py b/azure_functions_worker/dispatcher.py index eaf43a66a..d00b1dd05 100644 --- a/azure_functions_worker/dispatcher.py +++ b/azure_functions_worker/dispatcher.py @@ -29,7 +29,7 @@ from .logging import disable_console_logging, enable_console_logging from .logging import enable_debug_logging_recommendation from .logging import (logger, error_logger, is_system_log_category, - CONSOLE_LOG_PREFIX) + CONSOLE_LOG_PREFIX, format_exception) from .utils.common import get_app_setting, is_envvar_true from .utils.dependency import DependencyManager from .utils.tracing import marshall_exception_trace @@ -745,7 +745,9 @@ def gen(resp_queue): if ex is grpc_req_stream: # Yes, this is how grpc_req_stream iterator exits. return - error_logger.exception('unhandled error in gRPC thread') + error_logger.exception( + 'unhandled error in gRPC thread. Exception: {0}'.format( + format_exception(ex))) raise diff --git a/azure_functions_worker/logging.py b/azure_functions_worker/logging.py index cd8a0be99..02465f01e 100644 --- a/azure_functions_worker/logging.py +++ b/azure_functions_worker/logging.py @@ -20,6 +20,12 @@ error_handler: Optional[logging.Handler] = None +def format_exception(exception): + msg = str(exception) + "\n" + msg += ''.join(traceback.format_exception(etype=type(exception), value=exception, tb=exception.__traceback__)) + return msg + + def setup(log_level, log_destination): # Since handler and error_handler are moved to the global scope, # before assigning to these handlers, we should define 'global' keyword diff --git a/azure_functions_worker/main.py b/azure_functions_worker/main.py index 3a7e44417..3edf28400 100644 --- a/azure_functions_worker/main.py +++ b/azure_functions_worker/main.py @@ -36,7 +36,7 @@ def main(): from . import logging from ._thirdparty import aio_compat - from .logging import error_logger, logger + from .logging import error_logger, logger, format_exception args = parse_args() logging.setup(log_level=args.log_level, log_destination=args.log_to) @@ -48,8 +48,10 @@ def main(): try: return aio_compat.run(start_async( args.host, args.port, args.worker_id, args.request_id)) - except Exception: - error_logger.exception('unhandled error in functions worker') + except Exception as ex: + error_logger.exception( + 'unhandled error in functions worker: {0}'.format( + format_exception(ex))) raise From cc1588faa565ba9bdbb6ede442b865c03537310b Mon Sep 17 00:00:00 2001 From: Varad Meru Date: Thu, 23 Jun 2022 05:50:42 +0000 Subject: [PATCH 2/2] Adding missing import --- azure_functions_worker/logging.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/azure_functions_worker/logging.py b/azure_functions_worker/logging.py index 02465f01e..2f701d4bd 100644 --- a/azure_functions_worker/logging.py +++ b/azure_functions_worker/logging.py @@ -4,6 +4,7 @@ import logging import logging.handlers import sys +import traceback from typing import Optional # Logging Prefixes @@ -22,7 +23,8 @@ def format_exception(exception): msg = str(exception) + "\n" - msg += ''.join(traceback.format_exception(etype=type(exception), value=exception, tb=exception.__traceback__)) + msg += ''.join(traceback.format_exception( + etype=type(exception), value=exception, tb=exception.__traceback__)) return msg