crawlo 1.3.4__py3-none-any.whl → 1.3.5__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of crawlo might be problematic. Click here for more details.
- crawlo/__version__.py +1 -1
- crawlo/pipelines/mysql_pipeline.py +7 -0
- crawlo/templates/run.py.tmpl +2 -2
- crawlo/utils/log.py +42 -6
- {crawlo-1.3.4.dist-info → crawlo-1.3.5.dist-info}/METADATA +1 -1
- {crawlo-1.3.4.dist-info → crawlo-1.3.5.dist-info}/RECORD +19 -9
- tests/debug_log_config.py +127 -0
- tests/detailed_log_test.py +234 -0
- tests/final_log_test.py +261 -0
- tests/fix_log_test.py +143 -0
- tests/log_buffering_test.py +112 -0
- tests/log_generation_timing_test.py +154 -0
- tests/simple_log_test2.py +138 -0
- tests/spider_log_timing_test.py +178 -0
- tests/test_get_component_logger.py +84 -0
- tests/test_logging_system.py +283 -0
- {crawlo-1.3.4.dist-info → crawlo-1.3.5.dist-info}/WHEEL +0 -0
- {crawlo-1.3.4.dist-info → crawlo-1.3.5.dist-info}/entry_points.txt +0 -0
- {crawlo-1.3.4.dist-info → crawlo-1.3.5.dist-info}/top_level.txt +0 -0
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
#!/usr/bin/env python
|
|
2
|
+
# -*- coding: UTF-8 -*-
|
|
3
|
+
"""
|
|
4
|
+
简单的日志系统测试
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
import os
|
|
8
|
+
import sys
|
|
9
|
+
import tempfile
|
|
10
|
+
import shutil
|
|
11
|
+
from pathlib import Path
|
|
12
|
+
|
|
13
|
+
# 添加项目根目录到Python路径
|
|
14
|
+
project_root = Path(__file__).parent.parent
|
|
15
|
+
sys.path.insert(0, str(project_root))
|
|
16
|
+
|
|
17
|
+
from crawlo.logging import configure_logging as configure, get_logger, LogManager
|
|
18
|
+
from crawlo.logging.config import LogConfig
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def test_file_logging_simple():
|
|
22
|
+
"""简单测试文件日志功能"""
|
|
23
|
+
print("=== 简单测试文件日志功能 ===")
|
|
24
|
+
|
|
25
|
+
# 创建临时目录
|
|
26
|
+
temp_dir = tempfile.mkdtemp()
|
|
27
|
+
log_file = os.path.join(temp_dir, 'simple_test.log')
|
|
28
|
+
|
|
29
|
+
try:
|
|
30
|
+
print(f"日志文件路径: {log_file}")
|
|
31
|
+
|
|
32
|
+
# 配置文件日志
|
|
33
|
+
configure(
|
|
34
|
+
LOG_LEVEL='INFO',
|
|
35
|
+
LOG_FILE=log_file,
|
|
36
|
+
LOG_FILE_ENABLED=True
|
|
37
|
+
)
|
|
38
|
+
|
|
39
|
+
config = LogManager().config
|
|
40
|
+
print(f"配置文件路径: {config.file_path}")
|
|
41
|
+
print(f"文件启用: {config.file_enabled}")
|
|
42
|
+
|
|
43
|
+
# 获取logger并测试输出
|
|
44
|
+
logger = get_logger('test.simple')
|
|
45
|
+
print(f"Logger handlers数量: {len(logger.handlers)}")
|
|
46
|
+
|
|
47
|
+
for handler in logger.handlers:
|
|
48
|
+
print(f"Handler类型: {type(handler).__name__}")
|
|
49
|
+
if hasattr(handler, 'baseFilename'):
|
|
50
|
+
print(f"Handler文件名: {handler.baseFilename}")
|
|
51
|
+
|
|
52
|
+
logger.info("这是一条测试日志")
|
|
53
|
+
logger.warning("这是一条警告日志")
|
|
54
|
+
|
|
55
|
+
# 检查日志文件
|
|
56
|
+
print("检查日志文件...")
|
|
57
|
+
if os.path.exists(log_file):
|
|
58
|
+
with open(log_file, 'r', encoding='utf-8') as f:
|
|
59
|
+
content = f.read()
|
|
60
|
+
print(f"日志文件内容:\n{content}")
|
|
61
|
+
else:
|
|
62
|
+
print("日志文件不存在!")
|
|
63
|
+
|
|
64
|
+
finally:
|
|
65
|
+
# 清理临时目录
|
|
66
|
+
shutil.rmtree(temp_dir, ignore_errors=True)
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def test_console_only():
|
|
70
|
+
"""测试仅控制台日志"""
|
|
71
|
+
print("\n=== 测试仅控制台日志 ===")
|
|
72
|
+
|
|
73
|
+
# 配置仅控制台日志
|
|
74
|
+
configure(
|
|
75
|
+
LOG_LEVEL='INFO',
|
|
76
|
+
LOG_CONSOLE_ENABLED=True,
|
|
77
|
+
LOG_FILE_ENABLED=False
|
|
78
|
+
)
|
|
79
|
+
|
|
80
|
+
config = LogManager().config
|
|
81
|
+
print(f"控制台启用: {config.console_enabled}")
|
|
82
|
+
print(f"文件启用: {config.file_enabled}")
|
|
83
|
+
|
|
84
|
+
# 获取logger并测试输出
|
|
85
|
+
logger = get_logger('test.console_only')
|
|
86
|
+
logger.info("这条日志应该只在控制台显示")
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
def test_module_levels():
|
|
90
|
+
"""测试模块级别日志"""
|
|
91
|
+
print("\n=== 测试模块级别日志 ===")
|
|
92
|
+
|
|
93
|
+
# 配置模块级别
|
|
94
|
+
configure(
|
|
95
|
+
LOG_LEVEL='WARNING',
|
|
96
|
+
LOG_LEVELS={
|
|
97
|
+
'module.debug': 'DEBUG',
|
|
98
|
+
'module.error': 'ERROR'
|
|
99
|
+
}
|
|
100
|
+
)
|
|
101
|
+
|
|
102
|
+
config = LogManager().config
|
|
103
|
+
print(f"默认级别: {config.level}")
|
|
104
|
+
print(f"模块级别: {config.module_levels}")
|
|
105
|
+
|
|
106
|
+
# 测试不同模块的日志级别
|
|
107
|
+
debug_logger = get_logger('module.debug')
|
|
108
|
+
debug_logger.debug("这条DEBUG日志应该显示")
|
|
109
|
+
debug_logger.info("这条INFO日志应该显示")
|
|
110
|
+
|
|
111
|
+
error_logger = get_logger('module.error')
|
|
112
|
+
error_logger.info("这条INFO日志不应该显示")
|
|
113
|
+
error_logger.error("这条ERROR日志应该显示")
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
def main():
|
|
117
|
+
"""主测试函数"""
|
|
118
|
+
print("开始简单测试Crawlo框架日志系统...")
|
|
119
|
+
|
|
120
|
+
try:
|
|
121
|
+
# 运行测试
|
|
122
|
+
test_file_logging_simple()
|
|
123
|
+
test_console_only()
|
|
124
|
+
test_module_levels()
|
|
125
|
+
|
|
126
|
+
print("\n=== 简单测试完成 ===")
|
|
127
|
+
|
|
128
|
+
except Exception as e:
|
|
129
|
+
print(f"\n测试过程中出现错误: {e}")
|
|
130
|
+
import traceback
|
|
131
|
+
traceback.print_exc()
|
|
132
|
+
return 1
|
|
133
|
+
|
|
134
|
+
return 0
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
if __name__ == '__main__':
|
|
138
|
+
sys.exit(main())
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
#!/usr/bin/env python
|
|
2
|
+
# -*- coding: UTF-8 -*-
|
|
3
|
+
"""
|
|
4
|
+
模拟爬虫场景测试日志文件生成时机
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
import os
|
|
8
|
+
import sys
|
|
9
|
+
import time
|
|
10
|
+
import asyncio
|
|
11
|
+
from pathlib import Path
|
|
12
|
+
|
|
13
|
+
# 添加项目根目录到Python路径
|
|
14
|
+
project_root = Path(__file__).parent.parent
|
|
15
|
+
sys.path.insert(0, str(project_root))
|
|
16
|
+
|
|
17
|
+
from crawlo.logging import configure_logging as configure, get_logger
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
class MockSpider:
|
|
21
|
+
"""模拟爬虫类"""
|
|
22
|
+
|
|
23
|
+
def __init__(self, name):
|
|
24
|
+
self.name = name
|
|
25
|
+
self.logger = get_logger(f'spider.{name}')
|
|
26
|
+
|
|
27
|
+
async def start_requests(self):
|
|
28
|
+
"""开始请求"""
|
|
29
|
+
self.logger.info(f"Spider {self.name} 开始请求")
|
|
30
|
+
# 模拟一些网络请求
|
|
31
|
+
for i in range(3):
|
|
32
|
+
self.logger.info(f"Spider {self.name} 发送请求 {i+1}")
|
|
33
|
+
await asyncio.sleep(0.1)
|
|
34
|
+
|
|
35
|
+
async def parse(self, response):
|
|
36
|
+
"""解析响应"""
|
|
37
|
+
self.logger.info(f"Spider {self.name} 解析响应")
|
|
38
|
+
# 模拟解析过程
|
|
39
|
+
await asyncio.sleep(0.05)
|
|
40
|
+
self.logger.info(f"Spider {self.name} 提取数据")
|
|
41
|
+
return {"data": f"item from {self.name}"}
|
|
42
|
+
|
|
43
|
+
async def crawl(self):
|
|
44
|
+
"""执行爬取"""
|
|
45
|
+
self.logger.info(f"Spider {self.name} 开始爬取")
|
|
46
|
+
await self.start_requests()
|
|
47
|
+
await self.parse("mock_response")
|
|
48
|
+
self.logger.info(f"Spider {self.name} 爬取完成")
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
async def test_spider_logging_timing():
|
|
52
|
+
"""测试爬虫日志记录时机"""
|
|
53
|
+
print("=== 测试爬虫日志记录时机 ===")
|
|
54
|
+
|
|
55
|
+
# 设置日志文件路径
|
|
56
|
+
log_file = "logs/spider_timing_test.log"
|
|
57
|
+
|
|
58
|
+
# 删除可能存在的旧日志文件
|
|
59
|
+
if os.path.exists(log_file):
|
|
60
|
+
os.remove(log_file)
|
|
61
|
+
|
|
62
|
+
# 配置日志系统
|
|
63
|
+
print("1. 配置日志系统...")
|
|
64
|
+
configure(
|
|
65
|
+
level='INFO',
|
|
66
|
+
file_path=log_file,
|
|
67
|
+
console_enabled=True,
|
|
68
|
+
file_enabled=True
|
|
69
|
+
)
|
|
70
|
+
|
|
71
|
+
print(f" 配置后日志文件是否存在: {os.path.exists(log_file)}")
|
|
72
|
+
|
|
73
|
+
# 创建爬虫实例
|
|
74
|
+
print("\n2. 创建爬虫实例...")
|
|
75
|
+
spider = MockSpider("test_spider")
|
|
76
|
+
print(f" 创建爬虫后日志文件是否存在: {os.path.exists(log_file)}")
|
|
77
|
+
|
|
78
|
+
# 检查文件内容
|
|
79
|
+
if os.path.exists(log_file):
|
|
80
|
+
with open(log_file, 'r', encoding='utf-8') as f:
|
|
81
|
+
content = f.read()
|
|
82
|
+
print(f" 日志文件行数: {len(content.splitlines())}")
|
|
83
|
+
|
|
84
|
+
# 执行爬取
|
|
85
|
+
print("\n3. 执行爬取...")
|
|
86
|
+
await spider.crawl()
|
|
87
|
+
|
|
88
|
+
print(f" 爬取完成后日志文件是否存在: {os.path.exists(log_file)}")
|
|
89
|
+
|
|
90
|
+
# 检查最终文件内容
|
|
91
|
+
if os.path.exists(log_file):
|
|
92
|
+
with open(log_file, 'r', encoding='utf-8') as f:
|
|
93
|
+
lines = f.readlines()
|
|
94
|
+
print(f"\n最终日志文件内容:")
|
|
95
|
+
print(f" 总行数: {len(lines)}")
|
|
96
|
+
print(f" 文件大小: {os.path.getsize(log_file)} 字节")
|
|
97
|
+
if lines:
|
|
98
|
+
print(f" 第一行: {lines[0].strip()}")
|
|
99
|
+
print(f" 最后一行: {lines[-1].strip()}")
|
|
100
|
+
|
|
101
|
+
# 等待一段时间后再次检查
|
|
102
|
+
print("\n4. 等待2秒后再次检查...")
|
|
103
|
+
time.sleep(2)
|
|
104
|
+
|
|
105
|
+
if os.path.exists(log_file):
|
|
106
|
+
with open(log_file, 'r', encoding='utf-8') as f:
|
|
107
|
+
lines = f.readlines()
|
|
108
|
+
print(f" 等待后总行数: {len(lines)}")
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
async def test_multiple_spiders():
|
|
112
|
+
"""测试多个爬虫的日志记录"""
|
|
113
|
+
print("\n=== 测试多个爬虫的日志记录 ===")
|
|
114
|
+
|
|
115
|
+
# 设置日志文件路径
|
|
116
|
+
log_file = "logs/multi_spider_test.log"
|
|
117
|
+
|
|
118
|
+
# 删除可能存在的旧日志文件
|
|
119
|
+
if os.path.exists(log_file):
|
|
120
|
+
os.remove(log_file)
|
|
121
|
+
|
|
122
|
+
# 配置日志系统
|
|
123
|
+
configure(
|
|
124
|
+
level='INFO',
|
|
125
|
+
file_path=log_file,
|
|
126
|
+
console_enabled=True,
|
|
127
|
+
file_enabled=True
|
|
128
|
+
)
|
|
129
|
+
|
|
130
|
+
# 创建多个爬虫实例
|
|
131
|
+
spiders = [MockSpider(f"spider_{i}") for i in range(3)]
|
|
132
|
+
|
|
133
|
+
print("1. 顺序执行多个爬虫...")
|
|
134
|
+
for i, spider in enumerate(spiders):
|
|
135
|
+
print(f" 执行爬虫 {i+1}...")
|
|
136
|
+
await spider.crawl()
|
|
137
|
+
|
|
138
|
+
# 检查当前日志文件状态
|
|
139
|
+
if os.path.exists(log_file):
|
|
140
|
+
with open(log_file, 'r', encoding='utf-8') as f:
|
|
141
|
+
lines = f.readlines()
|
|
142
|
+
print(f" 当前日志行数: {len(lines)}")
|
|
143
|
+
|
|
144
|
+
print(f"\n 所有爬虫执行完成后日志文件是否存在: {os.path.exists(log_file)}")
|
|
145
|
+
|
|
146
|
+
# 检查最终文件内容
|
|
147
|
+
if os.path.exists(log_file):
|
|
148
|
+
with open(log_file, 'r', encoding='utf-8') as f:
|
|
149
|
+
lines = f.readlines()
|
|
150
|
+
print(f"\n最终日志文件内容:")
|
|
151
|
+
print(f" 总行数: {len(lines)}")
|
|
152
|
+
print(f" 文件大小: {os.path.getsize(log_file)} 字节")
|
|
153
|
+
if lines:
|
|
154
|
+
print(f" 第一行: {lines[0].strip()}")
|
|
155
|
+
print(f" 最后一行: {lines[-1].strip()}")
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
async def main():
|
|
159
|
+
"""主函数"""
|
|
160
|
+
print("开始测试爬虫日志记录时机...")
|
|
161
|
+
|
|
162
|
+
try:
|
|
163
|
+
await test_spider_logging_timing()
|
|
164
|
+
await test_multiple_spiders()
|
|
165
|
+
|
|
166
|
+
print("\n=== 所有测试完成 ===")
|
|
167
|
+
|
|
168
|
+
except Exception as e:
|
|
169
|
+
print(f"\n测试过程中出现错误: {e}")
|
|
170
|
+
import traceback
|
|
171
|
+
traceback.print_exc()
|
|
172
|
+
return 1
|
|
173
|
+
|
|
174
|
+
return 0
|
|
175
|
+
|
|
176
|
+
|
|
177
|
+
if __name__ == '__main__':
|
|
178
|
+
asyncio.run(main())
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
#!/usr/bin/env python
|
|
2
|
+
# -*- coding: UTF-8 -*-
|
|
3
|
+
"""
|
|
4
|
+
测试增强后的 get_component_logger 函数
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
import os
|
|
8
|
+
import sys
|
|
9
|
+
from pathlib import Path
|
|
10
|
+
|
|
11
|
+
# 添加项目根目录到Python路径
|
|
12
|
+
project_root = Path(__file__).parent.parent
|
|
13
|
+
sys.path.insert(0, str(project_root))
|
|
14
|
+
|
|
15
|
+
from crawlo.utils.log import get_component_logger
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
class MockComponent:
|
|
19
|
+
"""模拟组件类"""
|
|
20
|
+
pass
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
class MockSettings:
|
|
24
|
+
"""模拟设置类"""
|
|
25
|
+
def __init__(self):
|
|
26
|
+
self.LOG_LEVEL = 'DEBUG'
|
|
27
|
+
self.LOG_LEVEL_MockComponent = 'WARNING'
|
|
28
|
+
|
|
29
|
+
def get(self, key, default=None):
|
|
30
|
+
return getattr(self, key, default)
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def test_get_component_logger():
|
|
34
|
+
"""测试 get_component_logger 函数"""
|
|
35
|
+
print("=== 测试 get_component_logger 函数 ===")
|
|
36
|
+
|
|
37
|
+
# 1. 测试基本用法
|
|
38
|
+
print("1. 测试基本用法...")
|
|
39
|
+
logger1 = get_component_logger(MockComponent)
|
|
40
|
+
print(f" Logger名称: {logger1.name}")
|
|
41
|
+
print(f" Logger级别: {logger1.level}")
|
|
42
|
+
|
|
43
|
+
# 2. 测试带settings的用法
|
|
44
|
+
print("2. 测试带settings的用法...")
|
|
45
|
+
settings = MockSettings()
|
|
46
|
+
logger2 = get_component_logger(MockComponent, settings)
|
|
47
|
+
print(f" Logger名称: {logger2.name}")
|
|
48
|
+
print(f" Logger级别: {logger2.level}")
|
|
49
|
+
|
|
50
|
+
# 3. 测试带level参数的用法
|
|
51
|
+
print("3. 测试带level参数的用法...")
|
|
52
|
+
logger3 = get_component_logger(MockComponent, level='ERROR')
|
|
53
|
+
print(f" Logger名称: {logger3.name}")
|
|
54
|
+
print(f" Logger级别: {logger3.level}")
|
|
55
|
+
|
|
56
|
+
# 4. 测试日志输出
|
|
57
|
+
print("4. 测试日志输出...")
|
|
58
|
+
logger1.info("这是info级别的测试消息")
|
|
59
|
+
logger1.warning("这是warning级别的测试消息")
|
|
60
|
+
logger1.error("这是error级别的测试消息")
|
|
61
|
+
|
|
62
|
+
print("\n=== 测试完成 ===")
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
def main():
|
|
66
|
+
"""主函数"""
|
|
67
|
+
print("开始测试增强后的 get_component_logger 函数...")
|
|
68
|
+
|
|
69
|
+
try:
|
|
70
|
+
test_get_component_logger()
|
|
71
|
+
|
|
72
|
+
print("\n=== 所有测试完成 ===")
|
|
73
|
+
|
|
74
|
+
except Exception as e:
|
|
75
|
+
print(f"\n测试过程中出现错误: {e}")
|
|
76
|
+
import traceback
|
|
77
|
+
traceback.print_exc()
|
|
78
|
+
return 1
|
|
79
|
+
|
|
80
|
+
return 0
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
if __name__ == '__main__':
|
|
84
|
+
sys.exit(main())
|
|
@@ -0,0 +1,283 @@
|
|
|
1
|
+
#!/usr/bin/env python
|
|
2
|
+
# -*- coding: UTF-8 -*-
|
|
3
|
+
"""
|
|
4
|
+
全面测试Crawlo框架日志系统的所有功能
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
import os
|
|
8
|
+
import sys
|
|
9
|
+
import tempfile
|
|
10
|
+
import shutil
|
|
11
|
+
from pathlib import Path
|
|
12
|
+
|
|
13
|
+
# 添加项目根目录到Python路径
|
|
14
|
+
project_root = Path(__file__).parent.parent
|
|
15
|
+
sys.path.insert(0, str(project_root))
|
|
16
|
+
|
|
17
|
+
from crawlo.logging import configure_logging as configure, get_logger, LogManager
|
|
18
|
+
from crawlo.logging.config import LogConfig
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def test_basic_logging():
|
|
22
|
+
"""测试基本日志功能"""
|
|
23
|
+
print("=== 测试基本日志功能 ===")
|
|
24
|
+
|
|
25
|
+
# 1. 测试默认配置
|
|
26
|
+
print("1. 测试默认配置...")
|
|
27
|
+
configure()
|
|
28
|
+
config = LogManager().config
|
|
29
|
+
print(f" 默认级别: {config.level}")
|
|
30
|
+
print(f" 默认格式: {config.format}")
|
|
31
|
+
|
|
32
|
+
# 2. 测试获取logger
|
|
33
|
+
logger = get_logger('test.basic')
|
|
34
|
+
print(f" Logger名称: {logger.name}")
|
|
35
|
+
print(f" Handlers数量: {len(logger.handlers)}")
|
|
36
|
+
|
|
37
|
+
# 3. 测试日志输出
|
|
38
|
+
logger.info("这是一条INFO级别日志")
|
|
39
|
+
logger.warning("这是一条WARNING级别日志")
|
|
40
|
+
logger.error("这是一条ERROR级别日志")
|
|
41
|
+
print(" 日志输出完成")
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def test_file_logging():
|
|
45
|
+
"""测试文件日志功能"""
|
|
46
|
+
print("\n=== 测试文件日志功能 ===")
|
|
47
|
+
|
|
48
|
+
# 创建临时目录
|
|
49
|
+
temp_dir = tempfile.mkdtemp()
|
|
50
|
+
log_file = os.path.join(temp_dir, 'test.log')
|
|
51
|
+
|
|
52
|
+
try:
|
|
53
|
+
# 1. 配置文件日志
|
|
54
|
+
print("1. 配置文件日志...")
|
|
55
|
+
configure(
|
|
56
|
+
LOG_LEVEL='DEBUG',
|
|
57
|
+
LOG_FILE=log_file,
|
|
58
|
+
LOG_MAX_BYTES=1024, # 小的轮转大小用于测试
|
|
59
|
+
LOG_BACKUP_COUNT=3
|
|
60
|
+
)
|
|
61
|
+
|
|
62
|
+
config = LogManager().config
|
|
63
|
+
print(f" 日志文件: {config.file_path}")
|
|
64
|
+
print(f" 轮转大小: {config.max_bytes}")
|
|
65
|
+
print(f" 备份数量: {config.backup_count}")
|
|
66
|
+
|
|
67
|
+
# 2. 获取logger并测试输出
|
|
68
|
+
logger = get_logger('test.file')
|
|
69
|
+
print("2. 测试日志输出...")
|
|
70
|
+
|
|
71
|
+
# 输出多条日志以触发轮转
|
|
72
|
+
for i in range(50):
|
|
73
|
+
logger.debug(f"这是第{i+1}条DEBUG日志,用于测试轮转功能")
|
|
74
|
+
logger.info(f"这是第{i+1}条INFO日志,用于测试轮转功能")
|
|
75
|
+
|
|
76
|
+
# 3. 检查日志文件
|
|
77
|
+
print("3. 检查日志文件...")
|
|
78
|
+
if os.path.exists(log_file):
|
|
79
|
+
file_size = os.path.getsize(log_file)
|
|
80
|
+
print(f" 主日志文件大小: {file_size} 字节")
|
|
81
|
+
else:
|
|
82
|
+
print(" 主日志文件不存在!")
|
|
83
|
+
|
|
84
|
+
# 检查备份文件
|
|
85
|
+
for i in range(1, 4):
|
|
86
|
+
backup_file = f"{log_file}.{i}"
|
|
87
|
+
if os.path.exists(backup_file):
|
|
88
|
+
backup_size = os.path.getsize(backup_file)
|
|
89
|
+
print(f" 备份文件 {i} 大小: {backup_size} 字节")
|
|
90
|
+
else:
|
|
91
|
+
print(f" 备份文件 {i} 不存在")
|
|
92
|
+
|
|
93
|
+
finally:
|
|
94
|
+
# 清理临时目录
|
|
95
|
+
shutil.rmtree(temp_dir, ignore_errors=True)
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
def test_console_logging():
|
|
99
|
+
"""测试控制台日志功能"""
|
|
100
|
+
print("\n=== 测试控制台日志功能 ===")
|
|
101
|
+
|
|
102
|
+
# 1. 配置仅控制台日志
|
|
103
|
+
print("1. 配置仅控制台日志...")
|
|
104
|
+
configure(
|
|
105
|
+
LOG_LEVEL='INFO',
|
|
106
|
+
LOG_CONSOLE_ENABLED=True,
|
|
107
|
+
LOG_FILE_ENABLED=False
|
|
108
|
+
)
|
|
109
|
+
|
|
110
|
+
config = LogManager().config
|
|
111
|
+
print(f" 控制台启用: {config.console_enabled}")
|
|
112
|
+
print(f" 文件日志启用: {config.file_enabled}")
|
|
113
|
+
|
|
114
|
+
# 2. 测试日志输出
|
|
115
|
+
logger = get_logger('test.console')
|
|
116
|
+
print("2. 测试日志输出...")
|
|
117
|
+
logger.info("这条日志应该只在控制台显示")
|
|
118
|
+
logger.debug("这条DEBUG日志不应该显示(级别低于INFO)")
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
def test_module_level_logging():
|
|
122
|
+
"""测试模块级别日志功能"""
|
|
123
|
+
print("\n=== 测试模块级别日志功能 ===")
|
|
124
|
+
|
|
125
|
+
# 1. 配置模块级别
|
|
126
|
+
print("1. 配置模块级别...")
|
|
127
|
+
configure(
|
|
128
|
+
LOG_LEVEL='WARNING',
|
|
129
|
+
LOG_LEVELS={
|
|
130
|
+
'test.module.high': 'DEBUG',
|
|
131
|
+
'test.module.low': 'ERROR'
|
|
132
|
+
}
|
|
133
|
+
)
|
|
134
|
+
|
|
135
|
+
config = LogManager().config
|
|
136
|
+
print(f" 默认级别: {config.level}")
|
|
137
|
+
print(f" 模块级别配置: {config.module_levels}")
|
|
138
|
+
|
|
139
|
+
# 2. 测试不同模块的日志级别
|
|
140
|
+
print("2. 测试不同模块的日志级别...")
|
|
141
|
+
|
|
142
|
+
# 高级别模块(DEBUG)
|
|
143
|
+
high_logger = get_logger('test.module.high')
|
|
144
|
+
print(f" high_logger 级别: {logging.getLevelName(high_logger.handlers[0].level)}")
|
|
145
|
+
high_logger.debug("这条DEBUG日志应该显示")
|
|
146
|
+
high_logger.info("这条INFO日志应该显示")
|
|
147
|
+
|
|
148
|
+
# 低级别模块(ERROR)
|
|
149
|
+
low_logger = get_logger('test.module.low')
|
|
150
|
+
print(f" low_logger 级别: {logging.getLevelName(low_logger.handlers[0].level)}")
|
|
151
|
+
low_logger.info("这条INFO日志不应该显示")
|
|
152
|
+
low_logger.error("这条ERROR日志应该显示")
|
|
153
|
+
|
|
154
|
+
# 默认级别模块(WARNING)
|
|
155
|
+
default_logger = get_logger('test.module.default')
|
|
156
|
+
print(f" default_logger 级别: {logging.getLevelName(default_logger.handlers[0].level)}")
|
|
157
|
+
default_logger.info("这条INFO日志不应该显示")
|
|
158
|
+
default_logger.warning("这条WARNING日志应该显示")
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
def test_log_config_from_settings():
|
|
162
|
+
"""测试从settings对象创建配置"""
|
|
163
|
+
print("\n=== 测试从settings对象创建配置 ===")
|
|
164
|
+
|
|
165
|
+
# 1. 创建模拟settings对象
|
|
166
|
+
class MockSettings:
|
|
167
|
+
def __init__(self):
|
|
168
|
+
self.LOG_LEVEL = 'DEBUG'
|
|
169
|
+
self.LOG_FILE = 'test_settings.log'
|
|
170
|
+
self.LOG_MAX_BYTES = 5 * 1024 * 1024
|
|
171
|
+
self.LOG_BACKUP_COUNT = 2
|
|
172
|
+
self.LOG_ENCODING = 'utf-8'
|
|
173
|
+
self.LOG_CONSOLE_ENABLED = True
|
|
174
|
+
self.LOG_FILE_ENABLED = True
|
|
175
|
+
self.LOG_LEVELS = {'test.custom': 'INFO'}
|
|
176
|
+
|
|
177
|
+
# 2. 从settings创建配置
|
|
178
|
+
print("2. 从settings创建配置...")
|
|
179
|
+
settings = MockSettings()
|
|
180
|
+
config = LogConfig.from_settings(settings)
|
|
181
|
+
|
|
182
|
+
print(f" 级别: {config.level}")
|
|
183
|
+
print(f" 文件: {config.file_path}")
|
|
184
|
+
print(f" 轮转大小: {config.max_bytes}")
|
|
185
|
+
print(f" 备份数量: {config.backup_count}")
|
|
186
|
+
print(f" 编码: {config.encoding}")
|
|
187
|
+
print(f" 控制台启用: {config.console_enabled}")
|
|
188
|
+
print(f" 文件启用: {config.file_enabled}")
|
|
189
|
+
print(f" 模块级别: {config.module_levels}")
|
|
190
|
+
|
|
191
|
+
|
|
192
|
+
def test_log_config_validation():
|
|
193
|
+
"""测试日志配置验证功能"""
|
|
194
|
+
print("\n=== 测试日志配置验证功能 ===")
|
|
195
|
+
|
|
196
|
+
# 1. 测试有效配置
|
|
197
|
+
print("1. 测试有效配置...")
|
|
198
|
+
valid_config = LogConfig(
|
|
199
|
+
level='INFO',
|
|
200
|
+
file_path='test_valid.log'
|
|
201
|
+
)
|
|
202
|
+
is_valid = valid_config.validate()
|
|
203
|
+
print(f" 有效配置验证结果: {is_valid}")
|
|
204
|
+
|
|
205
|
+
# 2. 测试无效级别
|
|
206
|
+
print("2. 测试无效级别...")
|
|
207
|
+
invalid_config = LogConfig(
|
|
208
|
+
level='INVALID',
|
|
209
|
+
file_path='test_invalid.log'
|
|
210
|
+
)
|
|
211
|
+
is_valid = invalid_config.validate()
|
|
212
|
+
print(f" 无效级别验证结果: {is_valid}")
|
|
213
|
+
|
|
214
|
+
# 3. 测试目录创建
|
|
215
|
+
print("3. 测试目录创建...")
|
|
216
|
+
temp_dir = tempfile.mkdtemp()
|
|
217
|
+
nested_log_file = os.path.join(temp_dir, 'subdir', 'test.log')
|
|
218
|
+
|
|
219
|
+
config = LogConfig(
|
|
220
|
+
level='INFO',
|
|
221
|
+
file_path=nested_log_file
|
|
222
|
+
)
|
|
223
|
+
is_valid = config.validate()
|
|
224
|
+
print(f" 目录创建验证结果: {is_valid}")
|
|
225
|
+
|
|
226
|
+
# 清理
|
|
227
|
+
shutil.rmtree(temp_dir, ignore_errors=True)
|
|
228
|
+
|
|
229
|
+
|
|
230
|
+
def test_logger_caching():
|
|
231
|
+
"""测试Logger缓存功能"""
|
|
232
|
+
print("\n=== 测试Logger缓存功能 ===")
|
|
233
|
+
|
|
234
|
+
# 重置配置以确保测试环境干净
|
|
235
|
+
LogManager().reset()
|
|
236
|
+
|
|
237
|
+
# 1. 获取同一个logger多次
|
|
238
|
+
print("1. 测试缓存...")
|
|
239
|
+
logger1 = get_logger('test.cache')
|
|
240
|
+
logger2 = get_logger('test.cache')
|
|
241
|
+
|
|
242
|
+
print(f" 第一次获取: {id(logger1)}")
|
|
243
|
+
print(f" 第二次获取: {id(logger2)}")
|
|
244
|
+
print(f" 是否为同一对象: {logger1 is logger2}")
|
|
245
|
+
|
|
246
|
+
# 2. 测试配置更新后缓存刷新
|
|
247
|
+
print("2. 测试配置更新...")
|
|
248
|
+
configure(LOG_LEVEL='ERROR')
|
|
249
|
+
logger3 = get_logger('test.cache')
|
|
250
|
+
print(f" 更新配置后获取: {id(logger3)}")
|
|
251
|
+
print(f" 是否与之前相同: {logger1 is logger3}")
|
|
252
|
+
|
|
253
|
+
|
|
254
|
+
def main():
|
|
255
|
+
"""主测试函数"""
|
|
256
|
+
print("开始全面测试Crawlo框架日志系统...")
|
|
257
|
+
|
|
258
|
+
try:
|
|
259
|
+
# 运行所有测试
|
|
260
|
+
test_basic_logging()
|
|
261
|
+
test_file_logging()
|
|
262
|
+
test_console_logging()
|
|
263
|
+
test_module_level_logging()
|
|
264
|
+
test_log_config_from_settings()
|
|
265
|
+
test_log_config_validation()
|
|
266
|
+
test_logger_caching()
|
|
267
|
+
|
|
268
|
+
print("\n=== 所有测试完成 ===")
|
|
269
|
+
|
|
270
|
+
except Exception as e:
|
|
271
|
+
print(f"\n测试过程中出现错误: {e}")
|
|
272
|
+
import traceback
|
|
273
|
+
traceback.print_exc()
|
|
274
|
+
return 1
|
|
275
|
+
|
|
276
|
+
return 0
|
|
277
|
+
|
|
278
|
+
|
|
279
|
+
# 导入logging模块用于测试
|
|
280
|
+
import logging
|
|
281
|
+
|
|
282
|
+
if __name__ == '__main__':
|
|
283
|
+
sys.exit(main())
|
|
File without changes
|
|
File without changes
|
|
File without changes
|