/gormzap

zap logging driver for gorm v2 with custom field. use https://github.com/things-go/gin-contrib instead.

Primary LanguageGoMIT LicenseMIT

zap logging driver for gorm v2

zap logging driver for gorm v2

GoDoc Go.Dev reference codecov Action Status Go Report Card Licence Tag

Features

Usage

Installation

Use go get.

    go get github.com/things-go/gormzap

Then import the gormzap package into your own code.

    import gormzap "github.com/things-go/gormzap"

Example

package main

import (
	"context"
	"time"

	"go.uber.org/zap"
	"gorm.io/gorm"
	"gorm.io/gorm/logger"

	"github/things-go/gormzap"
)

func main() {
	zapL, err := zap.NewProduction()
	if err != nil {
		panic(err)
	}
	log := gormzap.New(zapL,
		gormzap.WithCustomFields(
			gormzap.String("service", "test"),
			func(ctx context.Context) zap.Field {
				v := ctx.Value("requestId")
				if v == nil {
					return zap.Skip()
				}
				if vv, ok := v.(string); ok {
					return zap.String("requestId", vv)
				}
				return zap.Skip()
			},
		),
		gormzap.WithConfig(logger.Config{
			SlowThreshold:             200 * time.Millisecond,
			Colorful:                  false,
			IgnoreRecordNotFoundError: false,
			LogLevel:                  logger.Info,
		}),
	)
	// your dialector
	db, _ := gorm.Open(nil, &gorm.Config{Logger: log})
	// do your things
	_ = db
}

License

This project is under MIT License. See the LICENSE file for the full license text.