gormzap

package module
v0.3.2 Latest Latest
Warning

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

Go to latest
Published: Apr 21, 2020 License: MIT Imports: 8 Imported by: 0

README

gormzap

GoDoc CircleCI GitHub release License MIT

GORM logger implementation using zap.

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 := zap.NewExample()

db.SetLogger(gormzap.New(log, gormzap.WithLevel(zap.DebugLevel)))

Documentation

Overview

Package gormzap provides gorm logger implementation using Uber's zap logger.

Example usage:

orm, _ := gorm.Open("postgres", dsn)
orm.LogMode(true)
orm.SetLogger(gormzap.New(log, gormzap.WithLevel(zap.InfoLevel))

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func DefaultRecordToFields

func DefaultRecordToFields(r Record) []zapcore.Field

DefaultRecordToFields is default encoder func for gormzap log records.

func ShortenCodeSource added in v0.3.1

func ShortenCodeSource(source string) string

simplify /usr/local/share/GOHOME/gopath/src/zgcloud.com/backend/goim/store/reminder_store.go:35 to store/reminder_store.go:35

Types

type Logger

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

Logger is a gorm logger implementation using zap.

Example
package main

import (
	"github.com/everimbaq/gormzap"
	"time"

	"go.uber.org/zap"
)

func main() {
	z := zap.NewExample()

	l := gormzap.New(z)

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

}
Output:

{"level":"debug","msg":"gorm query","source":"foo/bar.go","dur":"2s","query":"SELECT * FROM foo WHERE id = 123","rows_affected":2}

func New

func New(origin *zap.Logger, opts ...LoggerOption) *Logger

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

func (*Logger) Print

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

Print implements gorm's logger interface.

func (*Logger) SetSimplifyLog added in v0.3.1

func (l *Logger) SetSimplifyLog(simplifyLog bool)

type LoggerOption

type LoggerOption func(*Logger)

LoggerOption is an option for Logger.

func WithLevel

func WithLevel(level zapcore.Level) LoggerOption

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) LoggerOption

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

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

Example
package main

import (
	"github.com/everimbaq/gormzap"
	"time"

	"go.uber.org/zap"
	"go.uber.org/zap/zapcore"
)

func main() {
	z := zap.NewExample()

	l := gormzap.New(
		z,
		gormzap.WithLevel(zap.DebugLevel),
		gormzap.WithRecordToFields(func(r gormzap.Record) []zapcore.Field {
			return []zapcore.Field{
				zap.String("caller", r.Source),
				zap.Float32("duration_ms", float32(r.Duration.Nanoseconds()/1000)/1000),
				zap.String("query", r.SQL),
				zap.Int64("rows_affected", r.RowsAffected),
			}
		}),
	)

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

}
Output:

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

type Record

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

	Duration     time.Duration
	SQL          string
	RowsAffected int64
}

Record is gormzap log record.

type RecordToFields

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

RecordToFields func can encode gormzap 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