58 lines
1.3 KiB
Go
58 lines
1.3 KiB
Go
package gelfexporter
|
|
|
|
import (
|
|
"log"
|
|
|
|
"time"
|
|
|
|
"git.ma-al.com/maal-libraries/observer/pkg/syslog"
|
|
"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"`
|
|
|
|
// All additional field names must start with an underline.
|
|
ExtraFields map[string]interface{} `json:"extrafields,omitempty"`
|
|
|
|
Tag string `json:"tag,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!")
|
|
}
|
|
}
|
|
|
|
func (g GELFMessage) GELFFormat() *gelf.Message {
|
|
prefixedExtras := make(map[string]interface{}, len(g.ExtraFields))
|
|
prefixedExtras["tag"] = g.Tag
|
|
for k, v := range g.ExtraFields {
|
|
prefixedExtras["_"+k] = v
|
|
}
|
|
|
|
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,
|
|
}
|
|
}
|