gormlog

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Feb 11, 2020 License: MIT Imports: 7 Imported by: 0

README

gormlog

GORM logger implementation using go-micro meta logger.

Usage

var debug bool // shows if we have debug enabled in our app

db, err := gorm.Open("postgres", dsn)
if err != nil {
    panic(err)
}

if debug {
    // By default, gorm logs only errors. If we set LogMode to true,
    // then all queries will be logged.
    // WARNING: if you explicitly set this to false, then even
    // errors won't be logged.
    db.LogMode(true)
}

log := logger.NewLogger()

db.SetLogger(gormlog.NewGormLogger(log, gormlog.WithLevel(logger.DebugLevel)))

Documentation

Overview

Package gormlog provides gorm logger implementation using Go-Micro's meta logger.

Example usage:

orm, _ := gorm.Open("postgres", dsn)
orm.LogMode(true)
orm.SetLogger(gormlog.NewGormLogger(log, gormlog.WithLevel(logger.DebugLevel)))

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func DefaultRecordToFields

func DefaultRecordToFields(r Record) []ml.Field

DefaultRecordToFields is default encoder func for gormzap log records.

Types

type GormLogger

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

Logger is a gorm logger implementation using zap.

func NewGormLogger

func NewGormLogger(origin ml.Logger, opts ...GormLoggerOption) *GormLogger

New returns a new gorm logger implemented using zap. By default it logs with debug level.

func (*GormLogger) Print

func (l *GormLogger) Print(values ...interface{})

Print implements gorm's logger interface.

type GormLoggerOption

type GormLoggerOption func(*GormLogger)

LoggerOption is an option for Logger.

func WithLevel

func WithLevel(level ml.Level) GormLoggerOption

WithLevel returns Logger option that sets level for gorm logs. It affects only general logs, e.g. those that contain SQL queries. Errors will be logged with error level independently of this option.

func WithRecordToFields

func WithRecordToFields(f RecordToFields) GormLoggerOption

WithRecordToFields returns Logger option that sets RecordToFields func which encodes log Record to a slice of micro logger fields.

This can be used to control field names or field values types.

Example
package main

import (
	"os"
	"time"

	ml "github.com/micro/go-micro/v2/logger"
	gormlog "github.com/xmlking/micro-starter-kit/shared/micro/gorm"

	zero "github.com/xmlking/micro-starter-kit/shared/micro/logger/zerolog"
)

func main() {
	mLogger := zero.NewLogger(zero.WithOut(os.Stdout), zero.WithLevel(ml.DebugLevel))

	l := gormlog.NewGormLogger(
		mLogger,
		gormlog.WithLevel(ml.DebugLevel),
		gormlog.WithRecordToFields(func(r gormlog.Record) []ml.Field {
			return []ml.Field{
				{Key: "caller", Type: ml.StringType, Value: r.Source},
				{Key: "duration_ms", Type: ml.Float32Type, Value: float32(r.Duration.Nanoseconds()/1000) / 1000},
				{Key: "query", Type: ml.StringType, Value: r.SQL},
				{Key: "rows_affected", Type: ml.Int64Type, Value: r.RowsAffected},
			}
		}),
	)

	l.Print(
		"sql",
		"/foo/bar.go",
		time.Millisecond*200,
		"SELECT * FROM foo WHERE id = ?",
		[]interface{}{123},
		int64(2),
	)

}
Output:

{"level":"debug","caller":"/foo/bar.go","duration_ms":200,"query":"SELECT * FROM foo WHERE id = 123","rows_affected":2,"message":"gorm query"}

type Record

type Record struct {
	Message string
	Source  string
	Level   ml.Level

	Duration     time.Duration
	SQL          string
	RowsAffected int64
}

Record is gormlog log record.

type RecordToFields

type RecordToFields func(r Record) []ml.Field

RecordToFields func can encode gormlog Record into a slice of zap fields.

Jump to

Keyboard shortcuts

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