Skip to content

Commit

Permalink
优化日志输出格式
Browse files Browse the repository at this point in the history
  • Loading branch information
duanhf2012 committed Sep 1, 2023
1 parent fa8cbfb commit 2d1bee4
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 27 deletions.
23 changes: 12 additions & 11 deletions log/handler.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package log

import (
"log/slog"
"context"
"io"
"log/slog"
"path/filepath"
"context"
"runtime"
"runtime/debug"
"sync"
Expand Down Expand Up @@ -35,21 +35,21 @@ type OriginJsonHandler struct {
func getStrLevel(level slog.Level) string{
switch level {
case LevelTrace:
return "TRACE"
return "Trace"
case LevelDebug:
return "DEBUG"
return "Debug"
case LevelInfo:
return "INFO"
return "Info"
case LevelWarning:
return "WARNING"
return "Warning"
case LevelError:
return "ERROR"
return "Error"
case LevelStack:
return "STACK"
return "Stack"
case LevelDump:
return "DUMP"
return "Dump"
case LevelFatal:
return "FATAL"
return "Fatal"
}

return ""
Expand Down Expand Up @@ -97,7 +97,8 @@ func (oh *OriginTextHandler) Handle(context context.Context, record slog.Record)
oh.w.Write([]byte(strDump))
return err
}



return oh.TextHandler.Handle(context, record)
}

Expand Down
17 changes: 1 addition & 16 deletions log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -489,23 +489,8 @@ func (logger *Logger) formatHeader(buf *Buffer,level slog.Level,calldepth int) {
file = filepath.Base(file)

buf.AppendString("time=\"")
year, month, day := t.Date()
buf.AppendInt(int64(year))
buf.AppendByte('/')
buf.AppendInt(int64(month))
buf.AppendByte('/')
buf.AppendInt(int64(day))
buf.AppendByte(' ')

hour, min, sec := t.Clock()
buf.AppendInt(int64(hour))
buf.AppendByte(':')
buf.AppendInt(int64(min))
buf.AppendByte(':')

buf.AppendInt(int64(sec))
buf.AppendString(t.Format("2006/01/02 15:04:05"))
buf.AppendString("\"")

logger.sBuff.AppendString(" level=")
logger.sBuff.AppendString(getStrLevel(level))
logger.sBuff.AppendString(" source=")
Expand Down

0 comments on commit 2d1bee4

Please sign in to comment.