d28f005552
Fix limit for databases other than sqlite go mod tidy && go mod vendor Remove unneeded break statements Make everything work with the new xorm version Fix xorm logging Fix lint Fix redis init Fix using id field Fix database init for testing Change default database log level Add xorm logger Use const for postgres go mod tidy Merge branch 'master' into update/xorm # Conflicts: # go.mod # go.sum # vendor/modules.txt go mod vendor Fix loading fixtures for postgres Go mod vendor1 Update xorm to version 1 Co-authored-by: kolaente <k@knt.li> Reviewed-on: https://kolaente.dev/vikunja/api/pulls/323
97 lines
2.2 KiB
Go
97 lines
2.2 KiB
Go
// Copyright 2020 The Xorm Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package log
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
)
|
|
|
|
// LogContext represents a log context
|
|
type LogContext struct {
|
|
Ctx context.Context
|
|
SQL string // log content or SQL
|
|
Args []interface{} // if it's a SQL, it's the arguments
|
|
ExecuteTime time.Duration
|
|
Err error // SQL executed error
|
|
}
|
|
|
|
type SQLLogger interface {
|
|
BeforeSQL(context LogContext) // only invoked when IsShowSQL is true
|
|
AfterSQL(context LogContext) // only invoked when IsShowSQL is true
|
|
}
|
|
|
|
// ContextLogger represents a logger interface with context
|
|
type ContextLogger interface {
|
|
SQLLogger
|
|
|
|
Debugf(format string, v ...interface{})
|
|
Errorf(format string, v ...interface{})
|
|
Infof(format string, v ...interface{})
|
|
Warnf(format string, v ...interface{})
|
|
|
|
Level() LogLevel
|
|
SetLevel(l LogLevel)
|
|
|
|
ShowSQL(show ...bool)
|
|
IsShowSQL() bool
|
|
}
|
|
|
|
var (
|
|
_ ContextLogger = &LoggerAdapter{}
|
|
)
|
|
|
|
// LoggerAdapter wraps a Logger interafce as LoggerContext interface
|
|
type LoggerAdapter struct {
|
|
logger Logger
|
|
}
|
|
|
|
func NewLoggerAdapter(logger Logger) ContextLogger {
|
|
return &LoggerAdapter{
|
|
logger: logger,
|
|
}
|
|
}
|
|
|
|
func (l *LoggerAdapter) BeforeSQL(ctx LogContext) {}
|
|
|
|
func (l *LoggerAdapter) AfterSQL(ctx LogContext) {
|
|
if ctx.ExecuteTime > 0 {
|
|
l.logger.Infof("[SQL] %v %v - %v", ctx.SQL, ctx.Args, ctx.ExecuteTime)
|
|
} else {
|
|
l.logger.Infof("[SQL] %v %v", ctx.SQL, ctx.Args)
|
|
}
|
|
}
|
|
|
|
func (l *LoggerAdapter) Debugf(format string, v ...interface{}) {
|
|
l.logger.Debugf(format, v...)
|
|
}
|
|
|
|
func (l *LoggerAdapter) Errorf(format string, v ...interface{}) {
|
|
l.logger.Errorf(format, v...)
|
|
}
|
|
|
|
func (l *LoggerAdapter) Infof(format string, v ...interface{}) {
|
|
l.logger.Infof(format, v...)
|
|
}
|
|
|
|
func (l *LoggerAdapter) Warnf(format string, v ...interface{}) {
|
|
l.logger.Warnf(format, v...)
|
|
}
|
|
|
|
func (l *LoggerAdapter) Level() LogLevel {
|
|
return l.logger.Level()
|
|
}
|
|
|
|
func (l *LoggerAdapter) SetLevel(lv LogLevel) {
|
|
l.logger.SetLevel(lv)
|
|
}
|
|
|
|
func (l *LoggerAdapter) ShowSQL(show ...bool) {
|
|
l.logger.ShowSQL(show...)
|
|
}
|
|
|
|
func (l *LoggerAdapter) IsShowSQL() bool {
|
|
return l.logger.IsShowSQL()
|
|
}
|