This repository has been archived by the owner on Jun 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
logger_context.go
121 lines (99 loc) · 2.93 KB
/
logger_context.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// 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"
"fmt"
"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
}
// SQLLogger represents an interface to log SQL
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{}
)
// enumerate all the context keys
var (
SessionIDKey = "__xorm_session_id"
SessionShowSQLKey = "__xorm_show_sql"
)
// LoggerAdapter wraps a Logger interafce as LoggerContext interface
type LoggerAdapter struct {
logger Logger
}
// NewLoggerAdapter creates an adapter for old xorm logger interface
func NewLoggerAdapter(logger Logger) ContextLogger {
return &LoggerAdapter{
logger: logger,
}
}
// BeforeSQL implements ContextLogger
func (l *LoggerAdapter) BeforeSQL(ctx LogContext) {}
// AfterSQL implements ContextLogger
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 {
l.logger.Infof("[SQL]%s %s %v - %v", sessionPart, ctx.SQL, ctx.Args, ctx.ExecuteTime)
} else {
l.logger.Infof("[SQL]%s %s %v", sessionPart, ctx.SQL, ctx.Args)
}
}
// Debugf implements ContextLogger
func (l *LoggerAdapter) Debugf(format string, v ...interface{}) {
l.logger.Debugf(format, v...)
}
// Errorf implements ContextLogger
func (l *LoggerAdapter) Errorf(format string, v ...interface{}) {
l.logger.Errorf(format, v...)
}
// Infof implements ContextLogger
func (l *LoggerAdapter) Infof(format string, v ...interface{}) {
l.logger.Infof(format, v...)
}
// Warnf implements ContextLogger
func (l *LoggerAdapter) Warnf(format string, v ...interface{}) {
l.logger.Warnf(format, v...)
}
// Level implements ContextLogger
func (l *LoggerAdapter) Level() LogLevel {
return l.logger.Level()
}
// SetLevel implements ContextLogger
func (l *LoggerAdapter) SetLevel(lv LogLevel) {
l.logger.SetLevel(lv)
}
// ShowSQL implements ContextLogger
func (l *LoggerAdapter) ShowSQL(show ...bool) {
l.logger.ShowSQL(show...)
}
// IsShowSQL implements ContextLogger
func (l *LoggerAdapter) IsShowSQL() bool {
return l.logger.IsShowSQL()
}