mirror of
https://github.com/Nanaloveyuki/py-logiliteal.git
synced 2025-10-18 16:16:23 +00:00
Compare commits
5 Commits
80e4a4d02f
...
71d5dcaff3
Author | SHA1 | Date | |
---|---|---|---|
|
71d5dcaff3 | ||
|
635fc846f8 | ||
|
b1918d1948 | ||
|
da2231875f | ||
|
d1422513e9 |
@@ -67,11 +67,12 @@ pip install logiliteal
|
|||||||
## 示例
|
## 示例
|
||||||
```python
|
```python
|
||||||
# 导入
|
# 导入
|
||||||
from logiliteal import Logger
|
from logiliteal import logger, Logger
|
||||||
# 或 import logiliteal(不推荐)
|
# 或 import logiliteal(不推荐)
|
||||||
|
|
||||||
# 实例化
|
# 实例化(不推荐: 会造成多实例问题)
|
||||||
logger = Logger()
|
# 推荐直接使用 logger 而不是 Logger()
|
||||||
|
# logger = Logger()
|
||||||
|
|
||||||
#使用功能
|
#使用功能
|
||||||
logger.info("这是一条信息日志")
|
logger.info("这是一条信息日志")
|
||||||
|
2
setup.py
2
setup.py
@@ -6,7 +6,7 @@ long_description = (here / "README.md").read_text(encoding="utf-8")
|
|||||||
|
|
||||||
setup(
|
setup(
|
||||||
name="logiliteal",
|
name="logiliteal",
|
||||||
version="0.1.2",
|
version="0.1.4",
|
||||||
description="简洁,高扩展性,可自定义的日志库 / Simple, high extensibility, and customizable logging library",
|
description="简洁,高扩展性,可自定义的日志库 / Simple, high extensibility, and customizable logging library",
|
||||||
long_description=long_description,
|
long_description=long_description,
|
||||||
long_description_content_type="text/markdown",
|
long_description_content_type="text/markdown",
|
||||||
|
@@ -11,6 +11,8 @@ from .utils import fmt_console, fmt_placeholder, fmt_message, fmt_level_name
|
|||||||
from .utils import set_style, set_color, set_bg_color
|
from .utils import set_style, set_color, set_bg_color
|
||||||
from .levels import Logger
|
from .levels import Logger
|
||||||
|
|
||||||
|
logger = Logger()
|
||||||
|
|
||||||
__all__ = [
|
__all__ = [
|
||||||
"get_config",
|
"get_config",
|
||||||
"set_config",
|
"set_config",
|
||||||
@@ -27,5 +29,6 @@ __all__ = [
|
|||||||
"set_color",
|
"set_color",
|
||||||
"set_bg_color",
|
"set_bg_color",
|
||||||
"create_backup",
|
"create_backup",
|
||||||
"Logger" # 日志记录器非实例化
|
"Logger", # 日志记录器非实例化
|
||||||
|
"logger" # 日志记录器实例化
|
||||||
]
|
]
|
@@ -12,6 +12,8 @@ from ..utils.configs import get_config, set_config
|
|||||||
from ..utils.time import get_asctime
|
from ..utils.time import get_asctime
|
||||||
import pathlib
|
import pathlib
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
import sys
|
||||||
|
import os
|
||||||
|
|
||||||
def _get_full_path(file_path, file_name):
|
def _get_full_path(file_path, file_name):
|
||||||
file_path.mkdir(parents=True, exist_ok=True)
|
file_path.mkdir(parents=True, exist_ok=True)
|
||||||
@@ -19,7 +21,18 @@ def _get_full_path(file_path, file_name):
|
|||||||
|
|
||||||
class Logger:
|
class Logger:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
project_root = Path(__file__).parent.parent.parent.parent
|
# 检测虚拟环境并确定项目根目录
|
||||||
|
python_path = sys.executable
|
||||||
|
project_root = Path.cwd()
|
||||||
|
|
||||||
|
# 如果在虚拟环境中,向上查找项目根目录
|
||||||
|
if 'venv' in python_path.lower():
|
||||||
|
# 分割路径并查找venv目录
|
||||||
|
path_parts = os.path.normpath(python_path).split(os.sep)
|
||||||
|
if 'venv' in path_parts:
|
||||||
|
venv_index = path_parts.index('venv')
|
||||||
|
project_root = Path(os.sep.join(path_parts[:venv_index]))
|
||||||
|
|
||||||
file_path = project_root / get_config("file_path")
|
file_path = project_root / get_config("file_path")
|
||||||
file_path.mkdir(parents=True, exist_ok=True)
|
file_path.mkdir(parents=True, exist_ok=True)
|
||||||
if file_path.exists():
|
if file_path.exists():
|
||||||
@@ -34,7 +47,18 @@ class Logger:
|
|||||||
self.debug("日志文件已存在,已自动重命名")
|
self.debug("日志文件已存在,已自动重命名")
|
||||||
|
|
||||||
def _log(self, msg, pf, lvn, no_file: bool = False, no_console: bool = False):
|
def _log(self, msg, pf, lvn, no_file: bool = False, no_console: bool = False):
|
||||||
project_root = Path(__file__).parent.parent.parent.parent
|
# 检测虚拟环境并确定项目根目录
|
||||||
|
python_path = sys.executable
|
||||||
|
project_root = Path.cwd()
|
||||||
|
|
||||||
|
# 如果在虚拟环境中,向上查找项目根目录
|
||||||
|
if 'venv' in python_path.lower():
|
||||||
|
# 分割路径并查找venv目录
|
||||||
|
path_parts = os.path.normpath(python_path).split(os.sep)
|
||||||
|
if 'venv' in path_parts:
|
||||||
|
venv_index = path_parts.index('venv')
|
||||||
|
project_root = Path(os.sep.join(path_parts[:venv_index]))
|
||||||
|
|
||||||
file_path = project_root / get_config("file_path")
|
file_path = project_root / get_config("file_path")
|
||||||
file_path.mkdir(parents=True, exist_ok=True)
|
file_path.mkdir(parents=True, exist_ok=True)
|
||||||
file_name = get_config("file_name")
|
file_name = get_config("file_name")
|
||||||
|
37
tests/t-multithread.py
Normal file
37
tests/t-multithread.py
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
import sys
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
project_root = Path(__file__).parent.parent
|
||||||
|
sys.path.append(str(project_root))
|
||||||
|
|
||||||
|
from src.logiliteal import Logger
|
||||||
|
|
||||||
|
log = Logger()
|
||||||
|
|
||||||
|
import threading
|
||||||
|
import time
|
||||||
|
import random
|
||||||
|
|
||||||
|
# 定义线程测试函数
|
||||||
|
def thread_logger(thread_id: int):
|
||||||
|
"""
|
||||||
|
线程日志测试函数
|
||||||
|
Thread logger test function
|
||||||
|
Args:
|
||||||
|
thread_id (int): 线程ID Thread ID
|
||||||
|
"""
|
||||||
|
for i in range(10):
|
||||||
|
log.info(f"线程{thread_id}的第{i+1}条日志消息", no_file=True)
|
||||||
|
time.sleep(random.uniform(0.01, 0.05))
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
threads = []
|
||||||
|
for i in range(10):
|
||||||
|
thread = threading.Thread(target=thread_logger, args=(i,))
|
||||||
|
threads.append(thread)
|
||||||
|
thread.start()
|
||||||
|
|
||||||
|
for thread in threads:
|
||||||
|
thread.join()
|
||||||
|
|
||||||
|
log.info("所有线程日志记录完成", no_file=True)
|
15
tests/t-repeat.py
Normal file
15
tests/t-repeat.py
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
import sys
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
project_root = Path(__file__).parent.parent
|
||||||
|
sys.path.append(str(project_root))
|
||||||
|
|
||||||
|
from src.logiliteal import Logger
|
||||||
|
|
||||||
|
log = Logger()
|
||||||
|
|
||||||
|
i = 0
|
||||||
|
|
||||||
|
while i < 20000:
|
||||||
|
log.info(f"**md** | <#ff0000>测试--重--复~~日志~~: {i}")
|
||||||
|
i += 1
|
Reference in New Issue
Block a user