Skip to content

Commit

Permalink
merge filename/custom key names pull requests
Browse files Browse the repository at this point in the history
  • Loading branch information
inconshreveable committed Sep 3, 2014
2 parents 2eae4d8 + aebbe3e commit 99cdef5
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 12 deletions.
8 changes: 4 additions & 4 deletions format.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func TerminalFormat() Format {
//
func LogfmtFormat() Format {
return FormatFunc(func(r *Record) []byte {
common := []interface{}{"t", r.Time, "lvl", r.Lvl, "msg", r.Msg}
common := []interface{}{r.KeyNames.Time, r.Time, r.KeyNames.Lvl, r.Lvl, r.KeyNames.Msg, r.Msg}
buf := &bytes.Buffer{}
logfmt(buf, append(common, r.Ctx...), 0)
return buf.Bytes()
Expand Down Expand Up @@ -134,9 +134,9 @@ func JsonFormatEx(pretty, lineSeparated bool) Format {
return FormatFunc(func(r *Record) []byte {
props := make(map[string]interface{})

props["t"] = r.Time
props["lvl"] = r.Lvl
props["msg"] = r.Msg
props[r.KeyNames.Time] = r.Time
props[r.KeyNames.Lvl] = r.Lvl
props[r.KeyNames.Msg] = r.Msg

for i := 0; i < len(r.Ctx); i += 2 {
k, ok := r.Ctx[i].(string)
Expand Down
6 changes: 3 additions & 3 deletions handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,11 +168,11 @@ func FilterHandler(fn func(r *Record) bool, h Handler) Handler {
func MatchFilterHandler(key string, value interface{}, h Handler) Handler {
return FilterHandler(func(r *Record) (pass bool) {
switch key {
case "lvl":
case r.KeyNames.Lvl:
return r.Lvl == value
case "t":
case r.KeyNames.Time:
return r.Time == value
case "msg":
case r.KeyNames.Msg:
return r.Msg == value
}

Expand Down
24 changes: 19 additions & 5 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import (
"time"
)

const timeKey = "t"
const lvlKey = "lvl"
const msgKey = "msg"
const errorKey = "LOG15_ERROR"

type Lvl int
Expand Down Expand Up @@ -58,11 +60,18 @@ func LvlFromString(lvlString string) (Lvl, error) {

// A Record is what a Logger asks its handler to write
type Record struct {
Time time.Time
Lvl Lvl
Msg string
Ctx []interface{}
CallPC [1]uintptr
Time time.Time
Lvl Lvl
Msg string
Ctx []interface{}
CallPC [1]uintptr
KeyNames RecordKeyNames
}

type RecordKeyNames struct {
Time string
Msg string
Lvl string
}

// A Logger writes key/value pairs to a Handler
Expand Down Expand Up @@ -92,6 +101,11 @@ func (l *logger) write(msg string, lvl Lvl, ctx []interface{}) {
Lvl: lvl,
Msg: msg,
Ctx: append(l.ctx, normalize(ctx)...),
KeyNames: RecordKeyNames{
Time: timeKey,
Msg: msgKey,
Lvl: lvlKey,
},
}
runtime.Callers(3, r.CallPC[:])
l.h.Log(&r)
Expand Down

0 comments on commit 99cdef5

Please sign in to comment.