41 lines
1.1 KiB
Go
41 lines
1.1 KiB
Go
package notify
|
|
|
|
import (
|
|
"context"
|
|
|
|
"git.huangwc.com/pig/pig-farm-controller/internal/infra/logs"
|
|
)
|
|
|
|
// logNotifier 实现了 Notifier 接口,用于将告警信息记录到日志中。
|
|
type logNotifier struct {
|
|
ctx context.Context
|
|
}
|
|
|
|
// NewLogNotifier 创建一个新的 logNotifier 实例。
|
|
// 它接收一个日志记录器,用于实际的日志输出。
|
|
func NewLogNotifier(ctx context.Context) Notifier {
|
|
return &logNotifier{
|
|
ctx: ctx,
|
|
}
|
|
}
|
|
|
|
// Send 将告警内容以结构化的方式记录到日志中。
|
|
// toAddr 参数在这里表示告警的预期接收者地址,也会被记录。
|
|
func (l *logNotifier) Send(ctx context.Context, content AlarmContent, toAddr string) error {
|
|
logger := logs.TraceLogger(ctx, l.ctx, "Send")
|
|
logger.Infow("告警已记录到日志",
|
|
"notifierType", NotifierTypeLog,
|
|
"title", content.Title,
|
|
"message", content.Message,
|
|
"level", content.Level.String(),
|
|
"timestamp", content.Timestamp.Format(DefaultTimeFormat),
|
|
"toAddr", toAddr,
|
|
)
|
|
return nil // 记录日志操作本身不应失败
|
|
}
|
|
|
|
// Type 返回通知器的类型。
|
|
func (l *logNotifier) Type() NotifierType {
|
|
return NotifierTypeLog
|
|
}
|