74 lines
2.7 KiB
Python
74 lines
2.7 KiB
Python
|
|
import sqlite3
|
||
|
|
import os
|
||
|
|
|
||
|
|
def check_audit_logs():
|
||
|
|
"""检查审计日志表"""
|
||
|
|
try:
|
||
|
|
# 连接到数据库
|
||
|
|
if os.path.exists('instance/kamaxitong.db'):
|
||
|
|
conn = sqlite3.connect('instance/kamaxitong.db')
|
||
|
|
cursor = conn.cursor()
|
||
|
|
|
||
|
|
# 查询审计日志表
|
||
|
|
print("=== 查询审计日志表 ===")
|
||
|
|
cursor.execute("SELECT * FROM audit_log ORDER BY create_time DESC LIMIT 10")
|
||
|
|
rows = cursor.fetchall()
|
||
|
|
|
||
|
|
if rows:
|
||
|
|
print(f"找到 {len(rows)} 条审计日志记录:")
|
||
|
|
# 获取列名
|
||
|
|
column_names = [description[0] for description in cursor.description]
|
||
|
|
print("列名:", column_names)
|
||
|
|
|
||
|
|
for row in rows:
|
||
|
|
print(row)
|
||
|
|
else:
|
||
|
|
print("审计日志表为空")
|
||
|
|
|
||
|
|
conn.close()
|
||
|
|
else:
|
||
|
|
print("数据库文件不存在")
|
||
|
|
|
||
|
|
except Exception as e:
|
||
|
|
print(f"检查审计日志时出现错误: {e}")
|
||
|
|
|
||
|
|
def check_log_file():
|
||
|
|
"""检查日志文件"""
|
||
|
|
try:
|
||
|
|
print("\n=== 检查日志文件 ===")
|
||
|
|
if os.path.exists('logs/kamaxitong.log'):
|
||
|
|
# 以二进制模式读取文件
|
||
|
|
with open('logs/kamaxitong.log', 'rb') as f:
|
||
|
|
content = f.read()
|
||
|
|
print(f"日志文件大小: {len(content)} 字节")
|
||
|
|
|
||
|
|
# 尝试以不同编码读取
|
||
|
|
try:
|
||
|
|
text_content = content.decode('utf-8')
|
||
|
|
lines = text_content.split('\n')
|
||
|
|
print(f"日志文件行数: {len(lines)}")
|
||
|
|
print("最后10行:")
|
||
|
|
for line in lines[-10:]:
|
||
|
|
if line.strip():
|
||
|
|
print(line.strip())
|
||
|
|
except UnicodeDecodeError:
|
||
|
|
# 尝试其他编码
|
||
|
|
try:
|
||
|
|
text_content = content.decode('gbk')
|
||
|
|
lines = text_content.split('\n')
|
||
|
|
print(f"日志文件行数: {len(lines)} (GBK编码)")
|
||
|
|
print("最后10行:")
|
||
|
|
for line in lines[-10:]:
|
||
|
|
if line.strip():
|
||
|
|
print(line.strip())
|
||
|
|
except UnicodeDecodeError:
|
||
|
|
print("无法解码日志文件内容")
|
||
|
|
else:
|
||
|
|
print("日志文件不存在")
|
||
|
|
except Exception as e:
|
||
|
|
print(f"检查日志文件时出现错误: {e}")
|
||
|
|
|
||
|
|
if __name__ == "__main__":
|
||
|
|
print("检查日志系统...")
|
||
|
|
check_audit_logs()
|
||
|
|
check_log_file()
|