observer/pkg/exporters/gelf_exporter/gelf.go

55 lines
1.3 KiB
Go
Raw Normal View History

2024-04-26 13:09:54 +00:00
package gelfexporter
import (
"log"
2024-04-30 12:42:05 +00:00
2024-04-26 13:09:54 +00:00
"time"
"git.ma-al.com/maal-libraries/observer/pkg/syslog"
2024-04-26 13:09:54 +00:00
"gopkg.in/Graylog2/go-gelf.v2/gelf"
)
type GELFMessage struct {
// Name of the application
Host string `json:"host"`
// Short, descriptive message
ShortMessage string `json:"short_message"`
// Optional long message.
LongMessage string `json:"long_message,omitempty"`
// Timestamp in Unix
Timestamp time.Time `json:"timestamp"`
// Severity level matching Syslog standard.
Level syslog.SyslogLevel `json:"level"`
2024-04-26 13:09:54 +00:00
// All additional field names must start with an underline.
ExtraFields map[string]interface{} `json:"extrafields,omitempty"`
}
func Log(writer *gelf.UDPWriter, msg GELFMessage) {
if writer != nil {
err := writer.WriteMessage(msg.GELFFormat())
if err != nil {
log.Println(err)
}
} else {
log.Fatalln("gelf.UDPWriter is not set!")
2024-04-26 13:09:54 +00:00
}
}
func (g GELFMessage) GELFFormat() *gelf.Message {
prefixedExtras := make(map[string]interface{}, len(g.ExtraFields))
for k, v := range g.ExtraFields {
prefixedExtras["_"+k] = v
}
2024-04-26 13:09:54 +00:00
return &gelf.Message{
Version: "1.1",
Host: g.Host,
Short: g.ShortMessage,
Full: g.LongMessage,
TimeUnix: float64(g.Timestamp.Unix()),
Level: int32(g.Level),
Extra: prefixedExtras,
2024-04-26 13:09:54 +00:00
}
}