feat: add log settings page
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"time"
|
||||
|
||||
"fmt"
|
||||
@@ -85,24 +84,30 @@ func (s *AppLogService) Clear(category string) error {
|
||||
}
|
||||
|
||||
func (s *AppLogService) GetRetentionConfigs() map[string]LogRetentionConfig {
|
||||
val := s.settingsService.Get(constant.SectionSystem, "log_retention")
|
||||
var configs map[string]LogRetentionConfig
|
||||
if val != "" {
|
||||
_ = json.Unmarshal([]byte(val), &configs)
|
||||
}
|
||||
if configs == nil {
|
||||
configs = map[string]LogRetentionConfig{
|
||||
constant.LogCategorySystemNotice: {Days: 30, MaxCount: 500},
|
||||
constant.LogCategoryPushLog: {Days: 15, MaxCount: 5000},
|
||||
constant.LogCategoryDefault: {Days: 30, MaxCount: 10000},
|
||||
}
|
||||
configs := map[string]LogRetentionConfig{
|
||||
constant.LogCategorySystemNotice: {
|
||||
Days: utils.ToInt(s.settingsService.Get(constant.SectionSystem, constant.KeySystemNoticeDays), 30),
|
||||
MaxCount: utils.ToInt(s.settingsService.Get(constant.SectionSystem, constant.KeySystemNoticeMaxCount), 500),
|
||||
},
|
||||
constant.LogCategoryPushLog: {
|
||||
Days: utils.ToInt(s.settingsService.Get(constant.SectionSystem, constant.KeyPushLogDays), 15),
|
||||
MaxCount: utils.ToInt(s.settingsService.Get(constant.SectionSystem, constant.KeyPushLogMaxCount), 5000),
|
||||
},
|
||||
constant.LogCategoryLoginLog: {
|
||||
Days: utils.ToInt(s.settingsService.Get(constant.SectionSystem, constant.KeyLoginLogDays), 30),
|
||||
MaxCount: utils.ToInt(s.settingsService.Get(constant.SectionSystem, constant.KeyLoginLogMaxCount), 1000),
|
||||
},
|
||||
constant.LogCategoryDefault: {
|
||||
Days: 30,
|
||||
MaxCount: 10000,
|
||||
},
|
||||
}
|
||||
return configs
|
||||
}
|
||||
|
||||
func (s *AppLogService) CleanUp() {
|
||||
configs := s.GetRetentionConfigs()
|
||||
categories := []string{constant.LogCategorySystemNotice, constant.LogCategoryPushLog}
|
||||
categories := []string{constant.LogCategorySystemNotice, constant.LogCategoryPushLog, constant.LogCategoryLoginLog}
|
||||
|
||||
for _, cat := range categories {
|
||||
cfg, ok := configs[cat]
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"github.com/engigu/baihu-panel/internal/cache"
|
||||
"github.com/engigu/baihu-panel/internal/constant"
|
||||
"github.com/engigu/baihu-panel/internal/database"
|
||||
@@ -32,21 +35,58 @@ func (s *SettingsService) InitSettings() error {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// 初始化日志清理配置
|
||||
var logRetentionCount int64
|
||||
database.DB.Model(&models.Setting{}).Where("section = ? AND `key` = ?", constant.SectionSystem, constant.KeyLogRetention).Count(&logRetentionCount)
|
||||
if logRetentionCount == 0 {
|
||||
if err := database.DB.Create(&models.Setting{
|
||||
ID: utils.GenerateID(),
|
||||
Section: constant.SectionSystem,
|
||||
Key: constant.KeyLogRetention,
|
||||
Value: models.BigText(constant.DefaultLogRetention),
|
||||
}).Error; err != nil {
|
||||
return err
|
||||
// 检查是否需要从旧的 JSON 迁移
|
||||
oldVal := s.Get(constant.SectionSystem, "log_retention")
|
||||
if oldVal != "" {
|
||||
var oldConfigs map[string]struct {
|
||||
Days int `json:"days"`
|
||||
MaxCount int `json:"max_count"`
|
||||
}
|
||||
if err := json.Unmarshal([]byte(oldVal), &oldConfigs); err == nil {
|
||||
migrationMap := map[string]string{}
|
||||
if cfg, ok := oldConfigs[constant.LogCategorySystemNotice]; ok {
|
||||
migrationMap[constant.KeySystemNoticeDays] = fmt.Sprintf("%d", cfg.Days)
|
||||
migrationMap[constant.KeySystemNoticeMaxCount] = fmt.Sprintf("%d", cfg.MaxCount)
|
||||
}
|
||||
if cfg, ok := oldConfigs[constant.LogCategoryPushLog]; ok {
|
||||
migrationMap[constant.KeyPushLogDays] = fmt.Sprintf("%d", cfg.Days)
|
||||
migrationMap[constant.KeyPushLogMaxCount] = fmt.Sprintf("%d", cfg.MaxCount)
|
||||
}
|
||||
if cfg, ok := oldConfigs[constant.LogCategoryLoginLog]; ok {
|
||||
migrationMap[constant.KeyLoginLogDays] = fmt.Sprintf("%d", cfg.Days)
|
||||
migrationMap[constant.KeyLoginLogMaxCount] = fmt.Sprintf("%d", cfg.MaxCount)
|
||||
}
|
||||
|
||||
if len(migrationMap) > 0 {
|
||||
for k, v := range migrationMap {
|
||||
s.Set(constant.SectionSystem, k, v)
|
||||
}
|
||||
// 迁移完成后删除旧键
|
||||
s.Delete(constant.SectionSystem, "log_retention")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// 默认值初始化
|
||||
defaultRetention := map[string]string{
|
||||
constant.KeySystemNoticeDays: "30",
|
||||
constant.KeySystemNoticeMaxCount: "500",
|
||||
constant.KeyPushLogDays: "15",
|
||||
constant.KeyPushLogMaxCount: "5000",
|
||||
constant.KeyLoginLogDays: "30",
|
||||
constant.KeyLoginLogMaxCount: "1000",
|
||||
}
|
||||
|
||||
for k, v := range defaultRetention {
|
||||
var count int64
|
||||
database.DB.Model(&models.Setting{}).Where("section = ? AND `key` = ?", constant.SectionSystem, k).Count(&count)
|
||||
if count == 0 {
|
||||
s.Set(constant.SectionSystem, k, v)
|
||||
}
|
||||
}
|
||||
|
||||
// 初始化或获取 JWT Secret 密码
|
||||
var secCount int64
|
||||
database.DB.Model(&models.Setting{}).Where("section = ? AND `key` = ?", constant.SectionSecurity, constant.KeySecret).Count(&secCount)
|
||||
|
||||
Reference in New Issue
Block a user