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 ¶
DefaultRecordToFields is default encoder func for gormzap log records.
func ShortenCodeSource ¶ added in v0.3.1
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
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 ¶
RecordToFields func can encode gormzap Record into a slice of zap fields.