Skip to content

Commit

Permalink
调整日志格式
Browse files Browse the repository at this point in the history
  • Loading branch information
steden committed Oct 26, 2023
1 parent cb4f1a8 commit 09c5a63
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 33 deletions.
31 changes: 0 additions & 31 deletions flog/compositionLogger.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ import (
"fmt"
"github.com/farseer-go/fs/configure"
"github.com/farseer-go/fs/core/eumLogLevel"
"runtime"
"strconv"
"strings"
)

// CompositionLogger 根据已添加的Provider,创建组合模式的Logger(壳)
Expand Down Expand Up @@ -56,16 +53,12 @@ func (r *CompositionLogger) Warningf(format string, a ...any) {
}

func (r *CompositionLogger) Error(contents ...any) error {
r.log(newLogData(eumLogLevel.NoneLevel, Blue(fileWithLineNum()), ""))

log := newLogData(eumLogLevel.Error, fmt.Sprint(contents...), "")
r.log(log)
return fmt.Errorf(TextFormatter{}.Formatter(log))
}

func (r *CompositionLogger) Errorf(format string, a ...any) error {
r.log(newLogData(eumLogLevel.NoneLevel, Blue(fileWithLineNum()), ""))

log := newLogData(eumLogLevel.Error, fmt.Sprintf(format, a...), "")
r.log(log)
return fmt.Errorf(TextFormatter{}.Formatter(log))
Expand All @@ -89,27 +82,3 @@ func (r *CompositionLogger) Log(logLevel eumLogLevel.Enum, content string, compo
log.newLine = newLine
r.log(log)
}

var ComNames = []string{"/farseer-go/async/", "/farseer-go/cache/", "/farseer-go/cacheMemory/", "/farseer-go/collections/", "/farseer-go/data/", "/farseer-go/elasticSearch/", "/farseer-go/etcd/", "/farseer-go/eventBus/", "/farseer-go/fs/", "/farseer-go/linkTrace/", "/farseer-go/mapper/", "/farseer-go/queue/", "/farseer-go/rabbit/", "/farseer-go/redis/", "/farseer-go/redisStream/", "/farseer-go/tasks/", "/farseer-go/utils/", "/farseer-go/webapi/", "/src/reflect/", "/usr/local/go/src/"}

func IsSysCom(file string) bool {
for _, comName := range ComNames {
if strings.Contains(file, comName) {
return true
}
}
return false
}

func fileWithLineNum() string {
// the second caller usually from internal, so set i start from 1
var fileLineNum string
for i := 3; i < 15; i++ {
_, file, line, ok := runtime.Caller(i)
if ok && !strings.HasSuffix(file, "_test.go") && (!IsSysCom(file) || strings.HasSuffix(file, "healthCheck.go")) { // !strings.HasPrefix(file, gormSourceDir) ||
fileLineNum = file + ":" + strconv.FormatInt(int64(line), 10)
break
}
}
return fileLineNum
}
14 changes: 12 additions & 2 deletions trace/baseTraceDetail.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package trace
import (
"github.com/farseer-go/collections"
"github.com/farseer-go/fs/asyncLocal"
"github.com/farseer-go/fs/flog"
"github.com/farseer-go/fs/path"
"github.com/farseer-go/linkTrace/eumCallType"
"runtime"
Expand Down Expand Up @@ -70,6 +69,17 @@ func (receiver *BaseTraceDetail) GetLevel() int {
return receiver.Level
}

var ComNames = []string{"/farseer-go/async/", "/farseer-go/cache/", "/farseer-go/cacheMemory/", "/farseer-go/collections/", "/farseer-go/data/", "/farseer-go/elasticSearch/", "/farseer-go/etcd/", "/farseer-go/eventBus/", "/farseer-go/fs/", "/farseer-go/linkTrace/", "/farseer-go/mapper/", "/farseer-go/queue/", "/farseer-go/rabbit/", "/farseer-go/redis/", "/farseer-go/redisStream/", "/farseer-go/tasks/", "/farseer-go/utils/", "/farseer-go/webapi/", "/src/reflect/", "/usr/local/go/src/"}

func IsSysCom(file string) bool {
for _, comName := range ComNames {
if strings.Contains(file, comName) {
return true
}
}
return false
}

func GetCallerInfo() (string, string, int) {
// 获取调用栈信息
pc := make([]uintptr, 15) // 假设最多获取 10 层调用栈
Expand All @@ -79,7 +89,7 @@ func GetCallerInfo() (string, string, int) {
// 遍历调用栈帧
for {
frame, more := frames.Next()
if !strings.HasSuffix(frame.File, "_test.go") && (!flog.IsSysCom(frame.File) || strings.HasSuffix(frame.File, "healthCheck.go")) { // !strings.HasPrefix(file, gormSourceDir) ||
if !strings.HasSuffix(frame.File, "_test.go") && (!IsSysCom(frame.File) || strings.HasSuffix(frame.File, "healthCheck.go")) { // !strings.HasPrefix(file, gormSourceDir) ||
// 移除绝对路径
prefixFunc := frame.Function[0 : strings.Index(frame.Function, path.PathSymbol)+len(path.PathSymbol)]
packageIndex := strings.Index(frame.File, prefixFunc)
Expand Down

0 comments on commit 09c5a63

Please sign in to comment.