feat: 新增运行时重载配置文件;新增根据不同环境(dev;prod)显示不同级别的log
This commit is contained in:
38
bot.py
38
bot.py
@@ -51,15 +51,15 @@ def init_env():
|
||||
with open(".env", "w") as f:
|
||||
f.write("ENVIRONMENT=prod")
|
||||
|
||||
# 检测.env.prod文件是否存在
|
||||
if not os.path.exists(".env.prod"):
|
||||
logger.error("检测到.env.prod文件不存在")
|
||||
shutil.copy("template.env", "./.env.prod")
|
||||
# 检测.env.prod文件是否存在
|
||||
if not os.path.exists(".env.prod"):
|
||||
logger.error("检测到.env.prod文件不存在")
|
||||
shutil.copy("template.env", "./.env.prod")
|
||||
|
||||
# 检测.env.dev文件是否存在,不存在的话直接复制生产环境配置
|
||||
if not os.path.exists(".env.dev"):
|
||||
logger.error("检测到.env.dev文件不存在")
|
||||
shutil.copy("template.env", "./.env.dev")
|
||||
shutil.copy(".env.prod", "./.env.dev")
|
||||
|
||||
# 首先加载基础环境变量.env
|
||||
if os.path.exists(".env"):
|
||||
@@ -99,15 +99,25 @@ def load_env():
|
||||
|
||||
def load_logger():
|
||||
logger.remove() # 移除默认配置
|
||||
logger.add(
|
||||
sys.stderr,
|
||||
format="<green>{time:YYYY-MM-DD HH:mm:ss.SSS}</green> <fg #777777>|</> <level>{level: <7}</level> <fg "
|
||||
"#777777>|</> <cyan>{name:.<8}</cyan>:<cyan>{function:.<8}</cyan>:<cyan>{line: >4}</cyan> <fg "
|
||||
"#777777>-</> <level>{message}</level>",
|
||||
colorize=True,
|
||||
level=os.getenv("LOG_LEVEL", "INFO"), # 根据环境设置日志级别,默认为INFO
|
||||
filter=lambda record: "nonebot" not in record["name"]
|
||||
)
|
||||
if os.getenv("ENVIRONMENT") == "dev":
|
||||
logger.add(
|
||||
sys.stderr,
|
||||
format="<green>{time:YYYY-MM-DD HH:mm:ss.SSS}</green> <fg #777777>|</> <level>{level: <7}</level> <fg "
|
||||
"#777777>|</> <cyan>{name:.<8}</cyan>:<cyan>{function:.<8}</cyan>:<cyan>{line: >4}</cyan> <fg "
|
||||
"#777777>-</> <level>{message}</level>",
|
||||
colorize=True,
|
||||
level=os.getenv("LOG_LEVEL", "DEBUG"), # 根据环境设置日志级别,默认为DEBUG
|
||||
)
|
||||
else:
|
||||
logger.add(
|
||||
sys.stderr,
|
||||
format="<green>{time:YYYY-MM-DD HH:mm:ss.SSS}</green> <fg #777777>|</> <level>{level: <7}</level> <fg "
|
||||
"#777777>|</> <cyan>{name:.<8}</cyan>:<cyan>{function:.<8}</cyan>:<cyan>{line: >4}</cyan> <fg "
|
||||
"#777777>-</> <level>{message}</level>",
|
||||
colorize=True,
|
||||
level=os.getenv("LOG_LEVEL", "INFO"), # 根据环境设置日志级别,默认为INFO
|
||||
filter=lambda record: "nonebot" not in record["name"]
|
||||
)
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user