How to output to the console and file?

前端 未结 8 1153
青春惊慌失措
青春惊慌失措 2020-11-30 02:54

I\'m trying to find out a way in python to redirect the script execution log to a file as well as stdout in a pythonic way. Is there any easy way of achieving t

相关标签:
8条回答
  • 2020-11-30 03:25

    I just want to build upon Serpens answer and add the line:

    logger.setLevel('DEBUG')
    

    This will allow you to chose what level of message gets logged.

    For example in Serpens example,

    logger.info('Info message')
    

    Will not get recorded as it defaults to only recording Warnings and above.

    More about levels used can be read about here

    0 讨论(0)
  • 2020-11-30 03:30

    I came up with this [untested]

    import sys
    
    class Tee(object):
        def __init__(self, *files):
            self.files = files
        def write(self, obj):
            for f in self.files:
                f.write(obj)
                f.flush() # If you want the output to be visible immediately
        def flush(self) :
            for f in self.files:
                f.flush()
    
    f = open('out.txt', 'w')
    original = sys.stdout
    sys.stdout = Tee(sys.stdout, f)
    print "test"  # This will go to stdout and the file out.txt
    
    #use the original
    sys.stdout = original
    print "This won't appear on file"  # Only on stdout
    f.close()
    

    print>>xyz in python will expect a write() function in xyz. You could use your own custom object which has this. Or else, you could also have sys.stdout refer to your object, in which case it will be tee-ed even without >>xyz.

    0 讨论(0)
  • 2020-11-30 03:30

    Probably the shortest solution:

    def printLog(*args, **kwargs):
        print(*args, **kwargs)
        with open('output.out','a') as file:
            print(*args, **kwargs, file=file)
    
    printLog('hello world')
    

    Writes 'hello world' to sys.stdout and to output.out and works exactly the same way as print().

    Note: Please do not specify the file argument for the printLog function. Calls like printLog('test',file='output2.out') are not supported.

    0 讨论(0)
  • 2020-11-30 03:33

    Use logging module (http://docs.python.org/library/logging.html):

    import logging
    
    logger = logging.getLogger('scope.name')
    
    file_log_handler = logging.FileHandler('logfile.log')
    logger.addHandler(file_log_handler)
    
    stderr_log_handler = logging.StreamHandler()
    logger.addHandler(stderr_log_handler)
    
    # nice output format
    formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
    file_log_handler.setFormatter(formatter)
    stderr_log_handler.setFormatter(formatter)
    
    logger.info('Info message')
    logger.error('Error message')
    
    0 讨论(0)
  • 2020-11-30 03:33

    The easiest solution is to redirect the standard output. In your python program file use the following:

    if __name__ == "__main__":
       sys.stdout = open('file.log', 'w')
       #sys.stdout = open('/dev/null', 'w')
       main()
    

    Any std output (e.g. the output of print 'hi there') will be redirected to file.log or if you uncomment the second line, any output will just be suppressed.

    0 讨论(0)
  • 2020-11-30 03:36

    Create an output file and custom function:

    outputFile = open('outputfile.log', 'w')
    
    def printing(text):
        print(text)
        if outputFile:
            outputFile.write(str(text))
    

    Then instead of print(text) in your code, call printing function.

    printing("START")
    printing(datetime.datetime.now())
    printing("COMPLETE")
    printing(datetime.datetime.now())
    
    0 讨论(0)
提交回复
热议问题