Skip to content

Commit

Permalink
feat: initial version
Browse files Browse the repository at this point in the history
  • Loading branch information
moul committed Jun 26, 2020
1 parent 3f315d7 commit d41631a
Show file tree
Hide file tree
Showing 10 changed files with 147 additions and 117 deletions.
31 changes: 0 additions & 31 deletions Dockerfile

This file was deleted.

3 changes: 0 additions & 3 deletions Makefile
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
16 changes: 9 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
# zapgorm2

:smile: zapgorm2
:smile: zapgorm2 is a zap logging driver for gorm v2

[![go.dev reference](https://img.shields.io/badge/go.dev-reference-007d9c?logo=go&logoColor=white)](https://pkg.go.dev/moul.io/zapgorm2)
[![License](https://img.shields.io/badge/license-Apache--2.0%20%2F%20MIT-%2397ca00.svg)](https://github.com/moul/zapgorm2/blob/master/COPYRIGHT)
[![GitHub release](https://img.shields.io/github/release/moul/zapgorm2.svg)](https://github.com/moul/zapgorm2/releases)
[![Docker Metrics](https://images.microbadger.com/badges/image/moul/zapgorm2.svg)](https://microbadger.com/images/moul/zapgorm2)
[![Made by Manfred Touron](https://img.shields.io/badge/made%20by-Manfred%20Touron-blue.svg?style=flat)](https://manfred.life/)

[![CI](https://github.com/moul/zapgorm2/workflows/CI/badge.svg)](https://github.com/moul/zapgorm2/actions?query=workflow%3ACI)
Expand All @@ -15,10 +14,17 @@
[![Go Report Card](https://goreportcard.com/badge/moul.io/zapgorm2)](https://goreportcard.com/report/moul.io/zapgorm2)
[![CodeFactor](https://www.codefactor.io/repository/github/moul/zapgorm2/badge)](https://www.codefactor.io/repository/github/moul/zapgorm2)

If you're using gorm v1, you can use https://github.com/moul/zapgorm instead.

## Usage

TODO
```go
import "moul.io/zapgorm2"

logger := zapgorm2.New(zap.L())
logger.SetAsDefault() // optional: configure gorm to use this zapgorm.Logger for callbacks
db, err = gorm.Open(sqlite.Open("./db.sqlite"), &gorm.Config{Logger: logger})
```

## Install

Expand All @@ -28,10 +34,6 @@ TODO
$ go get -u moul.io/zapgorm2
```

### Releases

See https://github.com/moul/zapgorm2/releases

## Stargazers over time

[![Stargazers over time](https://starchart.cc/moul/zapgorm2.svg)](https://starchart.cc/moul/zapgorm2)
Expand Down
2 changes: 1 addition & 1 deletion doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@
// |/ | | \ \ / / ' \/ _ \/ // / / |
// || | | | | | /_/_/_/\___/\_,_/_/ |
// +--------------------------------------------------------------+
package main // import "moul.io/zapgorm2"
package zapgorm2
6 changes: 3 additions & 3 deletions go.mod

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 34 additions & 8 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 0 additions & 25 deletions main.go

This file was deleted.

18 changes: 0 additions & 18 deletions main_test.go

This file was deleted.

21 changes: 0 additions & 21 deletions package.json

This file was deleted.

100 changes: 100 additions & 0 deletions zapgorm2.go
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
}

0 comments on commit d41631a

Please sign in to comment.