日志发送逻辑及测试消息发送接口
This commit is contained in:
@@ -41,6 +41,9 @@ type Config struct {
|
||||
|
||||
// LoraMesh LoraMesh配置
|
||||
LoraMesh LoraMeshConfig `yaml:"lora_mesh"`
|
||||
|
||||
// Notify 通知服务配置
|
||||
Notify NotifyConfig `yaml:"notify"`
|
||||
}
|
||||
|
||||
// AppConfig 代表应用基础配置
|
||||
@@ -158,6 +161,40 @@ type LoraMeshConfig struct {
|
||||
ReassemblyTimeout int `yaml:"reassembly_timeout"`
|
||||
}
|
||||
|
||||
// NotifyConfig 包含了所有与通知服务相关的配置
|
||||
type NotifyConfig struct {
|
||||
Primary string `yaml:"primary"` // 首选通知渠道 (e.g., "smtp", "wechat", "lark", "log")
|
||||
FailureThreshold int `yaml:"failureThreshold"` // 连续失败多少次后触发广播模式
|
||||
SMTP SMTPConfig `yaml:"smtp"`
|
||||
WeChat WeChatConfig `yaml:"wechat"`
|
||||
Lark LarkConfig `yaml:"lark"`
|
||||
}
|
||||
|
||||
// SMTPConfig SMTP邮件配置
|
||||
type SMTPConfig struct {
|
||||
Enabled bool `yaml:"enabled"`
|
||||
Host string `yaml:"host"`
|
||||
Port int `yaml:"port"`
|
||||
Username string `yaml:"username"`
|
||||
Password string `yaml:"password"`
|
||||
Sender string `yaml:"sender"`
|
||||
}
|
||||
|
||||
// WeChatConfig 企业微信应用配置
|
||||
type WeChatConfig struct {
|
||||
Enabled bool `yaml:"enabled"`
|
||||
CorpID string `yaml:"corpID"`
|
||||
AgentID string `yaml:"agentID"`
|
||||
Secret string `yaml:"secret"`
|
||||
}
|
||||
|
||||
// LarkConfig 飞书应用配置
|
||||
type LarkConfig struct {
|
||||
Enabled bool `yaml:"enabled"`
|
||||
AppID string `yaml:"appID"`
|
||||
AppSecret string `yaml:"appSecret"`
|
||||
}
|
||||
|
||||
// NewConfig 创建并返回一个新的配置实例
|
||||
func NewConfig() *Config {
|
||||
// 默认值可以在这里设置,但我们优先使用配置文件中的值
|
||||
|
||||
@@ -5,7 +5,6 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
37
internal/infra/notify/log_notifier.go
Normal file
37
internal/infra/notify/log_notifier.go
Normal file
@@ -0,0 +1,37 @@
|
||||
package notify
|
||||
|
||||
import (
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/infra/logs"
|
||||
)
|
||||
|
||||
// logNotifier 实现了 Notifier 接口,用于将告警信息记录到日志中。
|
||||
type logNotifier struct {
|
||||
logger *logs.Logger
|
||||
}
|
||||
|
||||
// NewLogNotifier 创建一个新的 logNotifier 实例。
|
||||
// 它接收一个日志记录器,用于实际的日志输出。
|
||||
func NewLogNotifier(logger *logs.Logger) Notifier {
|
||||
return &logNotifier{
|
||||
logger: logger,
|
||||
}
|
||||
}
|
||||
|
||||
// Send 将告警内容以结构化的方式记录到日志中。
|
||||
// toAddr 参数在这里表示告警的预期接收者地址,也会被记录。
|
||||
func (l *logNotifier) Send(content AlarmContent, toAddr string) error {
|
||||
l.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
|
||||
}
|
||||
@@ -14,11 +14,13 @@ type NotifierType string
|
||||
|
||||
const (
|
||||
// NotifierTypeSMTP 表示 SMTP 邮件通知器。
|
||||
NotifierTypeSMTP NotifierType = "邮件"
|
||||
NotifierTypeSMTP NotifierType = "smtp"
|
||||
// NotifierTypeWeChat 表示企业微信通知器。
|
||||
NotifierTypeWeChat NotifierType = "企业微信"
|
||||
NotifierTypeWeChat NotifierType = "wechat"
|
||||
// NotifierTypeLark 表示飞书通知器。
|
||||
NotifierTypeLark NotifierType = "飞书"
|
||||
NotifierTypeLark NotifierType = "lark"
|
||||
// NotifierTypeLog 表示日志通知器,作为最终的告警记录渠道。
|
||||
NotifierTypeLog NotifierType = "log"
|
||||
)
|
||||
|
||||
// AlarmContent 定义了通知的内容
|
||||
|
||||
Reference in New Issue
Block a user