import logging
import sys
import os
import time
from logging.handlers import RotatingFileHandler
import io
def get_logger(tag):
# 创建一个新的输出流,并指定编码为UTF-8
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')
accPath = 'logs'
if not os.path.exists(accPath):
os.makedirs(accPath, 0o644)
logger = logging.getLogger(tag)
logger.setLevel(logging.DEBUG)
fh = RotatingFileHandler(accPath + os.sep + tag + '.log', maxBytes=1024*1024*100, backupCount=30, encoding='utf-8')
fh.setLevel(logging.DEBUG)
ch = logging.StreamHandler(sys.stdout)
ch.setLevel(logging.DEBUG)
formatter = logging.Formatter("%(asctime)s - %(thread)d - %(levelname)s - %(message)s")
ch.setFormatter(formatter)
fh.setFormatter(formatter)
logger.addHandler(ch)
logger.addHandler(fh)
return logger
# 测试日志输出
gl = get_logger('test')
gl.info("中文字符测试")
# 测试控制台输出
print("中文字符测试")
```
10-20
523