GormULog

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Jan 24, 2022 License: MIT Imports: 7 Imported by: 1

README

GormULog

logger for gorm using ULog

example

package main

import (
	"time"

	"github.com/universe-30/GormULog"
	"github.com/universe-30/LogrusULog"
	"github.com/universe-30/ULog"
	"gorm.io/driver/sqlite"
	"gorm.io/gorm"
)

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

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

	//new db
	db, err := gorm.Open(sqlite.Open("./sqlite.db"), &gorm.Config{
		//use custom logger
		Logger: GormULog.New_gormLocalLogger(logger, GormULog.Config{
			SlowThreshold:             500 * time.Millisecond,
			IgnoreRecordNotFoundError: false,
			//Level: Silent Error Warn Info. 
			//Info logs all record.
			//Silent turns off log.
			LogLevel:                  GormULog.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(ulog ULog.Logger, config Config) *gormLocalLogger

Types

type Config

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

type LogLevel added in v0.1.1

type LogLevel int
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