logger

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jun 23, 2024 License: MIT Imports: 9 Imported by: 0

README

GORM Logger

A customized GORM logger that implements the appropriate interface and uses Logrus to output logs.

Install

go get github.com/vschst/gorm-logger

Basic usage

package main

import (
    "github.com/vschst/gorm-logger"
    "github.com/sirupsen/logrus"
    "gorm.io/driver/sqlite"
    "gorm.io/gorm"
    gormLogger "gorm.io/gorm/logger"
    "time"
)

func main() {
    log := logrus.New()
    newLogger := logger.New(log, logger.Config{
        SlowThreshold:  time.Second,
        SkipErrRecordNotFound: true,
        LogLevel:   gormLogger.Error,
    })

    db, err := gorm.Open(sqlite.Open("test.db"), &gorm.Config{
        Logger: newLogger,
    })
}

Logger сonfiguration

When creating in the logger.New method, the second parameter specifies the configuration of the logger. The logger.Config structure has the following fields:

Parameter Type Default value Description
SlowThreshold time.Duration If the sql query time exceeds this value, a warning log about the slow sql query time will be output
SkipErrRecordNotFound bool false Skip ErrRecordNotFound error for logger
SourceField string Source field in config which is recorded file name and line number of the current error
ModuleName string "gorm" Name of the module in the log
LogLevel gormLogger.LogLevel gormLogger.Info Log level

License

© Viktor Schastnyy, since 2021

Released under the MIT License

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	SlowThreshold         time.Duration
	LogLevel              gormLogger.LogLevel
	SkipErrRecordNotFound bool
	SourceField           string
	ModuleName            string
}

type Logger

type Logger struct {
	// contains filtered or unexported fields
}

func New

func New(l *logrus.Logger, config Config) *Logger

func (*Logger) Error

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

func (*Logger) GetLogger

func (l *Logger) GetLogger() *logrus.Logger

func (*Logger) Info

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

func (*Logger) LogMode

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

func (*Logger) Trace

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

func (*Logger) Warn

func (l *Logger) 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