🕐 修复系统日志时区问题
- 日志写入时使用 datetime('now', 'localtime') 替代 CURRENT_TIMESTAMP
- 日志统计和清理查询也使用本地时间
- 解决日志时间比实际时间慢8小时的问题(UTC → 北京时间)
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -682,8 +682,8 @@ const SystemLogDB = {
|
|||||||
log({ level = 'info', category, action, message, userId = null, username = null, ipAddress = null, userAgent = null, details = null }) {
|
log({ level = 'info', category, action, message, userId = null, username = null, ipAddress = null, userAgent = null, details = null }) {
|
||||||
try {
|
try {
|
||||||
const stmt = db.prepare(`
|
const stmt = db.prepare(`
|
||||||
INSERT INTO system_logs (level, category, action, message, user_id, username, ip_address, user_agent, details)
|
INSERT INTO system_logs (level, category, action, message, user_id, username, ip_address, user_agent, details, created_at)
|
||||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, datetime('now', 'localtime'))
|
||||||
`);
|
`);
|
||||||
|
|
||||||
const detailsStr = details ? (typeof details === 'string' ? details : JSON.stringify(details)) : null;
|
const detailsStr = details ? (typeof details === 'string' ? details : JSON.stringify(details)) : null;
|
||||||
@@ -776,7 +776,7 @@ const SystemLogDB = {
|
|||||||
return db.prepare(`
|
return db.prepare(`
|
||||||
SELECT DATE(created_at) as date, COUNT(*) as count
|
SELECT DATE(created_at) as date, COUNT(*) as count
|
||||||
FROM system_logs
|
FROM system_logs
|
||||||
WHERE created_at >= datetime('now', '-' || ? || ' days')
|
WHERE created_at >= datetime('now', 'localtime', '-' || ? || ' days')
|
||||||
GROUP BY DATE(created_at)
|
GROUP BY DATE(created_at)
|
||||||
ORDER BY date DESC
|
ORDER BY date DESC
|
||||||
`).all(days);
|
`).all(days);
|
||||||
@@ -786,7 +786,7 @@ const SystemLogDB = {
|
|||||||
cleanup(keepDays = 90) {
|
cleanup(keepDays = 90) {
|
||||||
const result = db.prepare(`
|
const result = db.prepare(`
|
||||||
DELETE FROM system_logs
|
DELETE FROM system_logs
|
||||||
WHERE created_at < datetime('now', '-' || ? || ' days')
|
WHERE created_at < datetime('now', 'localtime', '-' || ? || ' days')
|
||||||
`).run(keepDays);
|
`).run(keepDays);
|
||||||
|
|
||||||
return result.changes;
|
return result.changes;
|
||||||
|
|||||||
Reference in New Issue
Block a user