2020-03-22 15:12:55 +00:00
|
|
|
// 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 (
|
2020-06-15 20:46:01 +00:00
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"xorm.io/xorm/contexts"
|
2020-03-22 15:12:55 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// LogContext represents a log context
|
2020-06-15 20:46:01 +00:00
|
|
|
type LogContext contexts.ContextHook
|
2020-03-22 15:12:55 +00:00
|
|
|
|
2020-06-15 20:46:01 +00:00
|
|
|
// SQLLogger represents an interface to log SQL
|
2020-03-22 15:12:55 +00:00
|
|
|
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{}
|
|
|
|
)
|
|
|
|
|
2020-06-15 20:46:01 +00:00
|
|
|
// enumerate all the context keys
|
|
|
|
var (
|
|
|
|
SessionIDKey = "__xorm_session_id"
|
2020-07-21 12:28:27 +00:00
|
|
|
SessionKey = "__xorm_session_key"
|
2020-06-15 20:46:01 +00:00
|
|
|
SessionShowSQLKey = "__xorm_show_sql"
|
|
|
|
)
|
|
|
|
|
|
|
|
// LoggerAdapter wraps a Logger interface as LoggerContext interface
|
2020-03-22 15:12:55 +00:00
|
|
|
type LoggerAdapter struct {
|
|
|
|
logger Logger
|
|
|
|
}
|
|
|
|
|
2020-06-15 20:46:01 +00:00
|
|
|
// NewLoggerAdapter creates an adapter for old xorm logger interface
|
2020-03-22 15:12:55 +00:00
|
|
|
func NewLoggerAdapter(logger Logger) ContextLogger {
|
|
|
|
return &LoggerAdapter{
|
|
|
|
logger: logger,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-15 20:46:01 +00:00
|
|
|
// BeforeSQL implements ContextLogger
|
2020-03-22 15:12:55 +00:00
|
|
|
func (l *LoggerAdapter) BeforeSQL(ctx LogContext) {}
|
|
|
|
|
2020-06-15 20:46:01 +00:00
|
|
|
// AfterSQL implements ContextLogger
|
2020-03-22 15:12:55 +00:00
|
|
|
func (l *LoggerAdapter) AfterSQL(ctx LogContext) {
|
2020-06-15 20:46:01 +00:00
|
|
|
var sessionPart string
|
|
|
|
v := ctx.Ctx.Value(SessionIDKey)
|
|
|
|
if key, ok := v.(string); ok {
|
|
|
|
sessionPart = fmt.Sprintf(" [%s]", key)
|
|
|
|
}
|
2020-03-22 15:12:55 +00:00
|
|
|
if ctx.ExecuteTime > 0 {
|
2020-06-15 20:46:01 +00:00
|
|
|
l.logger.Infof("[SQL]%s %s %v - %v", sessionPart, ctx.SQL, ctx.Args, ctx.ExecuteTime)
|
2020-03-22 15:12:55 +00:00
|
|
|
} else {
|
2020-06-15 20:46:01 +00:00
|
|
|
l.logger.Infof("[SQL]%s %s %v", sessionPart, ctx.SQL, ctx.Args)
|
2020-03-22 15:12:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-15 20:46:01 +00:00
|
|
|
// Debugf implements ContextLogger
|
2020-03-22 15:12:55 +00:00
|
|
|
func (l *LoggerAdapter) Debugf(format string, v ...interface{}) {
|
|
|
|
l.logger.Debugf(format, v...)
|
|
|
|
}
|
|
|
|
|
2020-06-15 20:46:01 +00:00
|
|
|
// Errorf implements ContextLogger
|
2020-03-22 15:12:55 +00:00
|
|
|
func (l *LoggerAdapter) Errorf(format string, v ...interface{}) {
|
|
|
|
l.logger.Errorf(format, v...)
|
|
|
|
}
|
|
|
|
|
2020-06-15 20:46:01 +00:00
|
|
|
// Infof implements ContextLogger
|
2020-03-22 15:12:55 +00:00
|
|
|
func (l *LoggerAdapter) Infof(format string, v ...interface{}) {
|
|
|
|
l.logger.Infof(format, v...)
|
|
|
|
}
|
|
|
|
|
2020-06-15 20:46:01 +00:00
|
|
|
// Warnf implements ContextLogger
|
2020-03-22 15:12:55 +00:00
|
|
|
func (l *LoggerAdapter) Warnf(format string, v ...interface{}) {
|
|
|
|
l.logger.Warnf(format, v...)
|
|
|
|
}
|
|
|
|
|
2020-06-15 20:46:01 +00:00
|
|
|
// Level implements ContextLogger
|
2020-03-22 15:12:55 +00:00
|
|
|
func (l *LoggerAdapter) Level() LogLevel {
|
|
|
|
return l.logger.Level()
|
|
|
|
}
|
|
|
|
|
2020-06-15 20:46:01 +00:00
|
|
|
// SetLevel implements ContextLogger
|
2020-03-22 15:12:55 +00:00
|
|
|
func (l *LoggerAdapter) SetLevel(lv LogLevel) {
|
|
|
|
l.logger.SetLevel(lv)
|
|
|
|
}
|
|
|
|
|
2020-06-15 20:46:01 +00:00
|
|
|
// ShowSQL implements ContextLogger
|
2020-03-22 15:12:55 +00:00
|
|
|
func (l *LoggerAdapter) ShowSQL(show ...bool) {
|
|
|
|
l.logger.ShowSQL(show...)
|
|
|
|
}
|
|
|
|
|
2020-06-15 20:46:01 +00:00
|
|
|
// IsShowSQL implements ContextLogger
|
2020-03-22 15:12:55 +00:00
|
|
|
func (l *LoggerAdapter) IsShowSQL() bool {
|
|
|
|
return l.logger.IsShowSQL()
|
|
|
|
}
|