Welcome to the Pycom forum Forum
    • \[\[global:header.categories\]\]
    • \[\[global:header.recent\]\]
    • \[\[global:header.tags\]\]
    • \[\[global:header.popular\]\]
    • Login

    Explore Pybytes | Official Documentation | Report a Firmware Bug/Issue | GitHub

    Capturing console messages to a log

    Scheduled Pinned Locked Moved MicroPython
    console
    5 Posts 4 Posters 1.7k Views 5 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • B Offline
      BetterAuto Pybytes Beta
      last edited by BetterAuto

      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.

      1 Reply Last reply Reply Quote 0
      • F Offline
        fsergeys
        last edited by

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

        S 1 Reply Last reply Reply Quote 0
        • B Offline
          BetterAuto Pybytes Beta
          last edited by

          Afraid not.

          1 Reply Last reply Reply Quote 0
          • sebS Offline
            seb
            last edited by

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

            1 Reply Last reply Reply Quote 0
            • S Offline
              svonbentzel @fsergeys
              last edited by

              @fsergeys same problem here :-(

              1 Reply Last reply Reply Quote 0

              Hello! It looks like you're interested in this conversation, but you don't have an account yet.

              Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

              With your input, this post could be even better 💗

              Register Login
              • 1 / 1
              • First post
                Last post

              Pycom on Twitter

              Pycom