Skip to content

Commit

Permalink
support any parseable logfmt or json key-value, not just those with time
Browse files Browse the repository at this point in the history
  • Loading branch information
aybabtme committed Dec 10, 2019
1 parent 6838ddf commit f20667e
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 30 deletions.
13 changes: 0 additions & 13 deletions json_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,19 +60,6 @@ func (h *JSONHandler) clear() {

// TryHandle tells if this line was handled by this handler.
func (h *JSONHandler) TryHandle(d []byte) bool {
var ok bool

for _, field := range supportedTimeFields {
ok = bytes.Contains(d, []byte(field))
if ok {
break
}
}

if !ok {
return false
}

if !h.UnmarshalJSON(d) {
h.clear()
return false
Expand Down
20 changes: 3 additions & 17 deletions logfmt_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,29 +42,15 @@ func (h *LogfmtHandler) clear() {

// CanHandle tells if this line can be handled by this handler.
func (h *LogfmtHandler) TryHandle(d []byte) bool {
var ok bool
for _, field := range supportedTimeFields {
ok = bytes.Contains(d, []byte(field))
if ok {
break
}
}

if !ok {
return false
}

err := h.UnmarshalLogfmt(d)
if err != nil {
if !h.UnmarshalLogfmt(d) {
h.clear()
return false
}
return true
}

// HandleLogfmt sets the fields of the handler.
func (h *LogfmtHandler) UnmarshalLogfmt(data []byte) error {

func (h *LogfmtHandler) UnmarshalLogfmt(data []byte) bool {
dec := logfmt.NewDecoder(bytes.NewReader(data))
for dec.ScanRecord() {
next_kv:
Expand Down Expand Up @@ -113,7 +99,7 @@ func (h *LogfmtHandler) UnmarshalLogfmt(data []byte) error {
h.setField(key, val)
}
}
return dec.Err()
return dec.Err() == nil
}

// Prettify the output in a logrus like fashion.
Expand Down

0 comments on commit f20667e

Please sign in to comment.