Error Logger Tutorial

Abstract

Its possible to tell Python to run a callback every time there is an uncaught exception. This allows your users to upload error log files, easing bug fixes. It also allows you to quit the game immediately preventing repeated exception spam in the console.

The function should be run as soon as possible. It only needs to be run once. It will stay active for the entire game.

Prerequisites

Magic Methods

Code

def errorLogIni():
  # https://upbge-docs.readthedocs.io/en/latest/api/bge.logic.html
  import bge
 
  # https://docs.python.org/3/library/logging.html
  import logging
 
  # https://docs.python.org/3/library/sys.html
  import sys
 
  # https://docs.python.org/3/library/datetime.html#datetime.datetime.now
  from datetime import datetime
 
  # The name of the file to save the error to.  In this case //system/error.log
  filename = bge.logic.expandPath('//system/')+'error.log'
 
  # (A) logging.basicConfig
  logging.basicConfig(filename=filename, filemode='w', level=logging.DEBUG)
 
  # We define the function first, then override sys.excepthook with it afterward.
  def exceptHook(type, value, traceback):
 
    # Places the date into the log file.
    logging.info(datetime.now())
    # Places the exception in the log file.
    # exc_info was probably taken from Stack Exchange, I don't remember.  Just use it verbatim.
    logging.exception('Uncaught exception', exc_info=(type, value, traceback))
 
    # (B) sys.__excepthook__
    sys.__excepthook__(type, value, traceback)
 
    # End the game immediately, preventing console spam.
    bge.logic.endGame()
 
  # This overrides the default exception hook.  That means our new function is run instead of the default one.
  sys.excepthook = exceptHook
 
  # Logging is a singleton so theres no reason to return it, but why not?
  return logging

Notes

(A) Logging
This sets the Logging object. Any time something is added to the log, it is added to the file specified by filename. The Logging object is a singleton. This means that every time you import it, the same Logging object is imported. This allows you to log to the same file from multiple scripts. The Logging object will last as long as the game is run.
filemode='w' means the file can only be written to.
https://docs.python.org/3/library/functions.html#filemodes
If a Logging object is passed a message with a level that is lower then its logging level, then that message is ignored. If your not sure, then set it low.
https://docs.python.org/3/library/logging.html#logging.Logger.setLevel

(B) sys.__excepthook__
When ever an exception is created, sys.__excepthook__ is run. We want to do extra stuff in addition to the normal exception. Thus we need to run sys.__excepthook__ manually so that a normal exception is still created.

Questions

Edit this page to ask or answer questions.

Further Reading

overriding
callbacks
hooks
singleton

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License