gologger/format.go

85 lines
2.1 KiB
Go

// Package gologger provides a simple logging implementation with multiple appenders and formatters.
// It supports console, file, and HTTP logging with customizable formats.
package gologger
import (
"encoding/json"
"fmt"
"strings"
)
// LogFormatter is a function type that formats a LogEvent into a string.
// Example:
// formatter := func(event LogEvent) string {
// return fmt.Sprintf("[%s] %s: %v", event.Ts.Format("2006-01-02"), event.Level, event.Data)
// }
type LogFormatter = func(LogEvent) string
// format is the default formatter that converts a LogEvent to a string using the default template.
// It handles both simple values and formatted strings.
// Template: "[timestamp] level : category - data"
func format(logEvent LogEvent) string {
data := logEvent.Ts.Format("2006-01-02 15:04:05")
msg := ""
firstMsg := logEvent.Data[0]
if isFormatString(firstMsg) {
msg = fmt.Sprintf(firstMsg.(string), logEvent.Data[1:]...)
} else {
msg = sprint(logEvent.Data)
}
ret := fmt.Sprintf(logTemplate, data, logEvent.Category, getLogLevelStr(logEvent.Level), msg)
return ret
}
func getLogLevelStr(level int) string {
for name, slevel := range logLevelMap {
if slevel == level {
return strings.ToUpper(name)
}
}
return "Unknown"
}
func isFormatString(f interface{}) bool {
s, ok := f.(string)
if !ok {
return false
}
// 尝试使用空接口来格式化字符串
m := fmt.Sprintf(s, []interface{}{}...)
return strings.Contains(m, "MISSING")
}
func sprint(s []interface{}) string {
str := make([]any, len(s))
for i, v := range s {
if i > 0 {
str[i] = fmt.Sprintf(" %v", v)
} else {
str[i] = fmt.Sprintf("%v", v)
}
}
return fmt.Sprint(str...)
}
func jsonFormatter(logEvent LogEvent) string {
_, err := json.Marshal(logEvent.Data)
if err != nil {
logEvent.Data = []interface{}{fmt.Sprintf("%v", logEvent.Data)}
}
d, _ := json.Marshal(logEvent)
return string(d)
}
func SelectFormatter(formatter string) LogFormatter {
switch strings.ToLower(formatter) {
case "json":
return jsonFormatter
case "text":
return format
default:
return format
}
}