1

I have a python script which consists of several threads running and they have print outs to tell any connection failure, e.g. failed to open ports, failed to connect etc.

I've tried the basic:

import sys
sys.stdout = open('file', 'w')

but nothing was saved into the file. I'm trying to get the output of the system print outs and save it into tinydb. I am trying to log all of the outputs into tinydb. How can I do this?

Is it possible to just retrieve all of the print outputs continuously and save it into a json file using tinydb?

Kim SuYu
  • 69
  • 1
  • 11

1 Answers1

0

In python3, you have to use print with a file argument if you want to override stdout as the documentation says and as documented in this question:

Python 2.7: Print to File

This gives you the basic building block for achieve what you're looking for. You're not really explaining anything on how you expect to use it with tinydb...

That said, your print call should look like this:

fake_stdout = open('file', 'w')
print(data, file=fake_stdout)

That's it.

If for some reasons you have a lot of print statement to change, you could do something like this:

Have a module with a thread local variable that get initialized. Here look at this question: Thread local storage in Python

Then when you have your module set up you can add this statement in all your files that needs a different print stdout

from special_print import local_print as print

All you have to do is to define a threadlocal variable in your special_print module and then define it when a new thread starts with something like this:

def print_factory(fout):
    def local_print(arg):
        print(arg, file=fout)
    return local_print

In each new thread you run something like this:

import special_print
special_print.local_print = print_factory(fake_stdout)

Then when you import this print method in each of your threads, they'll correctly output the data to the fake_stdout you defined in each thread.

Loïc Faure-Lacroix
  • 13,220
  • 6
  • 67
  • 99