mirror of
https://github.com/PaiGramTeam/PamGram.git
synced 2024-11-21 21:58:04 +00:00
059bcd5e70
* 🔧 使用 dotenv 重构 config
默认配置从 config.json 移动到 config.py 中。如果要覆盖默认配置,在根目录创建
.env 文件按照 .env.example 的例子编辑。
这个方案的优点是:
* 支持写注释
* 以后如果新增配置项,如果用默认值就可以,不需要修改 .env 文件
* 如果通过 serverless、docker 或者 k8s 部署,方便不用修改文件,直接注入环境变量
修改配置
62 lines
1.8 KiB
Python
62 lines
1.8 KiB
Python
import logging
|
|
import os
|
|
from logging.handlers import RotatingFileHandler
|
|
|
|
import colorlog
|
|
|
|
from config import config
|
|
|
|
current_path = os.path.realpath(os.getcwd())
|
|
log_path = os.path.join(current_path, "logs")
|
|
if not os.path.exists(log_path):
|
|
os.mkdir(log_path)
|
|
log_file_name = os.path.join(log_path, "log.log")
|
|
|
|
log_colors_config = {
|
|
"DEBUG": "cyan",
|
|
"INFO": "green",
|
|
"WARNING": "yellow",
|
|
"ERROR": "red",
|
|
"CRITICAL": "red",
|
|
}
|
|
|
|
|
|
class Logger:
|
|
def __init__(self):
|
|
self.logger = logging.getLogger("TGPaimonBot")
|
|
root_logger = logging.getLogger()
|
|
root_logger.setLevel(logging.CRITICAL)
|
|
if config.debug:
|
|
self.logger.setLevel(logging.DEBUG)
|
|
else:
|
|
self.logger.setLevel(logging.INFO)
|
|
self.formatter = colorlog.ColoredFormatter(
|
|
"%(log_color)s[%(asctime)s] [%(levelname)s] - %(message)s", log_colors=log_colors_config)
|
|
self.formatter2 = logging.Formatter("[%(asctime)s] [%(levelname)s] - %(message)s")
|
|
fh = RotatingFileHandler(filename=log_file_name, maxBytes=1024 * 1024 * 5, backupCount=5,
|
|
encoding="utf-8")
|
|
fh.setFormatter(self.formatter2)
|
|
root_logger.addHandler(fh)
|
|
|
|
ch = colorlog.StreamHandler()
|
|
ch.setFormatter(self.formatter)
|
|
root_logger.addHandler(ch)
|
|
|
|
def getLogger(self):
|
|
return self.logger
|
|
|
|
def debug(self, msg, exc_info=None):
|
|
self.logger.debug(msg=msg, exc_info=exc_info)
|
|
|
|
def info(self, msg, exc_info=None):
|
|
self.logger.info(msg=msg, exc_info=exc_info)
|
|
|
|
def warning(self, msg, exc_info=None):
|
|
self.logger.warning(msg=msg, exc_info=exc_info)
|
|
|
|
def error(self, msg, exc_info=None):
|
|
self.logger.error(msg=msg, exc_info=exc_info)
|
|
|
|
|
|
Log = Logger()
|