gormzap

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jul 13, 2018 License: MIT Imports: 8 Imported by: 0

README

gormzap

GoDoc CircleCI GitHub release License MIT

GORM logger implementation using zap.

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.

Types

type Logger

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

Logger is a gorm logger implementation using zap.

Example
package main

import (
	"time"

	"github.com/hypnoglow/gormzap"
	"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","sql.source":"/foo/bar.go","sql.duration":"2s","sql.query":"SELECT * FROM foo WHERE id = 123","sql.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.

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.

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 (
	"time"

	"github.com/hypnoglow/gormzap"
	"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
	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