Skip to content

Commit 2c647ce

Browse files
committed
修复部分语法错误
1 parent d73b145 commit 2c647ce

File tree

13 files changed

+93
-59
lines changed

13 files changed

+93
-59
lines changed

Makefile

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
all: crab
22

33
FILES := $$(find . -name '*.go' | grep -vE 'vendor')
4-
SOURCE_PATH := handler orm validation cache server
4+
SOURCE_PATH := orm validation cache server log util
55

6-
unused:
7-
go get honnef.co/go/unused/cmd/unused
86

97
golint:
108
go get github.com/golang/lint/golint
119

12-
lint: golint unused
10+
megacheck:
11+
go get honnef.co/go/tools/cmd/megacheck
12+
13+
lint: golint megacheck
1314
@for path in $(SOURCE_PATH); do echo "golint $$path"; golint $$path"/..."; done;
14-
@for path in $(SOURCE_PATH); do echo "unused $$path"; unused "./"$$path; done;
1515
@for path in $(SOURCE_PATH); do echo "gofmt -s -l -w $$path"; gofmt -s -l -w $$path; done;
1616
go tool vet $(FILES) 2>&1
17-
go tool vet --shadow $(FILES) 2>&1
17+
megacheck ./...
1818

1919
clean:
2020
@rm -rf bin

cache/cache_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ func TestCacheActive(t *testing.T) {
1616
c := NewCache(2)
1717

1818
c.Add("1", &data)
19-
val := c.Get("1")
19+
c.Get("1")
2020
time.Sleep(time.Second)
21-
val = c.Get("1")
21+
val := c.Get("1")
2222
if val == nil {
2323
t.Fatalf("not found, expect %v", data)
2424
}
@@ -39,7 +39,7 @@ func TestCacheInactive(t *testing.T) {
3939

4040
time.Sleep(time.Second * 2)
4141

42-
val = c.Get("1")
42+
c.Get("1")
4343
if val != nil {
4444
t.Fatalf("expect not found")
4545
}

config/config_test.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,6 @@ type testConf struct {
7676
Enable bool
7777
HeaderSize int
7878
}
79-
80-
aaa int
8179
}
8280

8381
func TestConfigStruct(t *testing.T) {

http/client/client.go

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,16 @@ import (
1010
"time"
1111

1212
"github.com/juju/errors"
13-
"github.com/zssky/log"
13+
14+
"github.com/dearcode/crab/log"
1415
)
1516

1617
//HTTPClient 带超时重试控制的http客户端.
1718
type HTTPClient struct {
1819
retryTimes int
1920
timeout time.Duration
2021
client http.Client
22+
logger *log.Logger
2123
}
2224

2325
type StatusError struct {
@@ -37,17 +39,17 @@ func (c *HTTPClient) dial(network, addr string) (net.Conn, error) {
3739
if err == nil {
3840
break
3941
}
40-
log.Errorf("DialTimeout %s:%s error:%v retry:%v", network, addr, err, i+1)
42+
c.logger.Errorf("DialTimeout %s:%s error:%v retry:%v", network, addr, err, i+1)
4143
}
4244

4345
if err != nil {
44-
log.Errorf("DialTimeout %s:%s error:%v", network, addr, err)
46+
c.logger.Errorf("DialTimeout %s:%s error:%v", network, addr, err)
4547
return nil, errors.Trace(err)
4648
}
4749

4850
deadline := time.Now().Add(c.timeout)
4951
if err = conn.SetDeadline(deadline); err != nil {
50-
log.Errorf("SetDeadline %s:%s", network, addr)
52+
c.logger.Errorf("SetDeadline %s:%s", network, addr)
5153
conn.Close()
5254
return nil, errors.Trace(err)
5355
}
@@ -78,8 +80,9 @@ func (c *HTTPClient) Timeout(t int) *HTTPClient {
7880
return c
7981
}
8082

81-
func (c *HTTPClient) SetLogLevel(l string) *HTTPClient {
82-
log.SetLevelByString(l)
83+
//SetLogger 开启日志.
84+
func (c *HTTPClient) SetLogger(l *log.Logger) *HTTPClient {
85+
c.logger = l
8386
return c
8487
}
8588

@@ -128,7 +131,7 @@ func (c HTTPClient) GetJSON(url string, headers map[string]string, resp interfac
128131
if err != nil {
129132
return errors.Trace(err)
130133
}
131-
log.Debugf("url:%v, resp:%s", url, buf)
134+
c.logger.Debugf("url:%v, resp:%s", url, buf)
132135
return errors.Trace(json.Unmarshal(buf, resp))
133136
}
134137

@@ -153,7 +156,7 @@ func (c HTTPClient) PostJSON(url string, headers map[string]string, data interfa
153156
return errors.Trace(err)
154157
}
155158

156-
log.Debugf("url:%v, resp:%s", url, buf)
159+
c.logger.Debugf("url:%v, resp:%s", url, buf)
157160
return json.Unmarshal(buf, resp)
158161
}
159162

@@ -177,7 +180,7 @@ func (c HTTPClient) PutJSON(url string, headers map[string]string, data interfac
177180
if buf, err = c.do("PUT", url, headers, buf); err != nil {
178181
return errors.Trace(err)
179182
}
180-
log.Debugf("url:%v, resp:%s", url, buf)
183+
c.logger.Debugf("url:%v, resp:%s", url, buf)
181184
return json.Unmarshal(buf, resp)
182185
}
183186

@@ -192,6 +195,6 @@ func (c HTTPClient) DeleteJSON(url string, headers map[string]string, resp inter
192195
if err != nil {
193196
return errors.Trace(err)
194197
}
195-
log.Debugf("url:%v, resp:%s", url, buf)
198+
c.logger.Debugf("url:%v, resp:%s", url, buf)
196199
return json.Unmarshal(buf, resp)
197200
}

http/server/handler.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ import (
1313

1414
"github.com/google/btree"
1515
"github.com/juju/errors"
16-
"github.com/zssky/log"
16+
17+
"github.com/dearcode/crab/log"
1718
)
1819

1920
var (
@@ -118,7 +119,7 @@ var (
118119
)
119120

120121
func init() {
121-
exp, err := regexp.Compile("{(\\w+)?}")
122+
exp, err := regexp.Compile(`{(\w+)?}`)
122123
if err != nil {
123124
panic(err.Error())
124125
}
@@ -170,7 +171,7 @@ func register(obj interface{}, path string, isPrefix bool) error {
170171
case http.MethodPut:
171172
case http.MethodDelete:
172173
default:
173-
log.Warnf("ignore func:%v %v %v", method, path, rt)
174+
log.Warningf("ignore func:%v %v %v", method, path, rt)
174175
continue
175176
}
176177

@@ -307,8 +308,6 @@ func (s *httpServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
307308
log.Debugf("%v %v %v h:%#v", r.RemoteAddr, r.Method, r.URL, h)
308309

309310
h(w, nr)
310-
311-
return
312311
}
313312

314313
func defaultFilter(_ http.ResponseWriter, r *http.Request) *http.Request {

http/server/vars.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import (
99
"strconv"
1010

1111
"github.com/juju/errors"
12-
"github.com/zssky/log"
1312

13+
"github.com/dearcode/crab/log"
1414
"github.com/dearcode/crab/meta"
1515
"github.com/dearcode/crab/validation"
1616
)

log/log_level.go renamed to log/level.go

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
11
package log
22

3-
import ()
4-
5-
type LogLevel int
3+
//Level 日志级别.
4+
type Level int
65

76
const (
8-
LogFatal LogLevel = iota
7+
//LogFatal fatal.
8+
LogFatal Level = iota
9+
//LogError error.
910
LogError
11+
//LogWarning warning.
1012
LogWarning
13+
//LogInfo info.
1114
LogInfo
15+
//LogDebug debug.
1216
LogDebug
1317
)
1418

15-
//StringToLogLevel 字符串转LogLevel.
16-
func StringToLogLevel(level string) LogLevel {
19+
//stringToLevel 字符串转Level.
20+
func stringToLevel(level string) Level {
1721
switch level {
1822
case "fatal":
1923
return LogFatal
@@ -31,8 +35,8 @@ func StringToLogLevel(level string) LogLevel {
3135
return LogDebug
3236
}
3337

34-
//LogLevel loglevel 转字符串.
35-
func (l LogLevel) String() string {
38+
//Level Level 转字符串.
39+
func (l Level) String() string {
3640
switch l {
3741
case LogFatal:
3842
return "fatal"
@@ -48,8 +52,8 @@ func (l LogLevel) String() string {
4852
return "unknown"
4953
}
5054

51-
//LogLevel loglevel转颜色.
52-
func (l LogLevel) Color() string {
55+
//color Level转颜色.
56+
func (l Level) color() string {
5357
switch l {
5458
case LogFatal:
5559
return "\033[0;31m"

log/log.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,58 +6,72 @@ import (
66

77
var mlog = NewLogger()
88

9-
func SetLevel(level LogLevel) {
9+
//SetLevel 设置日志级别.
10+
func SetLevel(level Level) {
1011
mlog.SetLevel(level)
1112
}
1213

13-
func GetLogLevel() LogLevel {
14+
//GetLogLevel 获取日志级别.
15+
func GetLogLevel() Level {
1416
return mlog.level
1517
}
1618

19+
//Info .
1720
func Info(v ...interface{}) {
1821
mlog.write(LogInfo, fmt.Sprint(v...))
1922
}
2023

24+
//Infof .
2125
func Infof(format string, v ...interface{}) {
2226
mlog.write(LogInfo, format, v...)
2327
}
2428

29+
//Debug .
2530
func Debug(v ...interface{}) {
2631
mlog.write(LogDebug, fmt.Sprint(v...))
2732
}
2833

34+
//Debugf .
2935
func Debugf(format string, v ...interface{}) {
3036
mlog.write(LogDebug, format, v...)
3137
}
3238

39+
//Warning .
3340
func Warning(v ...interface{}) {
3441
mlog.write(LogWarning, fmt.Sprint(v...))
3542
}
3643

44+
//Warningf .
3745
func Warningf(format string, v ...interface{}) {
3846
mlog.write(LogWarning, format, v...)
3947
}
4048

49+
//Error .
4150
func Error(v ...interface{}) {
4251
mlog.write(LogError, fmt.Sprint(v...))
4352
}
4453

54+
//Errorf .
4555
func Errorf(format string, v ...interface{}) {
4656
mlog.write(LogError, format, v...)
4757
}
4858

59+
//Fatal .
4960
func Fatal(v ...interface{}) {
5061
mlog.write(LogFatal, fmt.Sprint(v...))
5162
}
5263

64+
//Fatalf .
5365
func Fatalf(format string, v ...interface{}) {
5466
mlog.write(LogFatal, format, v...)
5567
}
5668

69+
//SetLevelByString 设置日志级别.
5770
func SetLevelByString(level string) {
5871
mlog.SetLevelByString(level)
5972
}
6073

74+
//SetColor 设置是否显示颜色.
6175
func SetColor(color bool) {
6276
mlog.SetColor(color)
6377
}

log/log_test.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,5 @@ func TestLog(t *testing.T) {
1818
Infof("%v default has color test log", time.Now())
1919

2020
l.SetOutputFile("./vvv.log").SetRolling(true)
21-
l.Infof("vvvvvvvvv")
22-
23-
for i := 0; i < 999999999; i++ {
24-
time.Sleep(time.Second)
25-
l.Info(time.Now())
26-
}
21+
l.Info(time.Now())
2722
}

0 commit comments

Comments
 (0)