Skip to content

Commit cc85b34

Browse files
committed
Tests for logging
Signed-off-by: Vishal Rana <[email protected]>
1 parent 48fadd2 commit cc85b34

11 files changed

+154
-12
lines changed

.travis.yml

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
language: go
2+
go:
3+
- 1.7
4+
- 1.8
5+
- tip
6+
install:
7+
- make dependency
8+
script:
9+
- make test
10+
after_success:
11+
- bash <(curl -s https://codecov.io/bash)
12+
matrix:
13+
allow_failures:
14+
- go: tip

Gopkg.lock

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Gopkg.toml

+4
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,7 @@
2424
[[constraint]]
2525
name = "github.com/dghubble/sling"
2626
version = "1.1.0"
27+
28+
[[constraint]]
29+
name = "github.com/stretchr/testify"
30+
version = "1.1.4"

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2017 LabStack
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

Makefile

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
dependency:
2+
go get -u github.com/golang/dep/cmd/dep
3+
dep ensure -update
4+
5+
test:
6+
echo "" > coverage.txt
7+
for d in $(shell go list ./... | grep -v vendor); do \
8+
go test -race -coverprofile=profile.out -covermode=atomic $$d || exit 1; \
9+
[ -f profile.out ] && cat profile.out >> coverage.txt && rm profile.out; \
10+
done

README.md

+8
Original file line numberDiff line numberDiff line change
@@ -1 +1,9 @@
11
# LabStack Go Client
2+
3+
4+
[![GoDoc](http://img.shields.io/badge/go-documentation-blue.svg?style=flat-square)](http://godoc.org/github.com/labstack/labstack-go)
5+
[![Go Report Card](https://goreportcard.com/badge/github.com/labstack/labstack-go?style=flat-square)](https://goreportcard.com/report/github.com/labstack/labstack-go)
6+
[![Build Status](http://img.shields.io/travis/labstack/labstack-go.svg?style=flat-square)](https://travis-ci.org/labstack/labstack-go)
7+
[![Codecov](https://img.shields.io/codecov/c/github/labstack/labstack-go.svg?style=flat-square)](https://codecov.io/gh/labstack/labstack-go) [![Forum](https://img.shields.io/badge/community-forum-00afd1.svg?style=flat-square)](https://forum.labstack.com)
8+
[![Twitter](https://img.shields.io/badge/[email protected]?style=flat-square)](https://twitter.com/labstack)
9+
[![License](http://img.shields.io/badge/license-mit-blue.svg?style=flat-square)](https://raw.githubusercontent.com/labstack/labstack-go/master/LICENSE)

cube.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ func (c *Cube) dispatch() (err error) {
9898
return
9999
}
100100
if res.StatusCode != http.StatusNoContent {
101-
return fmt.Errorf("cube: requests dispatching error=%s", err)
101+
return fmt.Errorf("cube: error dispatching requests, status=%d, message=%v", res.StatusCode, err)
102102
}
103103
return
104104
}

labstack.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ type (
1515
}
1616
)
1717

18-
const (
18+
var (
1919
apiURL = "https://api.labstack.com"
2020
)
2121

logging.go

+19-9
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ func (l *Logging) dispatch() (err error) {
8585
return
8686
}
8787
if res.StatusCode != http.StatusNoContent {
88-
return fmt.Errorf("cube: requests dispatching error=%s", err)
88+
return fmt.Errorf("logging: error dispatching logs, status=%d, message=%v", res.StatusCode, err)
8989
}
9090
return
9191
}
@@ -112,35 +112,45 @@ func (c *Client) Logging() (logging *Logging) {
112112

113113
// Debug logs a debug message.
114114
func (l *Logging) Debug(format string, args ...interface{}) {
115-
l.Log(DEBUG, format, args)
115+
l.Log(DEBUG, format, args...)
116116
}
117117

118118
// Info logs an informational message.
119119
func (l *Logging) Info(format string, args ...interface{}) {
120-
l.Log(INFO, format, args)
120+
l.Log(INFO, format, args...)
121121
}
122122

123123
// Warn logs a warning message.
124124
func (l *Logging) Warn(format string, args ...interface{}) {
125-
l.Log(WARN, format, args)
125+
l.Log(WARN, format, args...)
126126
}
127127

128128
// Error logs an error message.
129129
func (l *Logging) Error(format string, args ...interface{}) {
130-
l.Log(ERROR, format, args)
130+
l.Log(ERROR, format, args...)
131131
}
132132

133133
// Log logs a message with log level.
134134
func (l *Logging) Log(level, format string, args ...interface{}) {
135-
if levels[l.Level] < levels[level] {
135+
if levels[level] < levels[l.Level] {
136136
return
137137
}
138-
message := fmt.Sprintf(format, args)
138+
message := fmt.Sprintf(format, args...)
139139
log := &Log{
140-
Time: time.Now().Format(time.RFC3339),
140+
Time: time.Now().Format(RFC3339Milli),
141141
Module: l.Module,
142142
Level: level,
143143
Message: message,
144144
}
145-
l.sling.Post("").BodyJSON(log).ReceiveSuccess(nil)
145+
l.appendLog(log)
146+
147+
// Dispatch batch
148+
if l.logsLength() >= l.BatchSize {
149+
go func() {
150+
if err := l.dispatch(); err != nil {
151+
l.logger.Error(err)
152+
}
153+
l.resetLogs()
154+
}()
155+
}
146156
}

logging_test.go

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package labstack
2+
3+
import (
4+
"encoding/json"
5+
"net/http"
6+
"net/http/httptest"
7+
"testing"
8+
9+
"github.com/stretchr/testify/assert"
10+
)
11+
12+
func TestLogging(t *testing.T) {
13+
// dispatch
14+
handler := func(w http.ResponseWriter, r *http.Request) {
15+
logs := []*Log{}
16+
if err := json.NewDecoder(r.Body).Decode(&logs); err == nil {
17+
if assert.Len(t, logs, 1) {
18+
assert.Equal(t, INFO, logs[0].Level)
19+
assert.Equal(t, INFO, logs[0].Message)
20+
w.WriteHeader(http.StatusNoContent)
21+
return
22+
}
23+
}
24+
w.WriteHeader(http.StatusInternalServerError)
25+
}
26+
ts := httptest.NewServer(http.HandlerFunc(handler))
27+
defer ts.Close()
28+
apiURL = ts.URL
29+
l := NewClient("").Logging()
30+
l.appendLog(&Log{
31+
Level: INFO,
32+
Message: INFO,
33+
})
34+
assert.NoError(t, l.dispatch())
35+
36+
// Log
37+
for _, level := range []string{DEBUG, INFO, WARN, ERROR} {
38+
l := NewClient("").Logging()
39+
l.Level = DEBUG
40+
l.BatchSize = 1
41+
switch level {
42+
case DEBUG:
43+
l.Debug("%s", DEBUG)
44+
case INFO:
45+
l.Info("%s", INFO)
46+
case WARN:
47+
l.Warn("%s", WARN)
48+
case ERROR:
49+
l.Error("%s", ERROR)
50+
}
51+
if assert.Equal(t, 1, l.logsLength()) {
52+
log := l.logs[0]
53+
assert.Equal(t, level, log.Level)
54+
assert.Equal(t, level, log.Message)
55+
}
56+
}
57+
58+
// Level
59+
l = NewClient("").Logging()
60+
l.Level = ERROR
61+
l.Debug("debug")
62+
l.Info("info")
63+
l.Warn("warn")
64+
assert.Equal(t, 0, l.logsLength())
65+
l.Level = INFO
66+
l.Info("info")
67+
l.Warn("warn")
68+
assert.Equal(t, 2, l.logsLength())
69+
}

util.go

+6
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ import (
66
"strings"
77
)
88

9+
// Time layouts
10+
const (
11+
RFC3339Milli = "2006-01-02T15:04:05.000Z07:00"
12+
RFC3339Micro = "2006-01-02T15:04:05.000000Z07:00"
13+
)
14+
915
func realIP(r *http.Request) string {
1016
ra := r.RemoteAddr
1117
if ip := r.Header.Get("X-Forwarded-For"); ip != "" {

0 commit comments

Comments
 (0)