import logging
import logging.handlers
import os
import sys
logger = logging.getLogger()
def initLogger(name, std_level=logging.INFO, file_level=logging.DEBUG):
log_dir, _ = os.path.split(os.path.abspath(sys.argv[0]))
log_filename = log_dir + ‘/log/’ + name + ‘.log’
if not os.path.isdir(log_dir + “/log”):
os.mkdir(log_dir + “/log”)
#日志文件
# file_handler = logging.FileHandler(log_filename)
file_handler = logging.handlers.RotatingFileHandler(log_filename,
mode=‘a’,
maxBytes=102400,
backupCount=100,
encoding=“utf8”
)
fmt = logging.Formatter(’%(asctime)s %(levelname)s %(message)s’)
file_handler.setFormatter(fmt)
file_handler.setLevel(file_level)
logger.addHandler(file_handler)
#屏幕输出
stdout_handler = logging.StreamHandler(sys.stdout)
stdout_handler.setFormatter(fmt)
stdout_handler.setLevel(std_level)
logger.addHandler(stdout_handler)
logger.setLevel(file_level)
initLogger(“test”, logging.INFO, logging.INFO)
logger.info(“找不到 CONTACT SUPPLIER 按键,退出…”)