oscarpilote / Ortho4XP

A scenery generator for the X-Plane flight simulator

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

TECH DEBT | Follow pythons best practices for logging and error/exception handling.

anthraxn8b opened this issue · comments

commented
  • replace all "print" commands and equal practices
  • do not just catch exceptions, log them correctly including the stacktrace

By these two measures it will be much easier to find bugs in the future.

commented

What would be the ideal logging config for this project?

Most of the current logging and verbosity control is in src\O4_UI_Utils.py. The approach uses min_verbosity for deciding what to print and logs directly to a file within logprint.

def vprint(min_verbosity, *args):
    if verbosity >= min_verbosity:
        print(*args)

def logprint(*args):
    try:
        f = open(os.path.join(Ortho4XP_dir, "Ortho4XP.log"), "a")
        f.write(
            time.strftime("%c")
            + " | "
            + " ".join([str(x) for x in args])
            + "\n"
        )
        f.close()
    except:
        pass

We could start by wrapping the existing print and manual file logging statements in vprint, logprint, and lvprint with a proper logger setup.

Somethig that looks like this

setup_logging()
logger = get_logger(__name__)

def logprint(*args):
    message = " | ".join(str(arg) for arg in args) # not  really required 
    logger.info(message)

Moving away from numerical to logging levels (DEBUG, INFO, etc.) could make the application more intuitive. Now, even with descriptions, it can be confusing for end users.