Python3 print with timestamp

import sys
import datetime

old_f = sys.stdout
class F:
    def write(self, x):
        old_f.write(x.replace("\n", "    [%s]\n" % str(datetime.datetime.now())))
    def flush(self):
        pass
sys.stdout = F()


print('Hello World')

結果:

Hello World [2021-05-11 22:57:49.179915]