gorm_logrus

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Jan 10, 2024 License: MIT Imports: 7 Imported by: 0

README

gorm-logrus

A wrapper between logrus Logger and gorm Logger.

logrus日志到gorm日志的兼容层。

Example 示例

package main

import (
    "github.com/gedoy9793/gorm-logrus"
    "github.com/sirupsen/logrus"
    "gorm.io/gorm"
	"gorm.io/driver/mysql"
)

func main() {
    logrusLogger := logrus.New()
    
    // you can config this logger
    
    db, _ := gorm.Open(mysql.Open("data.db"), &gorm.Config{
        Logger: gorm_logrus.NewLogger(logrusLogger),
    })
}

You can also use logrus.Entry to create a wrapper:

也可以使用logrus.Entry创建一个兼容层:

package main

import (
    "github.com/gedoy9793/gorm-logrus"
    "github.com/sirupsen/logrus"
    "gorm.io/gorm"
	"gorm.io/driver/mysql"
)

func main() {
    logrusEntry := logrus.New().WithField("key", "value")
    
    // you can config this entry
    
    db, _ := gorm.Open(mysql.Open("data.db"), &gorm.Config{
        Logger: gorm_logrus.NewEntry(logrusEntry),
    })
}

Note: Gorm uses the LogMode function to set the logging level. In this wrapper, it's implemented as Entry.Logger.SetLevel. So I don't recommend using this Logger in more than one place, instead you should create a separate Logger for gorm.

注意: Gorm使用LogMode函数设置日志等级。在兼容层中,这个函数被实现为Entry.Logger.SetLevel。所以我不建议在多个地方使用这个Logger,而应该为gorm创建单独的Logger。

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type WrapEntry added in v0.0.2

type WrapEntry struct {
	SlowThreshold         time.Duration
	SourceField           string
	SkipErrRecordNotFound bool
	Debug                 bool
	// contains filtered or unexported fields
}

func NewEntry added in v0.0.2

func NewEntry(entry *logrusLogger.Entry) *WrapEntry

func (*WrapEntry) Error added in v0.0.2

func (l *WrapEntry) Error(ctx context.Context, s string, args ...interface{})

func (*WrapEntry) Info added in v0.0.2

func (l *WrapEntry) Info(ctx context.Context, s string, args ...interface{})

func (*WrapEntry) LogMode added in v0.0.2

func (l *WrapEntry) LogMode(level gormLogger.LogLevel) gormLogger.Interface

func (*WrapEntry) Trace added in v0.0.2

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

func (*WrapEntry) Warn added in v0.0.2

func (l *WrapEntry) Warn(ctx context.Context, s string, args ...interface{})

type WrapLogger

type WrapLogger struct {
	SlowThreshold         time.Duration
	SourceField           string
	SkipErrRecordNotFound bool
	Debug                 bool
	// contains filtered or unexported fields
}

func NewLogger added in v0.0.2

func NewLogger(logger *logrusLogger.Logger) *WrapLogger

func (*WrapLogger) Error

func (l *WrapLogger) Error(ctx context.Context, s string, args ...interface{})

func (*WrapLogger) Info

func (l *WrapLogger) Info(ctx context.Context, s string, args ...interface{})

func (*WrapLogger) LogMode

func (*WrapLogger) Trace

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

func (*WrapLogger) Warn

func (l *WrapLogger) Warn(ctx context.Context, s string, args ...interface{})

Jump to

Keyboard shortcuts

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