Skip to content

Commit

Permalink
完善日志系统,增加并完善一个api (#18)
Browse files Browse the repository at this point in the history
完善日志系统,增加并完善一个api
  • Loading branch information
northgreen authored Jan 30, 2024
1 parent 2fa933a commit 2c4fa68
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 48 deletions.
79 changes: 40 additions & 39 deletions docs/doc/None.md
Original file line number Diff line number Diff line change
@@ -1,47 +1,48 @@
# 想要实现但是没有实现的功能
`pluginsystem.py`

```python
# TODO:修不起这个bug了,谁爱修谁修。。。。。。。。。。。。。。。

for plugin_file in confi["plugins"]["others_plugin"]:
plugin_name = plugin_file
try:
if plugin_file is not None:
mlogger.info(f"loading plugin{plugin_file}")
path = os.path.normpath(plugin_file)
if os.path.isdir(plugin_name):
spec = importlib.util.spec_from_file_location(os.path.dirname(path), "__init__.py")
else:
spec = importlib.util.spec_from_file_location(os.path.dirname(path), os.path.basename(path))
if spec is None:
mlogger.error(f"no modle on {path}")
continue
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
# 检查是否实现了主方法
if not hasattr(module, "Plugin_Main"):
raise plugin_errors.NoMainMather("函数未实现主方法或者主方法名称错误")
plugin_main_class: plugin_main.Plugin_Main = module.Plugin_Main

# 获取插件类型
if plugin_main_class.plugin_type() == "message":
self.message_plugin_list.append(plugin_main_class)
elif plugin_main_class.plugin_type() == "analyzer":
self.analyzer_plugin_list.append(plugin_main_class)
else:
raise plugin_errors.PluginTypeError("未知的插件类型,该不会是插件吃了金克拉了吧?")
for plugin_file in confi["plugins"]["others_plugin"]:
plugin_name = plugin_file
try:
if plugin_file is not None:
mlogger.info(f"loading plugin{plugin_file}")
path = os.path.normpath(plugin_file)
if os.path.isdir(plugin_name):
spec = importlib.util.spec_from_file_location(os.path.dirname(path), "__init__.py")
else:
spec = importlib.util.spec_from_file_location(os.path.dirname(path), os.path.basename(path))
if spec is None:
mlogger.error(f"no modle on {path}")
continue
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
# 检查是否实现了主方法
if not hasattr(module, "Plugin_Main"):
raise plugin_errors.NoMainMather("函数未实现主方法或者主方法名称错误")
plugin_main_class: plugin_main.Plugin_Main = module.Plugin_Main

# 获取插件类型
if plugin_main_class.plugin_type() == "message":
self.message_plugin_list.append(plugin_main_class)
elif plugin_main_class.plugin_type() == "analyzer":
self.analyzer_plugin_list.append(plugin_main_class)
else:
raise plugin_errors.PluginTypeError("未知的插件类型,该不会是插件吃了金克拉了吧?")

# 注册插件cgi
if plugin_main_class.sprit_cgi_support:
if plugin_main_class.sprit_cgi_path:
self.plugin_cgi_support[plugin_main_class.sprit_cgi_path] = plugin_main_class.cgi_face
else:
raise

# 注册插件cgi
if plugin_main_class.sprit_cgi_support:
if plugin_main_class.sprit_cgi_path:
self.plugin_cgi_support[plugin_main_class.sprit_cgi_path] = plugin_main_class.sprit_cgi
else:
raise
# 注册js脚本
if plugin_main_class.plugin_js_sprit_support:
self.plugin_js_support[plugin_main_class.plugin_name] = plugin_main_class.plugin_js_sprit
except ImportError as e:
mlogger.error(f"failed to import plugin {plugin_name:{str(e)}}")

# 注册js脚本
if plugin_main_class.plugin_js_sprit_support:
self.plugin_js_support[plugin_main_class.plugin_name] = plugin_main_class.plugin_js_sprit
except ImportError as e:
mlogger.error(f"failed to import plugin {plugin_name:{str(e)}}")

```
2 changes: 1 addition & 1 deletion ictye-live-dm/config/system/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@ loglevel: INFO
#日志文件
logfile:
open: 1
name: "latest-log"
name: "latestlog"
29 changes: 28 additions & 1 deletion ictye-live-dm/depends/logger.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import logging
import datetime
import os


def setup_logging(config: dict):
Expand All @@ -12,4 +14,29 @@ def setup_logging(config: dict):
"ERROR": logging.ERROR,
"CRITICAL": logging.CRITICAL,
"FATAL": logging.FATAL}
logging.basicConfig(level=level_dic[config["loglevel"]], format="[%(asctime)s,%(name)s] %(levelname)s : %(message)s")

logger = logging.getLogger() # 获取全局logger
logger.setLevel(level_dic[config["loglevel"]]) # 设置日志级别

# 创建一个handler,用于写入日志文件
if not os.path.exists('logs'):
os.makedirs('logs')

fh = logging.FileHandler(
os.path.join('logs', config["logfile"]["name"] + datetime.time().strftime("%Y%m%d_%H%M%S") + ".log"),
encoding="utf-8")
fh.setLevel(level_dic[config["loglevel"]])

# 创建一个handler,用于将日志输出到控制台
ch = logging.StreamHandler()
ch.setLevel(level_dic[config["loglevel"]])

# 定义handler的输出格式
formatter = logging.Formatter("[%(asctime)s,%(name)s] %(levelname)s : %(message)s")
fh.setFormatter(formatter)
ch.setFormatter(formatter)

# 给logger添加handler
if config["logfile"]["open"]:
logger.addHandler(fh)
logger.addHandler(ch)
1 change: 0 additions & 1 deletion ictye-live-dm/depends/msgs.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
这个文件内定义全部的标准消息模板
所有的函数都应该又to_dict方法来将其转为字典,相反的这个函数能输出打包好的字典
别问我为啥,问就是不知道
"""


Expand Down
2 changes: 1 addition & 1 deletion ictye-live-dm/livewebsocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ async def websockets(websocket: server.WebSocketServerProtocol):
finally:
# 后续的处理
await websocket.close()
plugin_system.remove_connect_in_id_dict(websocket.id)
await plugin_system.remove_connect_in_id_dict(websocket.id)


async def websocket_main(configs):
Expand Down
Binary file removed ictye-live-dm/plugin/bilibili_dm_plugin.zip
Binary file not shown.
Binary file removed ictye-live-dm/plugin/test.zip
Binary file not shown.
5 changes: 4 additions & 1 deletion ictye-live-dm/pluginsystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,14 @@ def __init__(self):
except IndexError as e:
mlogger.error(f"failed to import plugin :\n{plugin_name} {str(e)}")

def remove_connect_in_id_dict(self, id):
async def remove_connect_in_id_dict(self, id):
"""
当连接关闭时,移除连接
:param id 连接id
"""
for i in self.connect_id_dict[id]:
if hasattr(i, "callback"):
await i.callback()
return self.connect_id_dict.pop(id, False)

async def get_plugin_message(self, params, connect: WebSocketServerProtocol):
Expand Down
8 changes: 4 additions & 4 deletions ictye-live-dm/web/style/default_style.css
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

html {
background-image: url(data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZlcnNpb249IjEuMSI+CiAgPGxpbmUgeDE9IjM1JSIgeDI9IjM1JSIgeTE9IjAlIiB5Mj0iMTAwJSIgc3Ryb2tlPSJyZ2IoMjE3LDIxNywyMTcpIiBzdHJva2Utd2lkdGg9IjVweCIgLz4KCjwvc3ZnPg==);
background-image: url(data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZlcnNpb249IjEuMSI+CiAgPGxpbmUgeDE9IjQwJSIgeDI9IjQwJSIgeTE9IjAlIiB5Mj0iMTAwJSIgc3Ryb2tlPSJyZ2IoMjE3LDIxNywyMTcpIiBzdHJva2Utd2lkdGg9IjVweCIgLz4KPC9zdmc+);
background-repeat: no-repeat;
width: 100%;
height: 100%;
Expand Down Expand Up @@ -54,12 +54,12 @@ body {
.dmlaft {
/*margin-top: 13px;*/
display: flex;
width: 32%;
width: 38%;
flex-wrap: wrap;
}
.pname{
margin-left: 12px;
width: 50%;
width: 55%;
font-size: 24px;
flex-grow:1;
display: flex;
Expand All @@ -76,7 +76,7 @@ body {
.dmright{
margin-left: 5%;
font-size: 24px;
width: 58%;
width: 48%;
flex-grow: 1;
word-break: break-all;
}
Expand Down

0 comments on commit 2c4fa68

Please sign in to comment.