114 lines
3.0 KiB
Go
114 lines
3.0 KiB
Go
|
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 }}" +
|
||
|
"{{index .Data \"hostname\"}}<span style=\"font-weight: bold; color: $color.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()
|
||
|
}
|