Skip to content

Commit

Permalink
add caller
Browse files Browse the repository at this point in the history
  • Loading branch information
Xib1uvXi committed Dec 18, 2019
1 parent bf44f95 commit ec4f97a
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
7 changes: 6 additions & 1 deletion log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"runtime"
"strconv"
"sync"
"time"
)

var QyLogger *zap.Logger
Expand Down Expand Up @@ -66,7 +67,7 @@ func newLogCore(lvl zapcore.Level, targetDir, logFileName string, withConsole bo

eConfig := zap.NewProductionEncoderConfig()
eConfig.EncodeDuration = zapcore.SecondsDurationEncoder
eConfig.EncodeTime = zapcore.ISO8601TimeEncoder
eConfig.EncodeTime = qyTimeEncoder

if withCaller {
eConfig.EncodeCaller = qyCallerEncoder
Expand Down Expand Up @@ -159,6 +160,10 @@ func getErrOutSink(outputPath string, withConsole bool) zapcore.WriteSyncer {
return getSink(outputPath, []string{"stdout", "stderr"}, withConsole)
}

func qyTimeEncoder(t time.Time, enc zapcore.PrimitiveArrayEncoder) {
enc.AppendString(t.Format("2006-01-02 15:04:05.000"))
}

func qyCallerEncoder(caller zapcore.EntryCaller, enc zapcore.PrimitiveArrayEncoder) {
enc.AppendString(printHostName() + " " + printMyName())
}
Expand Down
2 changes: 1 addition & 1 deletion log/log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func Test_pathExists(t *testing.T) {
}

func TestTrace(t *testing.T) {
InitLogger(zap.DebugLevel, "", "", true)
InitLoggerWithCaller(zap.DebugLevel, "", "", true)

QyLogger.Info("aaaaaaa")

Expand Down

0 comments on commit ec4f97a

Please sign in to comment.