zerologgorm

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 16, 2023 License: MIT Imports: 8 Imported by: 0

README

zerolog-gorm

license

Zerolog logger for gorm

package main

import (
    "context"
    "github.com/go-mods/zerolog-gorm"
    "gorm.io/driver/sqlite"
    "gorm.io/gorm"
    "time"
)

func main() {
    logger := zerolog.New(zerolog.ConsoleWriter{Out: os.Stdout}).With().Timestamp().Logger()

    db, err := gorm.Open(sqlite.Open("test.db"), &gorm.Config{
        NowFunc: func() time.Time { return time.Now() },
        Logger: &zerologgorm.GormLogger{
            FieldsExclude: []string{zerologgorm.DurationFieldName, zerologgorm.FileFieldName},
        },
    })

    if err != nil {
        panic("failed to connect the database")
    }

    db = db.WithContext(logger.WithContext(context.Background()))
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// TimestampFieldName is the field name used for the timestamp field.
	TimestampFieldName = zerolog.TimestampFieldName

	// DurationFieldName is the field name used for the duration field.
	DurationFieldName = "elapsed"

	// FileFieldName is the field name used for the file field.
	FileFieldName = "file"

	// SqlFieldName is the field name used for the sql field.
	SqlFieldName = "sql"

	// RowsFieldName is the field name used for the rows field.
	RowsFieldName = "rows"
)

Functions

This section is empty.

Types

type GormLogger

type GormLogger struct {
	// SlowThreshold is the delay which define the query as slow
	SlowThreshold time.Duration

	// IgnoreRecordNotFoundError is to ignore when the record is not found
	IgnoreRecordNotFoundError bool

	// FieldsOrder defines the order of fields in output.
	FieldsOrder []string

	// FieldsExclude defines contextual fields to not display in output.
	FieldsExclude []string
}

func NewGormLogger

func NewGormLogger() *GormLogger

NewGormLogger creates and initializes a new GormLogger.

func (GormLogger) Error

func (l GormLogger) Error(ctx context.Context, msg string, data ...interface{})

Error print error messages

func (GormLogger) Info

func (l GormLogger) Info(ctx context.Context, msg string, data ...interface{})

Info print info

func (*GormLogger) LogMode

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

LogMode log mode

func (GormLogger) Trace

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

Trace print sql message

func (GormLogger) Warn

func (l GormLogger) Warn(ctx context.Context, msg string, data ...interface{})

Warn print warn messages

Jump to

Keyboard shortcuts

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