优化抖动幅度和日志双时间显示
- jitterDuration 抖动从 base/3 降至 base/10,避免过大的随机延迟 - 日志新增斗鱼双时间显示:本地时间后追加 (D斗鱼时间),前端蓝色高亮 - 时间校准后自动同步斗鱼偏移量到日志模块
This commit is contained in:
@@ -23,11 +23,18 @@ const (
|
||||
// minLevel 日志最低等级,低于此等级的日志不输出。0=info, 1=warn, 2=error。
|
||||
var minLevel atomic.Int32
|
||||
|
||||
var douyuOffset atomic.Int64
|
||||
|
||||
// SetMinLevel 设置日志最低显示等级。
|
||||
func SetMinLevel(level string) {
|
||||
minLevel.Store(int32(levelPriority(level)))
|
||||
}
|
||||
|
||||
// SetDouyuOffset 设置斗鱼服务端时间与本地时间的偏移量,用于日志双时间显示。
|
||||
func SetDouyuOffset(offset time.Duration) {
|
||||
douyuOffset.Store(int64(offset))
|
||||
}
|
||||
|
||||
func levelPriority(level string) int {
|
||||
switch strings.ToLower(level) {
|
||||
case "debug":
|
||||
@@ -192,10 +199,19 @@ func logStructuredEvent(category, level, account, format string, args ...any) {
|
||||
|
||||
func formatLogLine(now time.Time, category, level, account, message string) string {
|
||||
ts := now.In(constants.CST).Format(logTimeLayout)
|
||||
if account != "" {
|
||||
return fmt.Sprintf("%s [%s][%s][账号:%s] %s", ts, category, level, account, message)
|
||||
offset := time.Duration(douyuOffset.Load())
|
||||
var prefix string
|
||||
if offset != 0 {
|
||||
dyTime := now.Add(offset)
|
||||
prefix = fmt.Sprintf("%s(D%02d:%02d:%02d.%03d) ", ts,
|
||||
dyTime.Hour(), dyTime.Minute(), dyTime.Second(), dyTime.Nanosecond()/1e6)
|
||||
} else {
|
||||
prefix = ts + " "
|
||||
}
|
||||
return fmt.Sprintf("%s [%s][%s] %s", ts, category, level, message)
|
||||
if account != "" {
|
||||
return fmt.Sprintf("%s[%s][%s][账号:%s] %s", prefix, category, level, account, message)
|
||||
}
|
||||
return fmt.Sprintf("%s[%s][%s] %s", prefix, category, level, message)
|
||||
}
|
||||
|
||||
// LogMeta 是从日志文本中解析出的结构化信息。
|
||||
|
||||
@@ -644,8 +644,7 @@ func jitterDuration(base time.Duration) time.Duration {
|
||||
if base <= 0 {
|
||||
return 0
|
||||
}
|
||||
// 加一点抖动,避免所有机器严格同频请求。
|
||||
jitter := time.Duration(rand.Int63n(int64(base / 3)))
|
||||
jitter := time.Duration(rand.Int63n(int64(base / 10)))
|
||||
return base + jitter
|
||||
}
|
||||
|
||||
@@ -858,6 +857,7 @@ func (s *Scheduler) calibrateDouyuTime(label string) types.TimeCalibration {
|
||||
if err != nil {
|
||||
logger.LogEvent("时间", "warn", "%s校准降级: %v", label, err)
|
||||
}
|
||||
logger.SetDouyuOffset(calibration.Offset)
|
||||
logger.LogEvent("时间", "success", "%s校准完成: 斗鱼估算=%s 偏移=%dms 误差<=%dms RTT=%dms 采样=%d 来源=%s",
|
||||
label,
|
||||
calibration.EstimatedServerNow().In(constants.CST).Format("2006-01-02 15:04:05.000"),
|
||||
|
||||
Reference in New Issue
Block a user