gorm_log

package module
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Feb 14, 2023 License: MIT Imports: 7 Imported by: 4

README

gorm_log

example

create a sqlite.db file with a table name 'person' defined as :

id   int64
name string
age  int64


package main

import (
	"time"

	"github.com/coreservice-io/gorm_log"
	"github.com/coreservice-io/log"
	"github.com/coreservice-io/logrus_log"
	"gorm.io/driver/sqlite"
	"gorm.io/gorm"
)

type Person struct {
	ID   int64  `gorm:"primaryKey"`
	Name string `gorm:"index"`
	Age  int64  `gorm:"index"`
}

func main() {
	//logger instance
	logger, _ := logrus_log.New("./logs", 2, 20, 30)
	logger.SetLevel(log.DebugLevel)

	//new db
	db, err := gorm.Open(sqlite.Open("./sqlite.db"), &gorm.Config{
		//use custom logger
		Logger: gorm_log.New_gormLocalLogger(logger, gorm_log.Config{
			SlowThreshold:             500 * time.Millisecond,
			IgnoreRecordNotFoundError: false,
			//Level: Silent Error Warn Info.
			//Info logs all record.
			//Silent turns off log.
			LogLevel: gorm_log.Info,
		}),
	})
	if err != nil {
		logger.Fatalln(err)
	}

	err = db.AutoMigrate(
		&Person{},
	)
	if err != nil {
		logger.Fatalln(err)
	}

	p := Person{Name: "Jack", Age: 18}
	db.Create(&p)

	var qp Person
	db.First(&qp)
	logger.Debugln(qp)

	//user Debug() to trace sql
	db.Debug().Last(&qp)
	logger.Debugln(qp)
}


Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New_gormLocalLogger

func New_gormLocalLogger(log log.Logger, config Config) *gormLocalLogger

Types

type Config

type Config struct {
	SlowThreshold             time.Duration
	IgnoreRecordNotFoundError bool
	LogLevel                  LogLevel
}

type LogLevel

type LogLevel int32

LogLevel

const (
	Silent LogLevel = iota + 1
	Error
	Warn
	Info
)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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