算法日志:python把终端的信息存为log和logging

2021-01-14 11:24:27 浏览数 (2)

python 把终端的信息存为log

代码语言:javascript复制
import sys
import time
class Logger(object):
    def __init__(self, filename='default.log', stream=sys.stdout):
	    self.terminal = stream
	    self.log = open(filename, 'a')

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

    def flush(self):
	    pass

sys.stdout = Logger('a.log', sys.stdout)
sys.stderr = Logger('a.log_file', sys.stderr)  # redirect std err, if necessary

string_info = "hello world"
print("{} -- {} -- {}".format(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()),"Text_similarity -- Run INFO ",string_info))

运用logging

代码语言:javascript复制
import logging

logger = logging.getLogger('songs generations server')
logger.setLevel(logging.INFO)
handler = logging.FileHandler(filename="./songs_maker.log")
hand

0 人点赞