Skip to content

Commit 88d824b

Browse files
committed
feat(vulnTestServer): 添加日志系统
- 使用Python内置logging模块 - 支持同时输出到控制台和文件 - 支持滚动日志文件(RotatingFileHandler) - 支持JSON配置文件进行配置 - 日志文件独立存放在logs目录 - 替换所有print调试语句为结构化日志
1 parent d8289f8 commit 88d824b

11 files changed

Lines changed: 317 additions & 33 deletions

File tree

src/vulnTestServer/handlers/base.py

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@
1111
import time
1212
import xml.etree.ElementTree as ET
1313
from urllib.parse import unquote
14-
from datetime import datetime
14+
15+
# 导入日志模块
16+
import sys
17+
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
18+
from logger import logger, sql_logger, error_logger
1519

1620

1721
class BaseHandlerMixin:
@@ -31,20 +35,6 @@ class BaseHandlerMixin:
3135
'.svg': 'image/svg+xml',
3236
}
3337

34-
def log_message(self, format, *args):
35-
"""自定义日志格式"""
36-
from config import LOG_REQUESTS, LOG_FILE
37-
if LOG_REQUESTS:
38-
timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
39-
message = f"[{timestamp}] {self.address_string()} - {format % args}"
40-
print(message)
41-
try:
42-
os.makedirs(os.path.dirname(LOG_FILE), exist_ok=True)
43-
with open(LOG_FILE, 'a', encoding='utf-8') as f:
44-
f.write(message + '\n')
45-
except:
46-
pass
47-
4838
def send_json_response(self, data, status=200):
4939
"""发送JSON响应"""
5040
self.send_response(status)
@@ -131,6 +121,7 @@ def send_static_file(self, filepath):
131121
self.end_headers()
132122
self.wfile.write(content)
133123
except Exception as e:
124+
error_logger.exception("Error serving static file: %s", filepath)
134125
self.send_error(500, str(e))
135126

136127
def get_post_data(self):

src/vulnTestServer/handlers/cart_handlers.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
from config import DEBUG
1414
from database import get_db_connection
15+
from logger import logger
1516

1617

1718
class CartHandlerMixin:
@@ -47,7 +48,7 @@ def handle_cart_add(self, data):
4748
return
4849

4950
if DEBUG:
50-
print(f"[CartAdd] session_id={session_id}, csrf_token={csrf_token}")
51+
logger.debug("[CartAdd] session_id=%s, csrf_token=%s", session_id, csrf_token)
5152

5253
conn = get_db_connection()
5354
cursor = conn.cursor()
@@ -102,7 +103,7 @@ def handle_cart_update(self, data):
102103
return
103104

104105
if DEBUG:
105-
print(f"[CartUpdate] session_id={session_id}, csrf_token={csrf_token}")
106+
logger.debug("[CartUpdate] session_id=%s, csrf_token=%s", session_id, csrf_token)
106107

107108
conn = get_db_connection()
108109
cursor = conn.cursor()

src/vulnTestServer/handlers/order_handlers.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from config import DEBUG
1818
from database import get_db_connection
1919
from waf import get_waf
20+
from logger import sql_logger, logger
2021

2122

2223
class OrderHandlerMixin:
@@ -62,7 +63,7 @@ def handle_order_create(self, data):
6263
return
6364

6465
if DEBUG:
65-
print(f"[OrderCreate] session_id={session_id}, token={token}, user_agent={user_agent}")
66+
logger.debug("[OrderCreate] session_id=%s, token=%s, user_agent=%s", session_id, token, user_agent)
6667

6768
conn = get_db_connection()
6869
cursor = conn.cursor()
@@ -130,7 +131,7 @@ def handle_orders_query(self, params):
130131
return
131132

132133
if DEBUG:
133-
print(f"[SQL] {sql}")
134+
sql_logger.debug("[SQL] %s", sql)
134135

135136
try:
136137
# 不使用executescript,避免堆叠查询修改数据
@@ -191,7 +192,7 @@ def handle_order_cancel(self, data):
191192
return
192193

193194
if DEBUG:
194-
print(f"[OrderCancel] session_id={session_id}, auth_token={auth_token}")
195+
logger.debug("[OrderCancel] session_id=%s, auth_token=%s", session_id, auth_token)
195196

196197
conn = get_db_connection()
197198
cursor = conn.cursor()

src/vulnTestServer/handlers/product_handlers.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from config import DEBUG
1313
from database import get_db_connection
1414
from waf import get_waf
15+
from logger import sql_logger
1516

1617

1718
class ProductHandlerMixin:
@@ -66,7 +67,7 @@ def handle_products_search(self, params):
6667
sql += " AND is_active = 1"
6768

6869
if DEBUG:
69-
print(f"[SQL] {sql}")
70+
sql_logger.debug("[SQL] %s", sql)
7071

7172
try:
7273
cursor.execute(sql)
@@ -123,7 +124,7 @@ def handle_product_detail(self, params):
123124
sql = f"SELECT * FROM products WHERE id = {product_id}"
124125

125126
if DEBUG:
126-
print(f"[SQL] {sql}")
127+
sql_logger.debug("[SQL] %s", sql)
127128

128129
try:
129130
start_time = time.time()

src/vulnTestServer/handlers/system_handlers.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from config import DEBUG, VERSION, DIFFICULTY, APP_NAME
1616
from database import get_db_connection
1717
from waf import get_waf, set_difficulty
18+
from logger import logger
1819

1920

2021
class SystemHandlerMixin:
@@ -127,7 +128,7 @@ def handle_feedback(self, data):
127128
return
128129

129130
if DEBUG:
130-
print(f"[Feedback] session_id={session_id}, token={token}, timestamp={timestamp}")
131+
logger.debug("[Feedback] session_id=%s, token=%s, timestamp=%s", session_id, token, timestamp)
131132

132133
conn = get_db_connection()
133134
cursor = conn.cursor()

src/vulnTestServer/handlers/user_handlers.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from config import DEBUG
1313
from database import get_db_connection, hash_password
1414
from waf import get_waf
15+
from logger import sql_logger, logger
1516

1617

1718
class UserHandlerMixin:
@@ -44,8 +45,8 @@ def handle_user_login(self, data):
4445
sql = f"SELECT * FROM users WHERE username = '{username}' AND password = '{hash_password(password)}'"
4546

4647
if DEBUG:
47-
print(f"[SQL] {sql}")
48-
print(f"[Session] session_id={session_id}, device_id={device_id}")
48+
sql_logger.debug("[SQL] %s", sql)
49+
logger.debug("[Session] session_id=%s, device_id=%s", session_id, device_id)
4950

5051
try:
5152
cursor.execute(sql)
@@ -101,7 +102,7 @@ def handle_user_register(self, data):
101102
return
102103

103104
if DEBUG:
104-
print(f"[Register] session_id={session_id}, captcha_token={captcha_token}")
105+
logger.debug("[Register] session_id=%s, captcha_token=%s", session_id, captcha_token)
105106

106107
conn = get_db_connection()
107108
cursor = conn.cursor()
@@ -147,7 +148,7 @@ def handle_user_profile(self, params):
147148
sql = f"SELECT id, username, email, phone, address, balance FROM users WHERE id = {user_id}"
148149

149150
if DEBUG:
150-
print(f"[SQL] {sql}")
151+
sql_logger.debug("[SQL] %s", sql)
151152

152153
try:
153154
cursor.execute(sql)
@@ -214,7 +215,7 @@ def handle_user_update(self, data):
214215
return
215216

216217
if DEBUG:
217-
print(f"[UserUpdate] session_id={session_id}, token={token}, device_id={device_id}")
218+
logger.debug("[UserUpdate] session_id=%s, token=%s, device_id=%s", session_id, token, device_id)
218219

219220
conn = get_db_connection()
220221
cursor = conn.cursor()

src/vulnTestServer/logger.py

Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
"""
4+
VulnShop 日志模块
5+
6+
使用Python内置logging库,支持:
7+
- 同时输出到控制台和文件
8+
- 滚动日志文件(RotatingFileHandler)
9+
- 通过JSON配置文件进行配置
10+
- 多个日志器:主日志、访问日志、SQL日志、错误日志
11+
"""
12+
13+
import os
14+
import json
15+
import logging
16+
import logging.config
17+
import logging.handlers
18+
from pathlib import Path
19+
20+
21+
# 日志目录
22+
LOG_DIR = Path(__file__).parent / "logs"
23+
CONFIG_FILE = Path(__file__).parent / "logging_config.json"
24+
25+
# 默认配置(当配置文件不存在时使用)
26+
DEFAULT_CONFIG = {
27+
"version": 1,
28+
"disable_existing_loggers": False,
29+
"formatters": {
30+
"standard": {
31+
"format": "[%(asctime)s] %(levelname)-8s %(name)s - %(message)s",
32+
"datefmt": "%Y-%m-%d %H:%M:%S"
33+
},
34+
"detailed": {
35+
"format": "[%(asctime)s] %(levelname)-8s [%(name)s:%(funcName)s:%(lineno)d] - %(message)s",
36+
"datefmt": "%Y-%m-%d %H:%M:%S"
37+
},
38+
"access": {
39+
"format": "[%(asctime)s] %(message)s",
40+
"datefmt": "%Y-%m-%d %H:%M:%S"
41+
}
42+
},
43+
"handlers": {
44+
"console": {
45+
"class": "logging.StreamHandler",
46+
"level": "DEBUG",
47+
"formatter": "standard",
48+
"stream": "ext://sys.stdout"
49+
},
50+
"file": {
51+
"class": "logging.handlers.RotatingFileHandler",
52+
"level": "DEBUG",
53+
"formatter": "detailed",
54+
"filename": str(LOG_DIR / "vulnshop.log"),
55+
"maxBytes": 10485760, # 10MB
56+
"backupCount": 5,
57+
"encoding": "utf-8"
58+
},
59+
"access_file": {
60+
"class": "logging.handlers.RotatingFileHandler",
61+
"level": "INFO",
62+
"formatter": "access",
63+
"filename": str(LOG_DIR / "access.log"),
64+
"maxBytes": 10485760,
65+
"backupCount": 5,
66+
"encoding": "utf-8"
67+
},
68+
"error_file": {
69+
"class": "logging.handlers.RotatingFileHandler",
70+
"level": "ERROR",
71+
"formatter": "detailed",
72+
"filename": str(LOG_DIR / "error.log"),
73+
"maxBytes": 5242880, # 5MB
74+
"backupCount": 3,
75+
"encoding": "utf-8"
76+
}
77+
},
78+
"loggers": {
79+
"vulnshop": {
80+
"level": "DEBUG",
81+
"handlers": ["console", "file"],
82+
"propagate": False
83+
},
84+
"vulnshop.access": {
85+
"level": "INFO",
86+
"handlers": ["console", "access_file"],
87+
"propagate": False
88+
},
89+
"vulnshop.sql": {
90+
"level": "DEBUG",
91+
"handlers": ["console", "file"],
92+
"propagate": False
93+
},
94+
"vulnshop.error": {
95+
"level": "ERROR",
96+
"handlers": ["console", "error_file"],
97+
"propagate": False
98+
}
99+
},
100+
"root": {
101+
"level": "INFO",
102+
"handlers": ["console", "file"]
103+
}
104+
}
105+
106+
107+
def _ensure_log_dir():
108+
"""确保日志目录存在"""
109+
LOG_DIR.mkdir(parents=True, exist_ok=True)
110+
111+
112+
def _fix_log_paths(config: dict) -> dict:
113+
"""修正配置中的日志文件路径为绝对路径"""
114+
handlers = config.get("handlers", {})
115+
for handler_name, handler_config in handlers.items():
116+
if "filename" in handler_config:
117+
filename = handler_config["filename"]
118+
# 如果是相对路径,转换为绝对路径
119+
if not os.path.isabs(filename):
120+
handler_config["filename"] = str(LOG_DIR / os.path.basename(filename))
121+
return config
122+
123+
124+
def load_config() -> dict:
125+
"""加载日志配置"""
126+
_ensure_log_dir()
127+
128+
if CONFIG_FILE.exists():
129+
try:
130+
with open(CONFIG_FILE, 'r', encoding='utf-8') as f:
131+
config = json.load(f)
132+
return _fix_log_paths(config)
133+
except Exception as e:
134+
print(f"[WARNING] Failed to load logging config: {e}, using default config")
135+
return _fix_log_paths(DEFAULT_CONFIG.copy())
136+
else:
137+
return _fix_log_paths(DEFAULT_CONFIG.copy())
138+
139+
140+
def setup_logging():
141+
"""初始化日志系统"""
142+
config = load_config()
143+
logging.config.dictConfig(config)
144+
145+
146+
def get_logger(name: str = "vulnshop") -> logging.Logger:
147+
"""
148+
获取日志器
149+
150+
Args:
151+
name: 日志器名称
152+
- "vulnshop": 主日志器
153+
- "vulnshop.access": 访问日志器
154+
- "vulnshop.sql": SQL日志器
155+
- "vulnshop.error": 错误日志器
156+
157+
Returns:
158+
logging.Logger: 日志器实例
159+
"""
160+
return logging.getLogger(name)
161+
162+
163+
# 便捷函数
164+
def get_main_logger() -> logging.Logger:
165+
"""获取主日志器"""
166+
return get_logger("vulnshop")
167+
168+
169+
def get_access_logger() -> logging.Logger:
170+
"""获取访问日志器"""
171+
return get_logger("vulnshop.access")
172+
173+
174+
def get_sql_logger() -> logging.Logger:
175+
"""获取SQL日志器"""
176+
return get_logger("vulnshop.sql")
177+
178+
179+
def get_error_logger() -> logging.Logger:
180+
"""获取错误日志器"""
181+
return get_logger("vulnshop.error")
182+
183+
184+
# 模块加载时自动初始化日志系统
185+
setup_logging()
186+
187+
# 导出的日志器实例
188+
logger = get_main_logger()
189+
access_logger = get_access_logger()
190+
sql_logger = get_sql_logger()
191+
error_logger = get_error_logger()

0 commit comments

Comments
 (0)