Support session id (#1632)
small nit Support session id Reviewed-on: https://gitea.com/xorm/xorm/pulls/1632
This commit is contained in:
parent
6254e7899f
commit
b78418daa5
|
@ -118,7 +118,7 @@ func (db *DB) NeedLogSQL(ctx context.Context) bool {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
v := ctx.Value("__xorm_show_sql")
|
v := ctx.Value(log.SessionShowSQLKey)
|
||||||
if showSQL, ok := v.(bool); ok {
|
if showSQL, ok := v.(bool); ok {
|
||||||
return showSQL
|
return showSQL
|
||||||
}
|
}
|
||||||
|
|
|
@ -42,6 +42,13 @@ type Engine struct {
|
||||||
|
|
||||||
TZLocation *time.Location // The timezone of the application
|
TZLocation *time.Location // The timezone of the application
|
||||||
DatabaseTZ *time.Location // The timezone of the database
|
DatabaseTZ *time.Location // The timezone of the database
|
||||||
|
|
||||||
|
logSessionID bool // create session id
|
||||||
|
}
|
||||||
|
|
||||||
|
// EnableSessionID if enable session id
|
||||||
|
func (engine *Engine) EnableSessionID(enable bool) {
|
||||||
|
engine.logSessionID = enable
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetCacher sets cacher for the table
|
// SetCacher sets cacher for the table
|
||||||
|
|
|
@ -6,6 +6,7 @@ package log
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -18,6 +19,7 @@ type LogContext struct {
|
||||||
Err error // SQL executed error
|
Err error // SQL executed error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SQLLogger represents an interface to log SQL
|
||||||
type SQLLogger interface {
|
type SQLLogger interface {
|
||||||
BeforeSQL(context LogContext) // only invoked when IsShowSQL is true
|
BeforeSQL(context LogContext) // only invoked when IsShowSQL is true
|
||||||
AfterSQL(context LogContext) // only invoked when IsShowSQL is true
|
AfterSQL(context LogContext) // only invoked when IsShowSQL is true
|
||||||
|
@ -43,55 +45,77 @@ var (
|
||||||
_ ContextLogger = &LoggerAdapter{}
|
_ ContextLogger = &LoggerAdapter{}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// enumerate all the context keys
|
||||||
|
var (
|
||||||
|
SessionIDKey = "__xorm_session_id"
|
||||||
|
SessionShowSQLKey = "__xorm_show_sql"
|
||||||
|
)
|
||||||
|
|
||||||
// LoggerAdapter wraps a Logger interafce as LoggerContext interface
|
// LoggerAdapter wraps a Logger interafce as LoggerContext interface
|
||||||
type LoggerAdapter struct {
|
type LoggerAdapter struct {
|
||||||
logger Logger
|
logger Logger
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewLoggerAdapter creates an adapter for old xorm logger interface
|
||||||
func NewLoggerAdapter(logger Logger) ContextLogger {
|
func NewLoggerAdapter(logger Logger) ContextLogger {
|
||||||
return &LoggerAdapter{
|
return &LoggerAdapter{
|
||||||
logger: logger,
|
logger: logger,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// BeforeSQL implements ContextLogger
|
||||||
func (l *LoggerAdapter) BeforeSQL(ctx LogContext) {}
|
func (l *LoggerAdapter) BeforeSQL(ctx LogContext) {}
|
||||||
|
|
||||||
|
// AfterSQL implements ContextLogger
|
||||||
func (l *LoggerAdapter) AfterSQL(ctx LogContext) {
|
func (l *LoggerAdapter) AfterSQL(ctx LogContext) {
|
||||||
|
var sessionPart string
|
||||||
|
v := ctx.Ctx.Value(SessionIDKey)
|
||||||
|
if key, ok := v.(string); ok {
|
||||||
|
sessionPart = fmt.Sprintf(" [%s]", key)
|
||||||
|
}
|
||||||
if ctx.ExecuteTime > 0 {
|
if ctx.ExecuteTime > 0 {
|
||||||
l.logger.Infof("[SQL] %v %v - %v", ctx.SQL, ctx.Args, ctx.ExecuteTime)
|
l.logger.Infof("[SQL]%s %s %v - %v", sessionPart, ctx.SQL, ctx.Args, ctx.ExecuteTime)
|
||||||
} else {
|
} else {
|
||||||
l.logger.Infof("[SQL] %v %v", ctx.SQL, ctx.Args)
|
l.logger.Infof("[SQL]%s %s %v", sessionPart, ctx.SQL, ctx.Args)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Debugf implements ContextLogger
|
||||||
func (l *LoggerAdapter) Debugf(format string, v ...interface{}) {
|
func (l *LoggerAdapter) Debugf(format string, v ...interface{}) {
|
||||||
l.logger.Debugf(format, v...)
|
l.logger.Debugf(format, v...)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Errorf implements ContextLogger
|
||||||
func (l *LoggerAdapter) Errorf(format string, v ...interface{}) {
|
func (l *LoggerAdapter) Errorf(format string, v ...interface{}) {
|
||||||
l.logger.Errorf(format, v...)
|
l.logger.Errorf(format, v...)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Infof implements ContextLogger
|
||||||
func (l *LoggerAdapter) Infof(format string, v ...interface{}) {
|
func (l *LoggerAdapter) Infof(format string, v ...interface{}) {
|
||||||
l.logger.Infof(format, v...)
|
l.logger.Infof(format, v...)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Warnf implements ContextLogger
|
||||||
func (l *LoggerAdapter) Warnf(format string, v ...interface{}) {
|
func (l *LoggerAdapter) Warnf(format string, v ...interface{}) {
|
||||||
l.logger.Warnf(format, v...)
|
l.logger.Warnf(format, v...)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Level implements ContextLogger
|
||||||
func (l *LoggerAdapter) Level() LogLevel {
|
func (l *LoggerAdapter) Level() LogLevel {
|
||||||
return l.logger.Level()
|
return l.logger.Level()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SetLevel implements ContextLogger
|
||||||
func (l *LoggerAdapter) SetLevel(lv LogLevel) {
|
func (l *LoggerAdapter) SetLevel(lv LogLevel) {
|
||||||
l.logger.SetLevel(lv)
|
l.logger.SetLevel(lv)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ShowSQL implements ContextLogger
|
||||||
func (l *LoggerAdapter) ShowSQL(show ...bool) {
|
func (l *LoggerAdapter) ShowSQL(show ...bool) {
|
||||||
l.logger.ShowSQL(show...)
|
l.logger.ShowSQL(show...)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// IsShowSQL implements ContextLogger
|
||||||
func (l *LoggerAdapter) IsShowSQL() bool {
|
func (l *LoggerAdapter) IsShowSQL() bool {
|
||||||
return l.logger.IsShowSQL()
|
return l.logger.IsShowSQL()
|
||||||
}
|
}
|
||||||
|
|
28
session.go
28
session.go
|
@ -6,10 +6,14 @@ package xorm
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"crypto/rand"
|
||||||
|
"crypto/sha256"
|
||||||
"database/sql"
|
"database/sql"
|
||||||
|
"encoding/hex"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"hash/crc32"
|
"hash/crc32"
|
||||||
|
"io"
|
||||||
"reflect"
|
"reflect"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
@ -19,6 +23,7 @@ import (
|
||||||
"xorm.io/xorm/core"
|
"xorm.io/xorm/core"
|
||||||
"xorm.io/xorm/internal/json"
|
"xorm.io/xorm/internal/json"
|
||||||
"xorm.io/xorm/internal/statements"
|
"xorm.io/xorm/internal/statements"
|
||||||
|
"xorm.io/xorm/log"
|
||||||
"xorm.io/xorm/schemas"
|
"xorm.io/xorm/schemas"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -92,6 +97,17 @@ func (session *Session) Clone() *Session {
|
||||||
return &sess
|
return &sess
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func newSessionID() string {
|
||||||
|
hash := sha256.New()
|
||||||
|
_, err := io.CopyN(hash, rand.Reader, 50)
|
||||||
|
if err != nil {
|
||||||
|
return "????????????????????"
|
||||||
|
}
|
||||||
|
md := hash.Sum(nil)
|
||||||
|
mdStr := hex.EncodeToString(md)
|
||||||
|
return mdStr[0:20]
|
||||||
|
}
|
||||||
|
|
||||||
// Init reset the session as the init status.
|
// Init reset the session as the init status.
|
||||||
func (session *Session) Init() {
|
func (session *Session) Init() {
|
||||||
session.statement = statements.NewStatement(
|
session.statement = statements.NewStatement(
|
||||||
|
@ -119,7 +135,11 @@ func (session *Session) Init() {
|
||||||
session.lastSQL = ""
|
session.lastSQL = ""
|
||||||
session.lastSQLArgs = []interface{}{}
|
session.lastSQLArgs = []interface{}{}
|
||||||
|
|
||||||
|
if session.engine.logSessionID {
|
||||||
|
session.ctx = context.WithValue(session.engine.defaultContext, log.SessionIDKey, newSessionID())
|
||||||
|
} else {
|
||||||
session.ctx = session.engine.defaultContext
|
session.ctx = session.engine.defaultContext
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Close release the connection from pool
|
// Close release the connection from pool
|
||||||
|
@ -264,12 +284,12 @@ func (session *Session) Cascade(trueOrFalse ...bool) *Session {
|
||||||
}
|
}
|
||||||
|
|
||||||
// MustLogSQL means record SQL or not and don't follow engine's setting
|
// MustLogSQL means record SQL or not and don't follow engine's setting
|
||||||
func (session *Session) MustLogSQL(log ...bool) *Session {
|
func (session *Session) MustLogSQL(logs ...bool) *Session {
|
||||||
var showSQL = true
|
var showSQL = true
|
||||||
if len(log) > 0 {
|
if len(logs) > 0 {
|
||||||
showSQL = log[0]
|
showSQL = logs[0]
|
||||||
}
|
}
|
||||||
session.ctx = context.WithValue(session.ctx, "__xorm_show_sql", showSQL)
|
session.ctx = context.WithValue(session.ctx, log.SessionShowSQLKey, showSQL)
|
||||||
return session
|
return session
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
1
xorm.go
1
xorm.go
|
@ -51,6 +51,7 @@ func NewEngine(driverName string, dataSourceName string) (*Engine, error) {
|
||||||
driverName: driverName,
|
driverName: driverName,
|
||||||
dataSourceName: dataSourceName,
|
dataSourceName: dataSourceName,
|
||||||
db: db,
|
db: db,
|
||||||
|
logSessionID: false,
|
||||||
}
|
}
|
||||||
|
|
||||||
if dialect.URI().DBType == schemas.SQLITE {
|
if dialect.URI().DBType == schemas.SQLITE {
|
||||||
|
|
Loading…
Reference in New Issue