Skip to content
This repository has been archived by the owner on Feb 9, 2022. It is now read-only.

Commit

Permalink
Make sure timestamp is formatted as a float
Browse files Browse the repository at this point in the history
  • Loading branch information
Dmitriy Kalinin and Pieter Noordhuis committed Mar 12, 2013
1 parent 71cc65f commit d290186
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
7 changes: 7 additions & 0 deletions json_codec_test.go
Expand Up @@ -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},.*`)
}
2 changes: 1 addition & 1 deletion json_prettifier.go
Expand Up @@ -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"))
}
Expand Down
11 changes: 9 additions & 2 deletions record.go
@@ -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"`
Expand All @@ -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,
Expand Down

0 comments on commit d290186

Please sign in to comment.