gormzap

package module
v0.0.10 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 4, 2023 License: MIT Imports: 8 Imported by: 2

README

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.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Any added in v0.0.8

func Any(key string, value interface{}) func(ctx context.Context) zap.Field

Any custom immutable any field

func Float64 added in v0.0.8

func Float64(key string, value float64) func(ctx context.Context) zap.Field

Float64 custom immutable float32 field

func Immutable added in v0.0.4

func Immutable(key string, value interface{}) func(ctx context.Context) zap.Field

Immutable custom immutable field Deprecated: use Any instead

func Int64 added in v0.0.8

func Int64(key string, value int64) func(ctx context.Context) zap.Field

Int64 custom immutable int64 field

func New

func New(zapLogger *zap.Logger, opts ...Option) logger.Interface

New logger form gorm2

func SetGormDBLogger added in v0.0.3

func SetGormDBLogger(db *gorm.DB, l logger.Interface)

SetGormDBLogger set db logger

func String added in v0.0.8

func String(key string, value string) func(ctx context.Context) zap.Field

String custom immutable string field

func Uint64 added in v0.0.8

func Uint64(key string, value uint64) func(ctx context.Context) zap.Field

Uint64 custom immutable uint64 field

Types

type Logger

type Logger struct {
	logger.Config
	// contains filtered or unexported fields
}

Logger logger for gorm2

func (Logger) Error

func (l Logger) Error(ctx context.Context, msg string, args ...interface{})

Error print error messages

func (Logger) Info

func (l Logger) Info(ctx context.Context, msg string, args ...interface{})

Info print info

func (*Logger) LogMode

func (l *Logger) LogMode(level logger.LogLevel) logger.Interface

LogMode log mode

func (Logger) Trace

func (l Logger) Trace(ctx context.Context, begin time.Time, fc func() (string, int64), err error)

Trace print sql message

func (Logger) Warn

func (l Logger) Warn(ctx context.Context, msg string, args ...interface{})

Warn print warn messages

type Option

type Option func(l *Logger)

Option logger/recover option

func WithConfig

func WithConfig(cfg logger.Config) Option

WithConfig optional custom logger.Config

func WithCustomFields

func WithCustomFields(fields ...func(ctx context.Context) zap.Field) Option

WithCustomFields optional custom field

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL