Capturing console messages to a log

  • Pybytes Beta

    I want to capture all console output such as debug messages and uncaught exceptions to a log file. I created the following code but it's not working. It does grab the console as expected. Also, if I comment out dupterm I can console.write("Some string") and it gets added to the log. But if I raise an exception it just quits the script without adding anything to the log. Please assist.

    import time
    from os import dupterm
    from uio import StringIO
    from _thread import start_new_thread
    def log_console():
        console = StringIO()
        while True:
            logH = open('/flash/console.log', 'w+')
    start_new_thread(log_console, ())

    Edit: Oops, this question is almost a duplicate. Forgot that I had asked.

  • Check out sys.print_exception, that might be able to help with printing out exceptions and traceback

  • Pybytes Beta

    Afraid not.

  • @BetterAuto: Did you find a working solution? I'm hitting the same problem.

Log in to reply

Pycom on Twitter