try color

This commit is contained in:
Martin/Geno 2019-06-21 00:46:35 +02:00
parent 6d1bc2341a
commit 346f3544d5
No known key found for this signature in database
GPG Key ID: 9D7D3C6BFF600C6A
3 changed files with 148 additions and 23 deletions

View File

@ -41,7 +41,7 @@ func NewPriority(db *database.DB) *Command {
n.MaxPrioIn = max n.MaxPrioIn = max
return fmt.Sprintf("set filter for %s to %d", to, max) return fmt.Sprintf("set filter for %s to %s", to, log.LevelString(max))
}, },
}, },
{ {
@ -50,7 +50,7 @@ func NewPriority(db *database.DB) *Command {
Action: func(from string, params []string) string { Action: func(from string, params []string) string {
msg := "priority: \n" msg := "priority: \n"
for _, n := range db.Notifies { for _, n := range db.Notifies {
msg = fmt.Sprintf("%s%s - %d\n", msg, n.Address(), n.MaxPrioIn) msg = fmt.Sprintf("%s%s - %s\n", msg, n.Address(), log.LevelString(n.MaxPrioIn))
} }
return msg return msg
}, },
@ -65,7 +65,7 @@ func NewPriority(db *database.DB) *Command {
of := params[0] of := params[0]
msg := "priority: \n" msg := "priority: \n"
if notify, ok := db.NotifiesByAddress[of]; ok { if notify, ok := db.NotifiesByAddress[of]; ok {
msg = fmt.Sprintf("%s %s is %d", msg, of, notify.MaxPrioIn) msg = fmt.Sprintf("%s %s is %s", msg, of, log.LevelString(notify.MaxPrioIn))
} }
return msg return msg
}, },
@ -74,7 +74,7 @@ func NewPriority(db *database.DB) *Command {
Action: func(from string, params []string) string { Action: func(from string, params []string) string {
msg := "priority: \n" msg := "priority: \n"
if notify, ok := db.NotifiesByAddress[from]; ok { if notify, ok := db.NotifiesByAddress[from]; ok {
msg = fmt.Sprintf("%s %s is %d", msg, from, notify.MaxPrioIn) msg = fmt.Sprintf("%s %s is %s", msg, from, log.LevelString(notify.MaxPrioIn))
} }
return msg return msg
}, },

126
output/xmpp/format.go Normal file
View File

@ -0,0 +1,126 @@
package xmpp
import (
"bytes"
"fmt"
"text/template"
"github.com/bdlm/log"
)
var tempLog = template.Must(template.New("log").Parse(
"{{$color := .Color}}<span>" +
// Hostname
"{{ if .Hostname }}<span style=\"color: {{ $color.Hostname }};\">{{ .Hostname}}</span>{{ end }}" +
// Level
"<span style=\"font-weight: bold; color: {{$color.Level}};\">{{printf \" %5s\" .Level}}</span>" +
// Message
"{{printf \" %s\" .Message}}" +
// Data
"{{if .Data}}{{range $k, $v := .Data}}" +
"<span style=\"color: {{$color.DataLabel}};\">{{printf \" %s\" $k}}</span>" +
"=" +
"<span style=\"color: {{$color.DataValue}};\">{{$v}}</span>" +
"{{end}}{{end}}" +
"</span>",
))
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, 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: log.LevelString(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,
}
for k, v := range entry.Data {
if k == "hostname" {
if data.Hostname == "" {
data.Hostname = v.(string)
}
continue
}
if str, ok := v.(string); ok {
data.Data[k] = "'" + str + "'"
} else {
data.Data[k] = v
}
}
if err := tempLog.Execute(logLine, data); err != nil {
return "formating error", "formating error"
}
return logLine.String(), fmt.Sprintf("%s> %s: %s", data.Hostname, log.LevelString(entry.Level), entry.Message)
}

View File

@ -1,6 +1,7 @@
package xmpp package xmpp
import ( import (
"encoding/xml"
"regexp" "regexp"
"strings" "strings"
@ -56,15 +57,8 @@ func Init(configInterface interface{}, db *database.DB, bot *bot.Bot) output.Out
return nil return nil
} }
go func() { go func() {
for { client.Start()
if err := client.Start(); err != nil { log.Panic("closed connection")
log.Warn("close connection, try reconnect")
client.Connect(config.Password)
} else {
log.Warn("closed connection")
return
}
}
}() }()
go func() { go func() {
for { for {
@ -190,12 +184,17 @@ func (out *Output) Default() []*database.Notify {
} }
func (out *Output) Send(e *log.Entry, to *database.Notify) bool { func (out *Output) Send(e *log.Entry, to *database.Notify) bool {
textByte, err := out.formatter.Format(e) html, text := formatLog(e)
if err != nil { if html == "" || text == "" {
logger.Error("during format notify", err) logger.Error("during format notify")
return false return false
} }
text := strings.TrimRight(to.RunReplace(string(textByte)), "\n") html = strings.TrimRight(to.RunReplace(html), "\n")
var body xmpp.XMLElement
xml.Unmarshal([]byte(html), &body)
text = strings.TrimRight(to.RunReplace(text), "\n")
if to.Protocol == protoGroup { if to.Protocol == protoGroup {
if _, ok := out.channels[to.To]; ok { if _, ok := out.channels[to.To]; ok {
toJID := xmppbase.NewJID(to.To) toJID := xmppbase.NewJID(to.To)
@ -214,23 +213,23 @@ func (out *Output) Send(e *log.Entry, to *database.Notify) bool {
out.channels[to.To] = true out.channels[to.To] = true
} }
} }
err := out.client.Send(&xmpp.MessageClient{ if err := out.client.Send(&xmpp.MessageClient{
Type: xmpp.MessageTypeGroupchat, Type: xmpp.MessageTypeGroupchat,
To: xmppbase.NewJID(to.To), To: xmppbase.NewJID(to.To),
Body: text, Body: text,
}) HTML: &xmpp.HTML{Body: xmpp.HTMLBody{Body: body}},
if err != nil { }); err != nil {
logger.Error("xmpp to ", to.To, " error:", err) logger.Error("xmpp to ", to.To, " error:", err)
} }
return true return true
} }
if to.Protocol == proto { if to.Protocol == proto {
err := out.client.Send(&xmpp.MessageClient{ if err := out.client.Send(&xmpp.MessageClient{
Type: xmpp.MessageTypeChat, Type: xmpp.MessageTypeChat,
To: xmppbase.NewJID(to.To), To: xmppbase.NewJID(to.To),
Body: text, Body: text,
}) HTML: &xmpp.HTML{Body: xmpp.HTMLBody{Body: body}},
if err != nil { }); err != nil {
logger.Error("xmpp to ", to, " error:", err) logger.Error("xmpp to ", to, " error:", err)
} }
return true return true