-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
29 changed files
with
1,776 additions
and
13 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
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 |
---|---|---|
|
@@ -69,5 +69,4 @@ func TestClientX_BatchWatch(t *testing.T) { | |
|
||
x.Close() | ||
time.Sleep(time.Second) | ||
|
||
} |
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
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
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
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,60 @@ | ||
package xlogger | ||
|
||
import "strings" | ||
|
||
// Level is a logger level. | ||
type Level int8 | ||
|
||
// LevelKey is logger level key. | ||
const LevelKey = "level" | ||
|
||
const ( | ||
// LevelDebug is logger debug level. | ||
LevelDebug Level = iota - 1 | ||
// LevelInfo is logger info level. | ||
LevelInfo | ||
// LevelWarn is logger warn level. | ||
LevelWarn | ||
// LevelError is logger error level. | ||
LevelError | ||
// LevelFatal is logger fatal level | ||
LevelFatal | ||
) | ||
|
||
func (l Level) Key() string { | ||
return LevelKey | ||
} | ||
|
||
func (l Level) String() string { | ||
switch l { | ||
case LevelDebug: | ||
return "DEBUG" | ||
case LevelInfo: | ||
return "INFO" | ||
case LevelWarn: | ||
return "WARN" | ||
case LevelError: | ||
return "ERROR" | ||
case LevelFatal: | ||
return "FATAL" | ||
default: | ||
return "" | ||
} | ||
} | ||
|
||
// ParseLevel parses a level string into a logger Level value. | ||
func ParseLevel(s string) Level { | ||
switch strings.ToUpper(s) { | ||
case "DEBUG": | ||
return LevelDebug | ||
case "INFO": | ||
return LevelInfo | ||
case "WARN": | ||
return LevelWarn | ||
case "ERROR": | ||
return LevelError | ||
case "FATAL": | ||
return LevelFatal | ||
} | ||
return LevelInfo | ||
} |
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,101 @@ | ||
package xlogger | ||
|
||
import "testing" | ||
|
||
func TestLevel_Key(t *testing.T) { | ||
if LevelInfo.Key() != LevelKey { | ||
t.Errorf("want: %s, got: %s", LevelKey, LevelInfo.Key()) | ||
} | ||
} | ||
|
||
func TestLevel_String(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
l Level | ||
want string | ||
}{ | ||
{ | ||
name: "DEBUG", | ||
l: LevelDebug, | ||
want: "DEBUG", | ||
}, | ||
{ | ||
name: "INFO", | ||
l: LevelInfo, | ||
want: "INFO", | ||
}, | ||
{ | ||
name: "WARN", | ||
l: LevelWarn, | ||
want: "WARN", | ||
}, | ||
{ | ||
name: "ERROR", | ||
l: LevelError, | ||
want: "ERROR", | ||
}, | ||
{ | ||
name: "FATAL", | ||
l: LevelFatal, | ||
want: "FATAL", | ||
}, | ||
{ | ||
name: "other", | ||
l: 10, | ||
want: "", | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if got := tt.l.String(); got != tt.want { | ||
t.Errorf("String() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestParseLevel(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
s string | ||
want Level | ||
}{ | ||
{ | ||
name: "DEBUG", | ||
want: LevelDebug, | ||
s: "DEBUG", | ||
}, | ||
{ | ||
name: "INFO", | ||
want: LevelInfo, | ||
s: "INFO", | ||
}, | ||
{ | ||
name: "WARN", | ||
want: LevelWarn, | ||
s: "WARN", | ||
}, | ||
{ | ||
name: "ERROR", | ||
want: LevelError, | ||
s: "ERROR", | ||
}, | ||
{ | ||
name: "FATAL", | ||
want: LevelFatal, | ||
s: "FATAL", | ||
}, | ||
{ | ||
name: "other", | ||
want: LevelInfo, | ||
s: "other", | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if got := ParseLevel(tt.s); got != tt.want { | ||
t.Errorf("ParseLevel() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |
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,63 @@ | ||
package xlogger | ||
|
||
import ( | ||
"context" | ||
"log" | ||
) | ||
|
||
// DefaultLogger is default logger. | ||
var DefaultLogger = NewStdLogger(log.Writer()) | ||
|
||
// Logger is a logger interface. | ||
type Logger interface { | ||
Log(level Level, keyvals ...interface{}) error | ||
} | ||
|
||
type logger struct { | ||
logger Logger | ||
prefix []interface{} | ||
hasValuer bool | ||
ctx context.Context | ||
} | ||
|
||
func (c *logger) Log(level Level, keyvals ...interface{}) error { | ||
kvs := make([]interface{}, 0, len(c.prefix)+len(keyvals)) | ||
kvs = append(kvs, c.prefix...) | ||
if c.hasValuer { | ||
bindValues(c.ctx, kvs) | ||
} | ||
kvs = append(kvs, keyvals...) | ||
return c.logger.Log(level, kvs...) | ||
} | ||
|
||
// With with logger fields. | ||
func With(l Logger, kv ...interface{}) Logger { | ||
c, ok := l.(*logger) | ||
if !ok { | ||
return &logger{logger: l, prefix: kv, hasValuer: containsValuer(kv), ctx: context.Background()} | ||
} | ||
kvs := make([]interface{}, 0, len(c.prefix)+len(kv)) | ||
kvs = append(kvs, c.prefix...) | ||
kvs = append(kvs, kv...) | ||
return &logger{ | ||
logger: c.logger, | ||
prefix: kvs, | ||
hasValuer: containsValuer(kvs), | ||
ctx: c.ctx, | ||
} | ||
} | ||
|
||
// WithContext returns a shallow copy of l with its context changed | ||
// to ctx. The provided ctx must be non-nil. | ||
func WithContext(ctx context.Context, l Logger) Logger { | ||
c, ok := l.(*logger) | ||
if !ok { | ||
return &logger{logger: l, ctx: ctx} | ||
} | ||
return &logger{ | ||
logger: c.logger, | ||
prefix: c.prefix, | ||
hasValuer: c.hasValuer, | ||
ctx: ctx, | ||
} | ||
} |
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,17 @@ | ||
package xlogger | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
) | ||
|
||
func TestInfo(_ *testing.T) { | ||
logger := DefaultLogger | ||
logger = With(logger, "ts", DefaultTimestamp) | ||
logger = With(logger, "caller", DefaultCaller) | ||
_ = logger.Log(LevelInfo, "key1", "value1") | ||
} | ||
|
||
func TestWithContext(_ *testing.T) { | ||
WithContext(context.Background(), nil) | ||
} |
Oops, something went wrong.