An example of Python log real time writing without delay

  • 2021-07-16 02:47:35
  • OfStack

When I used python to generate logs, I found that no matter how flush (), the file contents could not be written in real time, which led to the unexpected interruption of the program.

The following are the solutions found (pro-test is feasible):


open  Function has 1 A bufferin The default is -1 If set to 0 Yes, that is, unbuffered mode.  
 But with 2 Open this file in binary mode and convert the information to be written byte -like As follows. 
 
with open("test.txt",'wb',buffering=0) as f:
#wb Is write mode plus 2 Binary mode 
  f.write(b"hello!") Prefix the string with b , converted to 2 Binary system 
 
 If it doesn't work 2 You will be prompted to open the file in binary ValueEorror : 
 
 Didn't convert the string to 2 The binary system will prompt: TypeError: a bytes-like object is required, not  ' str'

Test:


class Logger(object):
  def __init__(self, log_path="default.log"):
    self.terminal = sys.stdout
    # self.log = open(log_path, "w+")
    self.log = open(log_path, "wb", buffering=0)
 
  def print(self, message):
    self.terminal.write(message + "\n")
    self.log.write(message.encode('utf-8') + b"\n")
 
  def flush(self):
    self.terminal.flush()
    self.log.flush()
 
  def close(self):
    self.log.close()

Error 1: TypeError: can 't concat str to bytes

Error 2: write requires str object, unable to write bytes object (general idea)

This is because:

(1) log. write requires writing to an bytes object, which is fine here. However, encode returns bytes data, which cannot be added with str. b needs to be added before '\ n'.

(2) terminal. write function parameters need to be of str type and be converted to str.

Replace with the following:


  def print(self, message):
    self.terminal.write(message + "\n")
    self.log.write(message.encode('utf-8') + b"\n")

Run successfully!


Related articles: