File tree Expand file tree Collapse file tree 3 files changed +19
-3
lines changed Expand file tree Collapse file tree 3 files changed +19
-3
lines changed Original file line number Diff line number Diff line change @@ -23,7 +23,11 @@ func Telescope() gin.HandlerFunc {
23
23
})
24
24
ctx .Abort ()
25
25
}
26
- log .WithContext (ctx ).WithFields (log.Fields {"type" : "request" }).Debug (ctx .Request .URL )
26
+ if errorRecord && hasError {
27
+ log .WithContext (ctx ).WithFields (log.Fields {"type" : "request" }).Error (ctx .Request .URL )
28
+ } else {
29
+ log .WithContext (ctx ).WithFields (log.Fields {"type" : "request" }).Debug (ctx .Request .URL )
30
+ }
27
31
TelescopeClose ()
28
32
}()
29
33
ctx .Set ("start" , time .Now ())
Original file line number Diff line number Diff line change @@ -24,16 +24,20 @@ type Providers struct {
24
24
}
25
25
26
26
var Routes = make (map [string ]Type )
27
+ var (
28
+ errorRecord bool //为true时,即使非debug模式也会开启望远镜,但只记录出错日志
29
+ hasError bool //标记错误日志,以便记录request为错误请求
30
+ )
27
31
28
32
// SetDB 任意框架下使用, 需要手动设置DB
29
33
func (t * Providers ) SetDB (db * gorm.DB ) {
30
34
t .Mysql = db
31
35
}
32
36
33
37
func (t * Providers ) Init () {
34
- if app .IsDebug () && ! t .init {
38
+ errorRecord = app .Config ("telescope.error_record" , false )
39
+ if (app .IsDebug () || errorRecord ) && ! t .init {
35
40
t .init = true
36
-
37
41
t .Register ()
38
42
}
39
43
}
@@ -76,6 +80,13 @@ func (t *telescopeHook) Init() {
76
80
}
77
81
78
82
func (t * telescopeHook ) Levels () []logrus.Level {
83
+ if ! app .IsDebug () || errorRecord {
84
+ return []logrus.Level {
85
+ logrus .PanicLevel ,
86
+ logrus .FatalLevel ,
87
+ logrus .ErrorLevel ,
88
+ }
89
+ }
79
90
return logrus .AllLevels
80
91
}
81
92
Original file line number Diff line number Diff line change @@ -20,6 +20,7 @@ func (b *Log) BindType() string {
20
20
21
21
func (b * Log ) Handler (entry * logrus.Entry ) (* entries , []tag ) {
22
22
if entry .Level <= logrus .ErrorLevel {
23
+ hasError = true
23
24
defer func () {
24
25
telescopeEntries , tags := NewException ().ToSave (string (debug .Stack ()), entry .Message )
25
26
NewtelescopeHook ().Save (telescopeEntries , tags )
You can’t perform that action at this time.
0 commit comments