Compare commits
3 Commits
Author | SHA1 | Date | |
---|---|---|---|
cecfed7212 | |||
7ecc9ad8a4 | |||
1aafb68b5a |
105
caller.go
Normal file
105
caller.go
Normal file
@@ -0,0 +1,105 @@
|
||||
package logger
|
||||
|
||||
import (
|
||||
"runtime"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
var (
|
||||
|
||||
// qualified package name, cached at first use
|
||||
logrusPackage string
|
||||
|
||||
// Positions in the call stack when tracing to report the calling method
|
||||
minimumCallerDepth int
|
||||
|
||||
// Used for caller information initialisation
|
||||
callerInitOnce sync.Once
|
||||
)
|
||||
|
||||
const (
|
||||
maximumCallerDepth int = 25
|
||||
knownLogrusFrames int = 8
|
||||
)
|
||||
|
||||
func init() {
|
||||
// start at the bottom of the stack before the package-name cache is primed
|
||||
minimumCallerDepth = 1
|
||||
}
|
||||
|
||||
// getPackageName reduces a fully qualified function name to the package name
|
||||
// There really ought to be to be a better way...
|
||||
func getPackageName(f string) string {
|
||||
for {
|
||||
lastPeriod := strings.LastIndex(f, ".")
|
||||
lastSlash := strings.LastIndex(f, "/")
|
||||
if lastPeriod > lastSlash {
|
||||
f = f[:lastPeriod]
|
||||
} else {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return f
|
||||
}
|
||||
|
||||
// getCaller retrieves the name of the first non-logrus calling function
|
||||
func getCaller() *runtime.Frame {
|
||||
// cache this package's fully-qualified name
|
||||
callerInitOnce.Do(func() {
|
||||
pcs := make([]uintptr, maximumCallerDepth)
|
||||
_ = runtime.Callers(0, pcs)
|
||||
|
||||
// dynamic get the package name and the minimum caller depth
|
||||
for i := 0; i < maximumCallerDepth; i++ {
|
||||
funcName := runtime.FuncForPC(pcs[i]).Name()
|
||||
if strings.Contains(funcName, "getCaller") {
|
||||
logrusPackage = getPackageName(funcName)
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
minimumCallerDepth = knownLogrusFrames
|
||||
})
|
||||
|
||||
// Restrict the lookback frames to avoid runaway lookups
|
||||
pcs := make([]uintptr, maximumCallerDepth)
|
||||
depth := runtime.Callers(minimumCallerDepth, pcs)
|
||||
frames := runtime.CallersFrames(pcs[:depth])
|
||||
|
||||
for f, again := frames.Next(); again; f, again = frames.Next() {
|
||||
pkg := getPackageName(f.Function)
|
||||
|
||||
// If the caller isn't part of this package, we're done
|
||||
if pkg != logrusPackage && pkg != "github.com/sirupsen/logrus" {
|
||||
return &f //nolint:scopelint
|
||||
}
|
||||
}
|
||||
|
||||
// if we got here, we failed to find the caller's context
|
||||
return nil
|
||||
}
|
||||
|
||||
// CallerHook is a hook designed for dealing with logs in test scenarios.
|
||||
type CallerHook struct{}
|
||||
|
||||
// NewCallerHook installs a test hook for a given local logger.
|
||||
func NewCallerHook(logger *logrus.Logger) *CallerHook {
|
||||
hook := new(CallerHook)
|
||||
logger.Hooks.Add(hook)
|
||||
return hook
|
||||
}
|
||||
|
||||
func (t *CallerHook) Fire(e *logrus.Entry) error {
|
||||
if e.HasCaller() {
|
||||
e.Caller = getCaller()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t *CallerHook) Levels() []logrus.Level {
|
||||
return logrus.AllLevels
|
||||
}
|
3
go.mod
3
go.mod
@@ -5,11 +5,10 @@ go 1.17
|
||||
require (
|
||||
github.com/caarlos0/env/v6 v6.9.2
|
||||
github.com/go-kit/kit v0.12.0
|
||||
github.com/lixh00/loki-client-go v1.0.1
|
||||
git.echol.cn/loser/loki-client-go v1.0.1
|
||||
github.com/natefinch/lumberjack v2.0.0+incompatible
|
||||
github.com/prometheus/common v0.34.0
|
||||
go.uber.org/zap v1.21.0
|
||||
gorm.io/driver/mysql v1.3.2
|
||||
gorm.io/gorm v1.23.5
|
||||
)
|
||||
|
||||
|
45
gorm_test.go
45
gorm_test.go
@@ -1,45 +0,0 @@
|
||||
package logger
|
||||
|
||||
import (
|
||||
"gitee.ltd/lxh/logger/log"
|
||||
"gorm.io/driver/mysql"
|
||||
"gorm.io/gorm"
|
||||
gl "gorm.io/gorm/logger"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestGormLogger(t *testing.T) {
|
||||
dsn := "saas:saas123@tcp(10.11.0.10:3307)/saas_tenant?charset=utf8mb4&parseTime=True&loc=Local"
|
||||
|
||||
engine, err := gorm.Open(mysql.Open(dsn), &gorm.Config{Logger: DefaultGormLogger()})
|
||||
if err != nil {
|
||||
log.Panicf("mysql connect error: %s", err.Error())
|
||||
}
|
||||
|
||||
var count int64
|
||||
if err := engine.Table("t_tenant").Count(&count).Error; err != nil {
|
||||
t.Log(err)
|
||||
}
|
||||
t.Logf("count: %d", count)
|
||||
}
|
||||
|
||||
func TestGormLoggerWithConfig(t *testing.T) {
|
||||
dsn := "saas:saas123@tcp(10.11.0.10:3307)/saas_tenant?charset=utf8mb4&parseTime=True&loc=Local"
|
||||
|
||||
engine, err := gorm.Open(mysql.Open(dsn), &gorm.Config{Logger: NewGormLoggerWithConfig(gl.Config{
|
||||
SlowThreshold: time.Second, // Slow SQL threshold
|
||||
IgnoreRecordNotFoundError: false, // 忽略没找到结果的错误
|
||||
LogLevel: gl.Warn, // Log level
|
||||
Colorful: false, // Disable color
|
||||
})})
|
||||
if err != nil {
|
||||
log.Panicf("mysql connect error: %s", err.Error())
|
||||
}
|
||||
|
||||
var count int64
|
||||
if err := engine.Table("t_tenant1").Count(&count).Error; err != nil {
|
||||
t.Log(err)
|
||||
}
|
||||
t.Logf("count: %d", count)
|
||||
}
|
12
log/say.go
12
log/say.go
@@ -1,6 +1,9 @@
|
||||
package log
|
||||
|
||||
import "go.uber.org/zap"
|
||||
import (
|
||||
"encoding/json"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
// Debug uses fmt.Sprint to construct and log a message.
|
||||
func Debug(args ...interface{}) {
|
||||
@@ -73,3 +76,10 @@ func Fatalf(template string, args ...interface{}) {
|
||||
defer zap.S().Sync()
|
||||
zap.S().Fatalf(template, args...)
|
||||
}
|
||||
|
||||
// Pretty provides pretty trace level logging
|
||||
func Pretty(v ...interface{}) {
|
||||
b, _ := json.MarshalIndent(v, "", " ")
|
||||
defer zap.S().Sync()
|
||||
zap.S().Info(string(b))
|
||||
}
|
||||
|
@@ -1,23 +0,0 @@
|
||||
package logger
|
||||
|
||||
import (
|
||||
"gitee.ltd/lxh/logger/log"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestLogger(t *testing.T) {
|
||||
InitLogger(LogConfig{Mode: Dev, LokiEnable: false, FileEnable: true})
|
||||
log.Debug("芜湖")
|
||||
}
|
||||
|
||||
func TestLogger1(t *testing.T) {
|
||||
log.Info("我是测试消息")
|
||||
time.Sleep(5 * time.Second)
|
||||
}
|
||||
|
||||
func TestLogger2(t *testing.T) {
|
||||
InitLogger(LogConfig{Mode: Dev, LokiEnable: false, FileEnable: true})
|
||||
log.Info("我是测试消息")
|
||||
//time.Sleep(5 * time.Second)
|
||||
}
|
Reference in New Issue
Block a user