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