-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathLogger.go
More file actions
103 lines (89 loc) · 3.58 KB
/
Logger.go
File metadata and controls
103 lines (89 loc) · 3.58 KB
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
// Copyright (c) 2023 gpress Authors.
//
// This file is part of gpress.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package main
import (
"context"
"io"
"os"
"reflect"
"unsafe"
"gitee.com/chunanyong/zorm"
"github.com/cloudwego/hertz/pkg/common/hlog"
)
// levelError 日志级别,使用变量,提升日志级别的优先级
var levelError = func() hlog.Level {
hlog.SetLevel(hlog.LevelError)
return hlog.LevelError
}()
// InitLog 初始化日志文件
func InitLog() *os.File {
//获取默认的logger
defaultLogger := hlog.DefaultLogger()
//获取私有的 depth 属性
depthField := reflect.ValueOf(defaultLogger).Elem().FieldByName("depth")
// 将私有字段变为可写
depthFieldPtr := unsafe.Pointer(depthField.UnsafeAddr())
depthValue := reflect.NewAt(depthField.Type(), depthFieldPtr).Elem()
//将 depth 改成 5
depthValue.Set(reflect.ValueOf(5))
// 重新设置logger
hlog.SetLogger(defaultLogger)
hlog.SetSystemLogger(defaultLogger)
f, err := os.OpenFile("./gpress.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
panic(err)
}
// https://github.com/cloudwego/hertz/issues/292
//defer f.Close()
fileWriter := io.MultiWriter(f, os.Stdout)
hlog.SetOutput(fileWriter)
hlog.SetSilentMode(true)
//使用变量,提升日志级别的优先级
hlog.SetLevel(levelError)
zorm.FuncLogError = FuncLogError
zorm.FuncLogPanic = FuncLogPanic
zorm.FuncPrintSQL = FuncPrintSQL
return f
}
// LogCallDepth 记录日志调用层级,用于定位到业务层代码
// Log Call Depth Record the log call level, used to locate the business layer code
var LogCallDepth = 4
// FuncLogError 记录error日志
// FuncLogError Record error log
var FuncLogError func(ctx context.Context, err error) = defaultLogError
// FuncLogPanic 记录panic日志,默认使用"defaultLogPanic"实现
// FuncLogPanic Record panic log, using "defaultLogPanic" by default
var FuncLogPanic func(ctx context.Context, err error) = defaultLogPanic
// FuncPrintSQL 打印sql语句,参数和执行时间,小于0是禁用日志输出;等于0是只输出日志,不计算SQ执行时间;大于0是计算执行时间,并且大于指定值
// FuncPrintSQL Print sql statement and parameters
var FuncPrintSQL func(ctx context.Context, sqlstr string, args []interface{}, execSQLMillis int64) = defaultPrintSQL
func defaultLogError(ctx context.Context, err error) {
//log.Output(LogCallDepth, fmt.Sprintln(err))
hlog.Error(err)
}
func defaultLogPanic(ctx context.Context, err error) {
defaultLogError(ctx, err)
}
func defaultPrintSQL(ctx context.Context, sqlstr string, args []interface{}, execSQLMillis int64) {
if args != nil {
hlog.Errorf("sql:", sqlstr, ",args:", args, ",execSQLMillis:", execSQLMillis)
//log.Output(LogCallDepth, fmt.Sprintln("sql:", sqlstr, ",args:", args, ",execSQLMillis:", execSQLMillis))
} else {
hlog.Errorf("sql:", sqlstr, ",args: [] ", ",execSQLMillis:", execSQLMillis)
//log.Output(LogCallDepth, fmt.Sprintln("sql:", sqlstr, ",args: [] ", ",execSQLMillis:", execSQLMillis))
}
}