From 363aad27d35c3775c7c31ac9b68b1e05dd9762dc Mon Sep 17 00:00:00 2001 From: Martin/Geno Date: Fri, 21 Jun 2019 00:46:35 +0200 Subject: [PATCH] try color --- output/xmpp/format.go | 113 ++++++++++++++++++++++++++++++++++++++++++ output/xmpp/main.go | 8 +-- 2 files changed, 117 insertions(+), 4 deletions(-) create mode 100644 output/xmpp/format.go diff --git a/output/xmpp/format.go b/output/xmpp/format.go new file mode 100644 index 0000000..afb313b --- /dev/null +++ b/output/xmpp/format.go @@ -0,0 +1,113 @@ +package xmpp + +import ( + "bytes" + "text/template" + + "github.com/bdlm/log" +) + +var tempLog = template.Must(template.New("log").Parse( + "{{$color := .Color}}" + + // Hostname + "{{if .Hostname}}{{ .Hostname}}{{ end }}" + + "{{$hostname := index .Data \"hostname\"}}{{if $hostname}}{{$hostname}}{{ end }}" + + // Level + "{{printf \"%5s\" .Level}}" + + // Message + "{{printf \"%s\" .Message}}" + + // Data + "{{if .Data}}{{range $k, $v := .Data}}{{ if ne $k \"hostname\"}}" + + "{{$k}}" + + "=" + + "{{$v}}" + + "{{end}}{{end}}{{end}}" + + "", +)) + +var ( + // DEFAULTColor is the default html 'level' color. + DEFAULTColor = "#00ff00" + // ERRORColor is the html 'level' color for error messages. + ERRORColor = "#ff8700" + // FATALColor is the html 'level' color for fatal messages. + FATALColor = "#af0000" + // PANICColor is the html 'level' color for panic messages. + PANICColor = "#ff0000" + // WARNColor is the html 'level' color for warning messages. + WARNColor = "#ffff00" + // DEBUGColor is the html 'level' color for debug messages. + DEBUGColor = "#8a8a8a" + + // DataLabelColor is the html data label color. + DataLabelColor = "#87afff" + // DataValueColor is the html data value color. + DataValueColor = "#d7af87" + // HostnameColor is the html hostname color. + HostnameColor = "#00afff" + // TimestampColor is the html timestamp color. + TimestampColor = "#5faf87" +) + +type logData struct { + Color colors `json:"-"` + Data map[string]interface{} `json:"data,omitempty"` + Hostname string `json:"host,omitempty"` + Level string `json:"level,omitempty"` + Message string `json:"msg,omitempty"` + Timestamp string `json:"time,omitempty"` +} + +type colors struct { + DataLabel string + DataValue string + Hostname string + Level string + Reset string + Timestamp string +} + +func formatLog(entry *log.Entry) string { + var levelColor string + + var logLine *bytes.Buffer + if entry.Buffer != nil { + logLine = entry.Buffer + } else { + logLine = &bytes.Buffer{} + } + + data := &logData{ + Data: make(map[string]interface{}), + Level: string(entry.Level), + Message: entry.Message, + Timestamp: entry.Time.Format(log.RFC3339Milli), + } + switch entry.Level { + case log.DebugLevel: + levelColor = DEBUGColor + case log.WarnLevel: + levelColor = WARNColor + case log.ErrorLevel: + levelColor = ERRORColor + case log.FatalLevel: + levelColor = FATALColor + case log.PanicLevel: + levelColor = PANICColor + default: + levelColor = DEFAULTColor + } + data.Color = colors{ + DataLabel: DataLabelColor, + DataValue: DataValueColor, + Hostname: HostnameColor, + Level: levelColor, + Timestamp: TimestampColor, + } + + if err := tempLog.Execute(logLine, data); err != nil { + return "formating error" + } + + return logLine.String() +} diff --git a/output/xmpp/main.go b/output/xmpp/main.go index 7583b62..efd72db 100644 --- a/output/xmpp/main.go +++ b/output/xmpp/main.go @@ -190,12 +190,12 @@ func (out *Output) Default() []*database.Notify { } func (out *Output) Send(e *log.Entry, to *database.Notify) bool { - textByte, err := out.formatter.Format(e) - if err != nil { - logger.Error("during format notify", err) + text := formatLog(e) + if text == "" { + logger.Error("during format notify") return false } - text := strings.TrimRight(to.RunReplace(string(textByte)), "\n") + text = strings.TrimRight(to.RunReplace(text), "\n") if to.Protocol == protoGroup { if _, ok := out.channels[to.To]; ok { toJID := xmppbase.NewJID(to.To)