Skip to content

Commit

Permalink
Log exception through logging system
Browse files Browse the repository at this point in the history
The default `sys.excepthook` (also available as `sys.__excepthook__` for
future reference) formats an exception and prints it to stderr. This
eventually shows up in the Android logging system through
pythonforandroid's stderr to Log converter, but that suffers all the
same problems that `AndroidLogHandler` addresses. Furthermore, since the
exception is sent directly to stderr, it doesn't get recorded in the
`kolibri.txt` file through its logging handler. Recording it with
`logging.critical` means it goes to both places with proper formatting
and priority.
  • Loading branch information
dbnicholson committed May 17, 2023
1 parent b6c677e commit d54d710
Showing 1 changed file with 4 additions and 6 deletions.
10 changes: 4 additions & 6 deletions src/kolibri_android/globals.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import logging
import os
import sys
import traceback
from functools import partial
from logging.config import dictConfig
from pathlib import Path

Expand All @@ -11,7 +11,6 @@
from .android_utils import get_log_root
from .android_utils import get_logging_config


SCRIPT_PATH = Path(__file__).absolute().parent.parent

FirebaseCrashlytics = autoclass("com.google.firebase.crashlytics.FirebaseCrashlytics")
Expand All @@ -32,12 +31,11 @@ def initialize():
sys.path.append(SCRIPT_PATH.joinpath("kolibri", "dist").as_posix())
sys.path.append(SCRIPT_PATH.joinpath("extra-packages").as_posix())

sys.excepthook = partial(log_exception, default_excepthook=sys.excepthook)
sys.excepthook = log_exception


def log_exception(type, value, tb, default_excepthook=None):
if callable(default_excepthook):
default_excepthook(type, value, tb)
def log_exception(type, value, tb):
logging.critical(str(value), exc_info=(type, value, tb))
FirebaseCrashlytics.getInstance().recordException(
PythonException(Arrays.toString(traceback.format_exception(type, value, tb)))
)

0 comments on commit d54d710

Please sign in to comment.