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()
        dupterm(console)
        while True:
            logH = open('/flash/console.log', 'w+')
            logH.write(console.getvalue())
            logH.close()
            time.sleep(1)
    
    start_new_thread(log_console, ())
    

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


Log in to reply
 

Looks like your connection to Pycom Forum was lost, please wait while we try to reconnect.