Skip to content

Commit 1cfa02e

Browse files
committed
feat: 新增仅记录错误日志功能,默认关闭,于配置文件telescope.yml的error_record设为true即开启
1 parent a99906a commit 1cfa02e

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed

middleware.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,11 @@ func Telescope() gin.HandlerFunc {
2323
})
2424
ctx.Abort()
2525
}
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+
}
2731
TelescopeClose()
2832
}()
2933
ctx.Set("start", time.Now())

telescope_provider.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,20 @@ type Providers struct {
2424
}
2525

2626
var Routes = make(map[string]Type)
27+
var (
28+
errorRecord bool //为true时,即使非debug模式也会开启望远镜,但只记录出错日志
29+
hasError bool //标记错误日志,以便记录request为错误请求
30+
)
2731

2832
// SetDB 任意框架下使用, 需要手动设置DB
2933
func (t *Providers) SetDB(db *gorm.DB) {
3034
t.Mysql = db
3135
}
3236

3337
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 {
3540
t.init = true
36-
3741
t.Register()
3842
}
3943
}
@@ -76,6 +80,13 @@ func (t *telescopeHook) Init() {
7680
}
7781

7882
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+
}
7990
return logrus.AllLevels
8091
}
8192

type_log.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ func (b *Log) BindType() string {
2020

2121
func (b *Log) Handler(entry *logrus.Entry) (*entries, []tag) {
2222
if entry.Level <= logrus.ErrorLevel {
23+
hasError = true
2324
defer func() {
2425
telescopeEntries, tags := NewException().ToSave(string(debug.Stack()), entry.Message)
2526
NewtelescopeHook().Save(telescopeEntries, tags)

0 commit comments

Comments
 (0)