generated from moul/golang-repo-template
-
-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
147 additions
and
117 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,3 @@ | ||
GOPKG ?= moul.io/zapgorm2 | ||
DOCKER_IMAGE ?= moul/zapgorm2 | ||
GOBINS ?= . | ||
NPM_PACKAGES ?= . | ||
|
||
include rules.mk |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package zapgorm2 | ||
|
||
import ( | ||
"context" | ||
"path/filepath" | ||
"runtime" | ||
"strings" | ||
"time" | ||
|
||
"go.uber.org/zap" | ||
gormlogger "gorm.io/gorm/logger" | ||
) | ||
|
||
type Logger struct { | ||
ZapLogger *zap.Logger | ||
LogLevel gormlogger.LogLevel | ||
SlowThreshold time.Duration | ||
SkipCallerLookup bool | ||
} | ||
|
||
func New(zapLogger *zap.Logger) Logger { | ||
return Logger{ | ||
ZapLogger: zapLogger, | ||
LogLevel: gormlogger.Warn, | ||
SlowThreshold: 100 * time.Millisecond, | ||
SkipCallerLookup: false, | ||
} | ||
} | ||
|
||
func (l Logger) SetAsDefault() { | ||
gormlogger.Default = l | ||
} | ||
|
||
func (l Logger) LogMode(level gormlogger.LogLevel) gormlogger.Interface { | ||
return Logger{ | ||
ZapLogger: l.ZapLogger, | ||
SlowThreshold: l.SlowThreshold, | ||
LogLevel: level, | ||
SkipCallerLookup: l.SkipCallerLookup, | ||
} | ||
} | ||
|
||
func (l Logger) Info(ctx context.Context, str string, args ...interface{}) { | ||
if l.LogLevel < gormlogger.Info { | ||
return | ||
} | ||
l.logger().Sugar().Debugf(str, args...) | ||
} | ||
|
||
func (l Logger) Warn(ctx context.Context, str string, args ...interface{}) { | ||
if l.LogLevel < gormlogger.Warn { | ||
return | ||
} | ||
l.logger().Sugar().Warnf(str, args...) | ||
} | ||
|
||
func (l Logger) Error(ctx context.Context, str string, args ...interface{}) { | ||
if l.LogLevel < gormlogger.Error { | ||
return | ||
} | ||
l.logger().Sugar().Errorf(str, args...) | ||
} | ||
|
||
func (l Logger) Trace(ctx context.Context, begin time.Time, fc func() (string, int64), err error) { | ||
if l.LogLevel <= 0 { | ||
return | ||
} | ||
elapsed := time.Since(begin) | ||
switch { | ||
case err != nil && l.LogLevel >= gormlogger.Error: | ||
sql, rows := fc() | ||
l.logger().Error("trace", zap.Error(err), zap.Duration("elapsed", elapsed), zap.Int64("rows", rows), zap.String("sql", sql)) | ||
case l.SlowThreshold != 0 && elapsed > l.SlowThreshold && l.LogLevel >= gormlogger.Warn: | ||
sql, rows := fc() | ||
l.logger().Warn("trace", zap.Duration("elapsed", elapsed), zap.Int64("rows", rows), zap.String("sql", sql)) | ||
case l.LogLevel >= gormlogger.Info: | ||
sql, rows := fc() | ||
l.logger().Debug("trace", zap.Duration("elapsed", elapsed), zap.Int64("rows", rows), zap.String("sql", sql)) | ||
} | ||
} | ||
|
||
var ( | ||
gormPackage = filepath.Join("gorm.io", "gorm") | ||
zapgormPackage = filepath.Join("moul.io", "zapgorm2") | ||
) | ||
|
||
func (l Logger) logger() *zap.Logger { | ||
for i := 2; i < 15; i++ { | ||
_, file, _, ok := runtime.Caller(i) | ||
switch { | ||
case !ok: | ||
case strings.HasSuffix(file, "_test.go"): | ||
case strings.Contains(file, gormPackage): | ||
case strings.Contains(file, zapgormPackage): | ||
default: | ||
return l.ZapLogger.WithOptions(zap.AddCallerSkip(i)) | ||
} | ||
} | ||
return l.ZapLogger | ||
} |