|
@@ -3,6 +3,7 @@ package gelf
|
|
import (
|
|
import (
|
|
"bytes"
|
|
"bytes"
|
|
"encoding/json"
|
|
"encoding/json"
|
|
|
|
+ "fmt"
|
|
"time"
|
|
"time"
|
|
)
|
|
)
|
|
|
|
|
|
@@ -84,29 +85,35 @@ func (m *Message) UnmarshalJSON(data []byte) error {
|
|
m.Extra[k] = v
|
|
m.Extra[k] = v
|
|
continue
|
|
continue
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ ok := true
|
|
switch k {
|
|
switch k {
|
|
case "version":
|
|
case "version":
|
|
- m.Version = v.(string)
|
|
|
|
|
|
+ m.Version, ok = v.(string)
|
|
case "host":
|
|
case "host":
|
|
- m.Host = v.(string)
|
|
|
|
|
|
+ m.Host, ok = v.(string)
|
|
case "short_message":
|
|
case "short_message":
|
|
- m.Short = v.(string)
|
|
|
|
|
|
+ m.Short, ok = v.(string)
|
|
case "full_message":
|
|
case "full_message":
|
|
- m.Full = v.(string)
|
|
|
|
|
|
+ m.Full, ok = v.(string)
|
|
case "timestamp":
|
|
case "timestamp":
|
|
- m.TimeUnix = v.(float64)
|
|
|
|
|
|
+ m.TimeUnix, ok = v.(float64)
|
|
case "level":
|
|
case "level":
|
|
- m.Level = int32(v.(float64))
|
|
|
|
|
|
+ var level float64
|
|
|
|
+ level, ok = v.(float64)
|
|
|
|
+ m.Level = int32(level)
|
|
case "facility":
|
|
case "facility":
|
|
- m.Facility = v.(string)
|
|
|
|
|
|
+ m.Facility, ok = v.(string)
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if !ok {
|
|
|
|
+ return fmt.Errorf("invalid type for field %s", k)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return nil
|
|
return nil
|
|
}
|
|
}
|
|
|
|
|
|
-func (m *Message) toBytes() (messageBytes []byte, err error) {
|
|
|
|
- buf := newBuffer()
|
|
|
|
- defer bufPool.Put(buf)
|
|
|
|
|
|
+func (m *Message) toBytes(buf *bytes.Buffer) (messageBytes []byte, err error) {
|
|
if err = m.MarshalJSONBuf(buf); err != nil {
|
|
if err = m.MarshalJSONBuf(buf); err != nil {
|
|
return nil, err
|
|
return nil, err
|
|
}
|
|
}
|
|
@@ -134,7 +141,7 @@ func constructMessage(p []byte, hostname string, facility string, file string, l
|
|
Host: hostname,
|
|
Host: hostname,
|
|
Short: string(short),
|
|
Short: string(short),
|
|
Full: string(full),
|
|
Full: string(full),
|
|
- TimeUnix: float64(time.Now().Unix()),
|
|
|
|
|
|
+ TimeUnix: float64(time.Now().UnixNano()) / float64(time.Second),
|
|
Level: 6, // info
|
|
Level: 6, // info
|
|
Facility: facility,
|
|
Facility: facility,
|
|
Extra: map[string]interface{}{
|
|
Extra: map[string]interface{}{
|