-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Based on Logrus's code - Add Logger - Add Entry - Add Filter - Add PkgFilter
- Loading branch information
Showing
6 changed files
with
117 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# Log that support filter by field | ||
|
||
- [ ] thread safe | ||
- [ ] log to stdout | ||
- [ ] leveled logging | ||
- [ ] support filed | ||
- [ ] filter by field |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/* | ||
Package log can filter log by field and support multiple level | ||
*/ | ||
package log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package log | ||
|
||
type Entry struct { | ||
Fields Fields | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package log | ||
|
||
type Set map[string]bool | ||
|
||
// Filter determines if the entry should be logged | ||
type Filter interface { | ||
Filter(entry *Entry) bool | ||
} | ||
|
||
// PkgFilter only allows entry without `pkg` field or `pkg` value in the allow set to pass | ||
type PkgFilter struct { | ||
allow Set | ||
} | ||
|
||
func (filter *PkgFilter) Filter(entry *Entry) bool { | ||
pkg, ok := entry.Fields["pkg"] | ||
// entry without pkg is not filtered | ||
if !ok { | ||
return true | ||
} | ||
_, ok = filter.allow[pkg] | ||
return ok | ||
} | ||
|
||
func NewPkgFilter(allow Set) *PkgFilter { | ||
return &PkgFilter{ | ||
allow: allow, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package log | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestFilterInterface(t *testing.T) { | ||
assert := assert.New(t) | ||
|
||
allow := make(map[string]bool) | ||
allow["ayi.app.git"] = true | ||
f := NewPkgFilter(allow) | ||
entryWithoutField := &Entry{} | ||
assert.True(f.Filter(entryWithoutField)) | ||
field := make(map[string]string, 1) | ||
field["pkg"] = "ayi.app.git" | ||
entryWithAllowedPkg := &Entry{Fields: field} | ||
assert.True(f.Filter(entryWithAllowedPkg)) | ||
field["pkg"] = "ayi.app.web" | ||
entryWithDisallowedPkg := &Entry{Fields: field} | ||
assert.False(f.Filter(entryWithDisallowedPkg)) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package log | ||
|
||
import ( | ||
"io" | ||
"os" | ||
"sync" | ||
) | ||
|
||
// Level is log level | ||
type Level uint8 | ||
|
||
const ( | ||
// PanicLevel log error and call painc | ||
PanicLevel Level = iota | ||
// FatalLevel log error and call `os.Exit(1)` | ||
FatalLevel | ||
// ErrorLevel log error | ||
ErrorLevel | ||
// WarnLevel log warning | ||
WarnLevel | ||
// InfoLevel log info | ||
InfoLevel | ||
// DebugLevel log debug message, user should enable DebugLevel logging when report bug | ||
DebugLevel | ||
// TraceLevel is used by developer only, user should stop at DebugLevel if they just want to report | ||
TraceLevel | ||
) | ||
|
||
// Fields is key-value string pair to annotate the log and can be used by filter | ||
type Fields map[string]string | ||
|
||
type Logger struct { | ||
Out io.Writer | ||
Level Level | ||
mu sync.Mutex | ||
} | ||
|
||
// NewLogger returns a new logger using StdOut and InfoLevel | ||
func NewLogger() *Logger { | ||
return &Logger{ | ||
Out: os.Stdout, | ||
Level: InfoLevel, | ||
} | ||
} | ||
|
||
func (log *Logger) AddFilter(filter Filter, level Level) { | ||
} |