Skip to content

Commit 61f944d

Browse files
committed
feat(0.7.x): 日只优化
1 parent 9b426d1 commit 61f944d

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

jd_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"io/ioutil"
99
"os"
1010
"testing"
11+
"time"
1112
)
1213

1314
type LocalConfig struct {
@@ -148,6 +149,7 @@ func TestGoodsServiceImpl_GoodsGigfieldQuery(t *testing.T) {
148149
func TestGoodsServiceImpl_GoodsJingfenQuery(t *testing.T) {
149150
config := GetConfig()
150151
service := NewJosService(config.AppKey, config.SecretKey, "")
152+
log.FileLog("logs/", "jd-go.log", 7, 1*time.Hour, 24*time.Minute)
151153
goodsService := service.GetGoodsService()
152154
res, err := goodsService.GoodsJingfenQuery(&GoodsJingfenQueryRequest{
153155
EliteId: 33,

log/log.go

+23
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/sirupsen/logrus"
1010
"os"
1111
"path"
12+
"runtime"
1213
"strings"
1314
"time"
1415
)
@@ -95,6 +96,7 @@ func (l *LoggerImpl) Trace(args ...interface{}) {
9596

9697
func (l *LoggerImpl) Logf(level Level, format string, args ...interface{}) {
9798
if l.checkLevel(level) {
99+
l.Line()
98100
n := strings.Count(format, "%s")
99101
le := len(args)
100102
for i := 0; i < (le - n); i++ {
@@ -119,6 +121,27 @@ func (l *LoggerImpl) checkLevel(level Level) bool {
119121
return uint64(l.Level) >= uint64(level)
120122
}
121123

124+
func (l LoggerImpl) Line() {
125+
_, fullFile, line, ok := runtime.Caller(5) // 5 skip: 指的是跳过多少个引用
126+
file := fullFile
127+
if file != "" {
128+
// Truncate file name at last file name separator.
129+
if index := strings.LastIndex(file, "/"); index >= 0 {
130+
file = file[index+1:]
131+
} else if index = strings.LastIndex(file, "\\"); index >= 0 {
132+
file = file[index+1:]
133+
}
134+
} else {
135+
file = "???"
136+
}
137+
if ok {
138+
buf := new(strings.Builder)
139+
140+
_, _ = fmt.Fprintf(buf, "\033[35m[jd-go]\033[0m: %s:%d", fullFile, line)
141+
l.Logger.Log(l.Level, buf.String())
142+
}
143+
}
144+
122145
// record output file log
123146
func (l *LoggerImpl) FileLog(logPath string, logFileName string, maxNum uint, maxAge, rotationTime time.Duration) {
124147

0 commit comments

Comments
 (0)