observer/pkg/gelf_exporter/gelf.go

49 lines
1.1 KiB
Go
Raw Normal View History

2024-04-26 13:09:54 +00:00
package gelfexporter
import (
2024-04-30 10:41:05 +00:00
"fmt"
2024-04-26 13:09:54 +00:00
"log"
"maal/tracer/pkg/level"
"time"
"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 level.SyslogLevel `json:"level"`
// 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)
}
2024-04-30 10:41:05 +00:00
fmt.Printf("msg: %v sent\n", msg)
2024-04-26 13:09:54 +00:00
}
}
func (g GELFMessage) GELFFormat() *gelf.Message {
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: g.ExtraFields,
}
}