Permalink
Browse files

Make sure timestamp is formatted as a float

  • Loading branch information...
1 parent 71cc65f commit d2901861671f274b20090dc6c78e62477d054a9a Dmitriy Kalinin and Pieter Noordhuis committed Mar 12, 2013
Showing with 17 additions and 3 deletions.
  1. +7 −0 json_codec_test.go
  2. +1 −1 json_prettifier.go
  3. +9 −2 record.go
View
@@ -36,3 +36,10 @@ func (s *JsonCodecSuite) TestJsonCodec(c *C) {
c.Check(string(m), Matches, fmt.Sprintf(`{.*"%s":.*}`, f))
}
}
+
+func (s *JsonCodecSuite) TestTimestampIsFormattedAsFloat(c *C) {
+ r := NewRecord("source", LOG_INFO, "Hello world", nil)
+ m, err := s.EncodeRecord(r)
+ c.Assert(err, IsNil)
+ c.Assert(string(m), Matches, `.*"timestamp":\d{10}\.\d{9},.*`)
+}
View
@@ -76,7 +76,7 @@ func encodeLevel(level LogLevel) string {
return fmt.Sprintf("%s ", strings.ToUpper(level.String()))
}
-func encodeTimestamp(t float64) string {
+func encodeTimestamp(t RecordTimestamp) string {
ut := time.Unix(int64(t), 0)
return fmt.Sprintf("%s ", ut.Format("2006-01-02 15:04:05"))
}
View
@@ -1,14 +1,21 @@
package steno
import (
+ "fmt"
"os"
"runtime"
"strings"
"time"
)
+type RecordTimestamp float64
+
+func (t RecordTimestamp) MarshalJSON() ([]byte, error) {
+ return []byte(fmt.Sprintf("%.9f", t)), nil
+}
+
type Record struct {
- Timestamp float64 `json:"timestamp"`
+ Timestamp RecordTimestamp `json:"timestamp"`
Pid int `json:"process_id"`
Source string `json:"source"`
Level LogLevel `json:"log_level"`
@@ -27,7 +34,7 @@ func init() {
func NewRecord(s string, l LogLevel, m string, d map[string]interface{}) *Record {
r := &Record{
- Timestamp: float64(time.Now().UnixNano()) / 1000000000,
+ Timestamp: RecordTimestamp(time.Now().UnixNano()) / 1000000000,
Pid: pid,
Source: s,
Level: l,

0 comments on commit d290186

Please sign in to comment.