封装记录log日志,多模块使用
# coding=utf-8
import logging
import time
import os
log_path = './log'
class Log:
def __init__(self):
self.now = time.strftime("%Y-%m-%d--%H-%M-%S")
self.logname = os.path.join(log_path, '{0}.log'.format(self.now))
def __printconsole(self, level, message):
# 创建一个logger
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
# 创建一个handler,用于写入日志文件
fh = logging.FileHandler(self.logname, 'a', encoding='utf-8')
fh.setLevel(logging.DEBUG)
# 再创建一个handler,用于输出到控制台
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
# 定义handler的输出格式
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
fh.setFormatter(formatter)
ch.setFormatter(formatt