Files
upkg/logger/formatter.go
2026-01-17 17:27:33 +08:00

78 lines
1.4 KiB
Go

package logger
import (
"encoding/json"
"fmt"
"io"
"strings"
"time"
)
type Formatter interface {
Format(writer io.Writer, entry *Entry)
}
type textFormatter struct {
timeFormat string
prefix string
}
type jsonFormatter struct{}
func newFormatter(format Format) Formatter {
switch format {
case JSON:
return &jsonFormatter{}
default:
return &textFormatter{
timeFormat: "2006-01-02 15:04:05",
}
}
}
func (f *textFormatter) Format(writer io.Writer, entry *Entry) {
parts := make([]string, 0, 8)
parts = append(parts, entry.Time.Format(f.timeFormat))
parts = append(parts, entry.Level.String())
if f.prefix != "" {
parts = append(parts, "["+f.prefix+"]")
}
if entry.Caller != "" {
parts = append(parts, entry.Caller)
}
parts = append(parts, entry.Message)
if len(entry.Fields) > 0 {
fieldStrs := make([]string, 0, len(entry.Fields))
for k, v := range entry.Fields {
fieldStrs = append(fieldStrs, fmt.Sprintf("%s=%v", k, v))
}
parts = append(parts, strings.Join(fieldStrs, " "))
}
fmt.Fprintln(writer, strings.Join(parts, " "))
}
func (f *jsonFormatter) Format(writer io.Writer, entry *Entry) {
m := map[string]interface{}{
"time": entry.Time.Format(time.RFC3339),
"level": entry.Level.String(),
"message": entry.Message,
}
if entry.Caller != "" {
m["caller"] = entry.Caller
}
for k, v := range entry.Fields {
m[k] = v
}
data, _ := json.Marshal(m)
writer.Write(data)
writer.Write([]byte("\n"))
}