Skip to content

Commit e12fd1e

Browse files
committed
Bumped v0.7.1
Signed-off-by: Vishal Rana <[email protected]>
1 parent 5200b02 commit e12fd1e

File tree

2 files changed

+22
-79
lines changed

2 files changed

+22
-79
lines changed

client.go

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -69,16 +69,12 @@ func (c *Client) Email() *Email {
6969

7070
// Log returns the log service.
7171
func (c *Client) Log() (log *Log) {
72-
log = &Log{
73-
sling: c.sling.Path("/log"),
74-
logger: c.logger,
75-
Level: LevelInfo,
76-
Fields: Fields{},
77-
BatchSize: 60,
78-
DispatchInterval: 60,
72+
return &Log{
73+
sling: c.sling.Path("/log"),
74+
logger: c.logger,
75+
Level: LevelInfo,
76+
Fields: Fields{},
7977
}
80-
log.resetEntries()
81-
return
8278
}
8379

8480
// Store returns the store service.

log.go

Lines changed: 17 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,11 @@ import (
1212
type (
1313
// Log defines the LabStack log service.
1414
Log struct {
15-
sling *sling.Sling
16-
entries []Fields
17-
timer <-chan time.Time
18-
mutex sync.RWMutex
19-
logger *glog.Logger
20-
Level Level
21-
Fields Fields
22-
BatchSize int
23-
DispatchInterval int
15+
sling *sling.Sling
16+
mutex sync.RWMutex
17+
logger *glog.Logger
18+
Level Level
19+
Fields Fields
2420
}
2521

2622
// Level defines the log level.
@@ -51,50 +47,17 @@ var levels = map[Level]string{
5147
LevelFatal: "FATAL",
5248
}
5349

54-
func (l *Log) resetEntries() {
55-
l.mutex.Lock()
56-
defer l.mutex.Unlock()
57-
l.entries = make([]Fields, 0, l.BatchSize)
58-
}
59-
60-
func (l *Log) appendEntry(f Fields) {
61-
l.mutex.Lock()
62-
defer l.mutex.Unlock()
63-
l.entries = append(l.entries, f)
64-
}
65-
66-
func (l *Log) listEntries() []Fields {
67-
l.mutex.Lock()
68-
defer l.mutex.Unlock()
69-
entries := make([]Fields, len(l.entries))
70-
for i, entry := range l.entries {
71-
entries[i] = entry
72-
}
73-
return entries
74-
}
75-
76-
func (l *Log) logsLength() int {
77-
l.mutex.RLock()
78-
defer l.mutex.RUnlock()
79-
return len(l.entries)
80-
}
81-
82-
func (l *Log) dispatch() error {
83-
if len(l.entries) == 0 {
84-
return nil
85-
}
86-
87-
defer l.resetEntries()
88-
50+
func (l *Log) write(entry Fields) error {
8951
le := new(LogError)
90-
_, err := l.sling.Post("").BodyJSON(l.listEntries()).Receive(nil, le)
52+
_, err := l.sling.Post("").BodyJSON(entry).Receive(nil, le)
9153
if err != nil {
54+
le.Message = err.Error()
9255
return err
9356
}
94-
if le.Code == 0 {
95-
return nil
57+
if le.Code != 0 {
58+
return le
9659
}
97-
return le
60+
return nil
9861
}
9962

10063
// Debug logs a message with DEBUG level.
@@ -128,33 +91,17 @@ func (l *Log) Log(level Level, fields Fields) {
12891
return
12992
}
13093

131-
if l.timer == nil {
132-
l.timer = time.Tick(time.Duration(l.DispatchInterval) * time.Second)
133-
go func() {
134-
for range l.timer {
135-
if err := l.dispatch(); err != nil {
136-
err := err.(*LogError)
137-
fmt.Printf("log error: code=%d, message=%s", err.Code, err.Message)
138-
}
139-
}
140-
}()
141-
}
142-
94+
// Log fields
14395
fields.Add("time", time.Now().Format(rfc3339Milli)).
14496
Add("level", levels[level])
14597
for k, v := range l.Fields {
14698
fields.Add(k, v)
14799
}
148-
l.appendEntry(fields)
149-
150-
// Dispatch batch
151-
if l.logsLength() >= l.BatchSize {
152-
go func() {
153-
if err := l.dispatch(); err != nil {
154-
err := err.(*LogError)
155-
fmt.Printf("log error: code=%d, message=%s", err.Code, err.Message)
156-
}
157-
}()
100+
101+
// Write log
102+
if err := l.write(fields); err != nil {
103+
err := err.(*LogError)
104+
fmt.Printf("log error: code=%d, message=%s", err.Code, err.Message)
158105
}
159106
}
160107

0 commit comments

Comments
 (0)