50 lines
1.7 KiB
Go
50 lines
1.7 KiB
Go
// Copyright 2025. All rights reserved.
|
||
|
||
package serverconfigs
|
||
|
||
import "encoding/json"
|
||
|
||
// AccessLogWriteTargets 访问日志写入目标(双写/单写:文件、MySQL、ClickHouse)
|
||
type AccessLogWriteTargets struct {
|
||
File bool `yaml:"file" json:"file"` // 写本地 JSON 文件(供 Fluent Bit → ClickHouse 或自用)
|
||
MySQL bool `yaml:"mysql" json:"mysql"` // 写 MySQL 默认库按日分表
|
||
ClickHouse bool `yaml:"clickhouse" json:"clickhouse"` // 需要落 ClickHouse(文件+Fluent Bit 或 API 直写)
|
||
}
|
||
|
||
// NeedReportToAPI 是否需要上报到 API(写 MySQL 或 API 直写 ClickHouse 时需要)
|
||
func (t *AccessLogWriteTargets) NeedReportToAPI() bool {
|
||
if t == nil {
|
||
return true // 兼容:未配置时保持原行为,上报
|
||
}
|
||
return t.MySQL || t.ClickHouse
|
||
}
|
||
|
||
// NeedWriteFile 节点是否需要写本地文件
|
||
func (t *AccessLogWriteTargets) NeedWriteFile() bool {
|
||
if t == nil {
|
||
return true // 兼容:未配置时保持原行为,写文件
|
||
}
|
||
return t.File
|
||
}
|
||
|
||
// ParseWriteTargetsFromPolicy 从策略的 writeTargets JSON 与旧字段解析;无 writeTargets 时按 type + disableDefaultDB 推断
|
||
func ParseWriteTargetsFromPolicy(writeTargetsJSON []byte, policyType string, disableDefaultDB bool) *AccessLogWriteTargets {
|
||
if len(writeTargetsJSON) > 0 {
|
||
var t AccessLogWriteTargets
|
||
if err := json.Unmarshal(writeTargetsJSON, &t); err == nil {
|
||
return &t
|
||
}
|
||
}
|
||
// 兼容旧策略:type=file 视为写文件,!disableDefaultDB 视为写 MySQL
|
||
t := &AccessLogWriteTargets{
|
||
File: policyType == AccessLogStorageTypeFile,
|
||
MySQL: !disableDefaultDB,
|
||
ClickHouse: false,
|
||
}
|
||
if !t.File && !t.MySQL && !t.ClickHouse {
|
||
t.File = true
|
||
t.MySQL = true
|
||
}
|
||
return t
|
||
}
|