python将print输出的信息保留到日志文件中

print与stdout说明参见:
 
import sys
import os
import io
import datetime


def create_detail_day():
    '''
    :return:
    '''
    # 年-月-日
    # daytime = datetime.datetime.now().strftime('day'+'%Y-%m-%d')
    # 年_月_日
    daytime = datetime.datetime.now().strftime('day'+'%Y_%m_%d')
    # 时:分:秒
    # hourtime = datetime.datetime.now().strftime("%H:%M:%S")
    # hourtime = datetime.datetime.now().strftime('time' + "%H_%M_%S")
    detail_time = daytime
    # print(daytime + "-" + hourtime)
    # detail_time = daytime + "__" + hourtime
    return detail_time


class Logger(object):
    def __init__(self, filename="Default.log", path="./"):
        sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')
        self.terminal = sys.stdout
        self.log = open(os.path.join(path, filename), "a", encoding='utf8')

    def write(self, message):
        self.terminal.write(message)
        self.log.write(message)

    def flush(self):
        pass


if __name__ == '__main__':
    sys.stdout = Logger(create_detail_day() + '.log', path='')
    print(create_detail_day().center(60, '*'))
    print('explanation'.center(80, '*'))
    info1 = '从大到小排序'
    info2 = ' sort the form large to small'
    print(info1)
    print(info2)
    print('END: explanation'.center(80, '*'))
posted @ 2020-03-11 16:22  cloudolt  阅读(2550)  评论(0编辑  收藏  举报