forked from bincooo/chatgpt-adapter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
com.go
159 lines (134 loc) · 3.28 KB
/
com.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package logger
import (
nested "github.com/antonfisher/nested-logrus-formatter"
rotatelogs "github.com/lestrrat-go/file-rotatelogs"
"github.com/sirupsen/logrus"
"io"
"os"
"path"
"path/filepath"
"runtime"
"strconv"
"strings"
"time"
)
var (
project = ""
projectDir = ""
)
func InitLogger(basePath string, level logrus.Level) {
logrus.SetLevel(level)
if len(basePath) == 0 {
basePath = "log"
}
writer, err := rotatelogs.New(
filepath.Join(basePath, "background-%Y-%m-%d.log"),
//日志最大保存时间
rotatelogs.WithMaxAge(7*24*time.Hour),
////设置日志切割时间间隔(1天)(隔多久分割一次)
rotatelogs.WithRotationTime(24*time.Hour),
)
if err != nil {
Fatal(err)
}
writers := []io.Writer{writer, os.Stdout}
logrus.SetOutput(io.MultiWriter(writers...))
logrus.SetFormatter(&nested.Formatter{
HideKeys: true,
TimestampFormat: "2006-01-02 15:04:05",
CallerFirst: true,
NoColors: true,
CustomCallerFormatter: CustomCallerFormatter,
})
logrus.SetReportCaller(true)
}
func CustomCallerFormatter(frame *runtime.Frame) string {
trimPackage := func(pkg string) string {
if pkg == "" {
return pkg
}
slice := strings.Split(pkg, "/")
length := len(slice)
if length <= 2 {
return pkg
}
return slice[length-2] + "/" + slice[length-1]
}
trimL := func(prefix string) string {
if prefix == "" {
return prefix
}
if strings.HasPrefix(prefix, "/") {
return prefix[1:]
}
return prefix
}
trimProject := func(file string) string {
if !strings.HasPrefix(file, project+"/") {
return file
}
return file[len(project)+1:]
}
// 尝试获取上层栈
pcs := make([]uintptr, 10)
depth := runtime.Callers(10, pcs)
frames := runtime.CallersFrames(pcs[:depth])
for f, next := frames.Next(); next; f, next = frames.Next() {
if f.PC == frame.PC {
if f, next = frames.Next(); next {
frame = &f
break
}
}
}
main := strings.HasPrefix(frame.Function, "main.")
slice := strings.Split(frame.File, trimPackage(path.Dir(frame.Function)))
if !main && len(slice) > 1 {
return " <" + trimProject(path.Dir(frame.Function)) + "> " + trimL(slice[1]) + ":" + strconv.Itoa(frame.Line) + " |"
}
root := path.Dir(frame.Function)
if main {
root = "main"
}
file := frame.File
if strings.HasPrefix(file, projectDir+"/") {
file = file[len(projectDir)+1:]
}
return " <" + trimProject(root) + "> " + file + ":" + strconv.Itoa(frame.Line) + " |"
}
func Trace(args ...interface{}) {
logrus.Trace(args...)
}
func Tracef(format string, args ...interface{}) {
logrus.Tracef(format, args...)
}
func Debug(args ...interface{}) {
logrus.Debug(args...)
}
func Debugf(format string, args ...interface{}) {
logrus.Debugf(format, args...)
}
func Info(args ...interface{}) {
logrus.Info(args...)
}
func Infof(format string, args ...interface{}) {
logrus.Infof(format, args...)
}
func Warn(args ...interface{}) {
logrus.Warn(args...)
}
func Warnf(format string, args ...interface{}) {
logrus.Warnf(format, args...)
}
func Error(args ...interface{}) {
logrus.Error(args...)
}
func Errorf(format string, args ...interface{}) {
logrus.Errorf(format, args...)
}
func Fatal(args ...interface{}) {
logrus.Fatal(args...)
}
func Fatalf(format string, args ...interface{}) {
logrus.Fatalf(format, args...)
}