fix log wrapped incorrectly in WebUI, implement log SSR

This commit is contained in:
yusing
2025-01-23 00:08:19 +08:00
parent 2bb13129de
commit ed3b26653c
7 changed files with 291 additions and 39 deletions

View File

@@ -10,13 +10,15 @@ import (
"github.com/yusing/go-proxy/internal/utils/strutils"
)
var logger zerolog.Logger
func InitLogger(out io.Writer) {
var timeFmt string
var level zerolog.Level
var exclude []string
var (
logger zerolog.Logger
timeFmt string
level zerolog.Level
prefix string
prefixHTML []byte
)
func init() {
switch {
case common.IsTrace:
timeFmt = "04:05"
@@ -28,27 +30,37 @@ func InitLogger(out io.Writer) {
timeFmt = "01-02 15:04"
level = zerolog.InfoLevel
}
prefixLength := len(timeFmt) + 5 // level takes 3 + 2 spaces
prefix := strings.Repeat(" ", prefixLength)
prefix = strings.Repeat(" ", prefixLength)
// prefixHTML = []byte(strings.Repeat(" ", prefixLength))
prefixHTML = []byte(prefix)
logger = zerolog.New(
zerolog.ConsoleWriter{
Out: out,
TimeFormat: timeFmt,
FieldsExclude: exclude,
FormatMessage: func(msgI interface{}) string { // pad spaces for each line
msg := msgI.(string)
lines := strutils.SplitRune(msg, '\n')
if len(lines) == 1 {
return msg
}
for i := 1; i < len(lines); i++ {
lines[i] = prefix + lines[i]
}
return strutils.JoinRune(lines, '\n')
},
if zerolog.TraceLevel != -1 && zerolog.NoLevel != 6 {
panic("zerolog implementation changed")
}
}
func fmtMessage(msg string) string {
lines := strutils.SplitRune(msg, '\n')
if len(lines) == 1 {
return msg
}
for i := 1; i < len(lines); i++ {
lines[i] = prefix + lines[i]
}
return strutils.JoinRune(lines, '\n')
}
func InitLogger(out io.Writer) {
writer := zerolog.ConsoleWriter{
Out: out,
TimeFormat: timeFmt,
FormatMessage: func(msgI interface{}) string { // pad spaces for each line
return fmtMessage(msgI.(string))
},
}
logger = zerolog.New(
writer,
).Level(level).With().Timestamp().Logger()
}