Skip to content

Commit

Permalink
✨ log caller pos
Browse files Browse the repository at this point in the history
  • Loading branch information
longhaoteng committed Apr 24, 2023
1 parent ae0399b commit ffa2589
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 35 deletions.
4 changes: 4 additions & 0 deletions _examples/cron/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,7 @@ func (t *Test) Run() {
func (t *Test) execute() {
log.Info("coon test")
}

func init() {
cron.AddJobs(&Test{})
}
30 changes: 30 additions & 0 deletions logger/default_hook.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package logger

import (
"fmt"
"path/filepath"
"runtime"

"github.com/sirupsen/logrus"
)

type defaultHook struct{}

func (d defaultHook) Levels() []logrus.Level {
return logrus.AllLevels
}

func (d defaultHook) Fire(e *logrus.Entry) error {
pos := "unknown"
_, file, line, ok := runtime.Caller(6)
if ok {
pos = fmt.Sprintf("%v:%v", filepath.Base(file), line)
}
e.Data["pos"] = pos

return nil
}

func init() {
AddHook(&defaultHook{})
}
18 changes: 9 additions & 9 deletions logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,39 +66,39 @@ func Fields(keysAndValues ...interface{}) *Logger {
}

func Log(level Level, v ...interface{}) {
log.entry.Log(level, v...)
log.entry.WithFields(logrus.Fields{}).Log(level, v...)
}

func Trace(args ...interface{}) {
log.entry.Trace(args...)
log.entry.WithFields(logrus.Fields{}).Log(TraceLevel, args...)
}

func Debug(args ...interface{}) {
log.entry.Debug(args...)
log.entry.WithFields(logrus.Fields{}).Log(DebugLevel, args...)
}

func Info(args ...interface{}) {
log.entry.Info(args...)
log.entry.WithFields(logrus.Fields{}).Log(InfoLevel, args...)
}

func Warn(args ...interface{}) {
log.entry.Warn(args...)
log.entry.WithFields(logrus.Fields{}).Log(WarnLevel, args...)
}

func Error(args ...interface{}) {
log.entry.Error(args...)
log.entry.WithFields(logrus.Fields{}).Log(ErrorLevel, args...)
}

func Fatal(args ...interface{}) {
log.entry.Fatal(args...)
log.entry.WithFields(logrus.Fields{}).Log(FatalLevel, args...)
}

func Panic(args ...interface{}) {
log.entry.Panic(args...)
log.entry.WithFields(logrus.Fields{}).Log(PanicLevel, args...)
}

func Logf(level Level, format string, v ...interface{}) {
log.entry.Logf(level, format, v...)
log.entry.WithFields(logrus.Fields{}).Logf(level, format, v...)
}

func (l *Logger) Log(level Level, v ...interface{}) {
Expand Down
29 changes: 3 additions & 26 deletions logger/logrus.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package logger

import (
"fmt"
"os"
"runtime"
"strings"

"github.com/longhaoteng/wineglass/config"
"github.com/longhaoteng/wineglass/consts/timef"
Expand Down Expand Up @@ -52,28 +49,8 @@ func GetEntry() *logrus.Entry {
return log.entry
}

func addHook(level logrus.Level, hks ...logrus.Hook) {
log.hooks[level] = hks
}

func caller(skip int) func(f *runtime.Frame) (string, string) {
return func(f *runtime.Frame) (string, string) {
_, file, line, ok := runtime.Caller(skip)
fileline := "unknown"
if ok {
filePath := strings.ReplaceAll(file, fmt.Sprintf("%s/pkg/mod/", os.Getenv("GOPATH")), "")
// 去除路径中版本号
versionIndex := strings.Index(filePath, "@")
if versionIndex != -1 {
subPath := filePath[versionIndex:]
version := subPath[:strings.Index(subPath, "/")]
filePath = strings.ReplaceAll(filePath, version, "")
}
fileline = fmt.Sprintf("%v:%v", filePath, line)
if config.IsDevEnv() {
fileline += "\t"
}
}
return "", fileline
func AddHook(hook logrus.Hook) {
for _, level := range hook.Levels() {
log.hooks[level] = append(log.hooks[level], hook)
}
}

0 comments on commit ffa2589

Please sign in to comment.