try color
This commit is contained in:
parent
6d1bc2341a
commit
2d2f8e061f
|
@ -0,0 +1,113 @@
|
|||
package xmpp
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"text/template"
|
||||
|
||||
"github.com/bdlm/log"
|
||||
)
|
||||
|
||||
var tempLog = template.Must(template.New("log").Parse(
|
||||
"{{$color := .Color}}<span>" +
|
||||
// Hostname
|
||||
"{{if .Hostname}}<span style=\"font-weight: bold; color: $color.Hostname;\">{{ .Hostname}}</span>{{ end }}" +
|
||||
"{{$hostname := index .Data \"hostname\"}}{{if $hostname}}<span style=\"font-weight: bold; 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}}{{ if $k != \"hostname\"}}" +
|
||||
"<span style=\"color: {{$color.DataLabel}};\">{{$k}}</span>" +
|
||||
"=" +
|
||||
"<span style=\"color: {{$color.DataValue}};\">{{$v}}</span>" +
|
||||
"{{end}}{{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 {
|
||||
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()
|
||||
}
|
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue