@@ -12,15 +12,11 @@ import (
12
12
type (
13
13
// Log defines the LabStack log service.
14
14
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
24
20
}
25
21
26
22
// Level defines the log level.
@@ -51,50 +47,17 @@ var levels = map[Level]string{
51
47
LevelFatal : "FATAL" ,
52
48
}
53
49
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 {
89
51
le := new (LogError )
90
- _ , err := l .sling .Post ("" ).BodyJSON (l . listEntries () ).Receive (nil , le )
52
+ _ , err := l .sling .Post ("" ).BodyJSON (entry ).Receive (nil , le )
91
53
if err != nil {
54
+ le .Message = err .Error ()
92
55
return err
93
56
}
94
- if le .Code = = 0 {
95
- return nil
57
+ if le .Code ! = 0 {
58
+ return le
96
59
}
97
- return le
60
+ return nil
98
61
}
99
62
100
63
// Debug logs a message with DEBUG level.
@@ -128,33 +91,17 @@ func (l *Log) Log(level Level, fields Fields) {
128
91
return
129
92
}
130
93
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
143
95
fields .Add ("time" , time .Now ().Format (rfc3339Milli )).
144
96
Add ("level" , levels [level ])
145
97
for k , v := range l .Fields {
146
98
fields .Add (k , v )
147
99
}
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 )
158
105
}
159
106
}
160
107
0 commit comments