"ntfy tier" CLI command
This commit is contained in:
parent
e3b39f670f
commit
a32e8abc12
8 changed files with 140 additions and 40 deletions
14
log/event.go
14
log/event.go
|
@ -11,13 +11,14 @@ import (
|
|||
)
|
||||
|
||||
const (
|
||||
tagField = "tag"
|
||||
errorField = "error"
|
||||
tagField = "tag"
|
||||
errorField = "error"
|
||||
timestampFormat = "2006-01-02T15:04:05.999Z07:00"
|
||||
)
|
||||
|
||||
// Event represents a single log event
|
||||
type Event struct {
|
||||
Timestamp int64 `json:"time"`
|
||||
Timestamp string `json:"time"`
|
||||
Level Level `json:"level"`
|
||||
Message string `json:"message"`
|
||||
fields Context
|
||||
|
@ -25,8 +26,9 @@ type Event struct {
|
|||
|
||||
// newEvent creates a new log event
|
||||
func newEvent() *Event {
|
||||
now := time.Now()
|
||||
return &Event{
|
||||
Timestamp: time.Now().UnixMilli(),
|
||||
Timestamp: now.Format(timestampFormat),
|
||||
fields: make(Context),
|
||||
}
|
||||
}
|
||||
|
@ -70,8 +72,8 @@ func (e *Event) Tag(tag string) *Event {
|
|||
}
|
||||
|
||||
// Time sets the time field
|
||||
func (e *Event) Time(time time.Time) *Event {
|
||||
e.Timestamp = time.UnixMilli()
|
||||
func (e *Event) Time(t time.Time) *Event {
|
||||
e.Timestamp = t.Format(timestampFormat)
|
||||
return e
|
||||
}
|
||||
|
||||
|
|
|
@ -35,7 +35,7 @@ func TestLog_TagContextFieldFields(t *testing.T) {
|
|||
Tag("mytag").
|
||||
Field("field2", 123).
|
||||
Field("field1", "value1").
|
||||
Time(time.Unix(123, 0)).
|
||||
Time(time.Unix(123, 999000000).UTC()).
|
||||
Info("hi there %s", "phil")
|
||||
log.
|
||||
Tag("not-stripe").
|
||||
|
@ -48,11 +48,11 @@ func TestLog_TagContextFieldFields(t *testing.T) {
|
|||
}).
|
||||
Tag("stripe").
|
||||
Err(err).
|
||||
Time(time.Unix(456, 0)).
|
||||
Time(time.Unix(456, 123000000).UTC()).
|
||||
Debug("Subscription status %s", "active")
|
||||
|
||||
expected := `{"time":123000,"level":"INFO","message":"hi there phil","field1":"value1","field2":123,"tag":"mytag"}
|
||||
{"time":456000,"level":"DEBUG","message":"Subscription status active","error":"some error","error_code":123,"stripe_customer_id":"acct_123","stripe_subscription_id":"sub_123","tag":"stripe","user_id":"u_abc","visitor_ip":"1.2.3.4"}
|
||||
expected := `{"time":"1970-01-01T00:02:03.999Z","level":"INFO","message":"hi there phil","field1":"value1","field2":123,"tag":"mytag"}
|
||||
{"time":"1970-01-01T00:07:36.123Z","level":"DEBUG","message":"Subscription status active","error":"some error","error_code":123,"stripe_customer_id":"acct_123","stripe_subscription_id":"sub_123","tag":"stripe","user_id":"u_abc","visitor_ip":"1.2.3.4"}
|
||||
`
|
||||
require.Equal(t, expected, out.String())
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue