redirect prints to log file

前端 未结 8 1397
萌比男神i
萌比男神i 2020-12-02 09:05

Okay. I have completed my first python program.It has around 1000 lines of code. During development I placed plenty of print statements before running a command

相关标签:
8条回答
  • 2020-12-02 09:45

    Putting your own file-like in sys.stdout will let you capture the text output via print.

    0 讨论(0)
  • 2020-12-02 09:46

    Just a note about append vs write mode. Change filemode to "w" if you would like it to replace log file. I also had to comment out the stream. Then using logging.info() was outputting to file specified.

    if __name__ == '__main__':
        LOG_FORMAT = '%(asctime)s:%(levelname)s ==> %(message)s'
        logging.basicConfig(
            level=logging.INFO,
            filename="logfile",
            filemode="w",
            format=LOG_FORMAT
            #stream=sys.stdout
        )
    
    0 讨论(0)
  • 2020-12-02 09:47

    A simple way to redirect stdout and stderr using the logging module is here: How do I duplicate sys.stdout to a log file in python?

    0 讨论(0)
  • 2020-12-02 09:48
    • Next time, you'll be happier if instead of using print statements at all you use the logging module from the start. It provides the control you want and you can have it write to stdout while that's still where you want it.

    • Many people here have suggested redirecting stdout. This is an ugly solution. It mutates a global and—what's worse—it mutates it for this one module's use. I would sooner make a regex that changes all print foo to print >>my_file, foo and set my_file to either stdout or an actual file of my choosing.

      • If you have any other parts of the application that actually depend on writing to stdout (or ever will in the future but you don't know it yet), this breaks them. Even if you don't, it makes reading your module look like it does one thing when it actually does another if you missed one little line up top.
      • Chevron print is pretty ugly, but not nearly as ugly as temporarily changing sys.stdout for the process.
      • Very technically speaking, a regex replacement isn't capable of doing this right (for example, it could make false positives if you are inside of a multiline string literal). However, it's apt to work, just keep an eye on it.
    • os.system is virtually always inferior to using the subprocess module. The latter needn't invoke the shell, doesn't pass signals in a way that usually is unwanted, and can be used in a non-blocking manner.

    0 讨论(0)
  • 2020-12-02 09:50

    You can create a log file and prepare it for writing. Then create a function:

    def write_log(*args):
        line = ' '.join([str(a) for a in args])
        log_file.write(line+'\n')
        print(line)
    

    and then replace your print() function name with write_log()

    0 讨论(0)
  • 2020-12-02 09:51

    Python lets you capture and assign sys.stdout - as mentioned - to do this:

    import sys
    old_stdout = sys.stdout
    
    log_file = open("message.log","w")
    
    sys.stdout = log_file
    
    print "this will be written to message.log"
    
    sys.stdout = old_stdout
    
    log_file.close()
    
    0 讨论(0)
提交回复
热议问题