log: add level printer (#918)

* add level printer

* fix helper args

* fix test

* Update README.md
pull/920/head
Tony Chen 4 years ago committed by GitHub
parent 24b1ca6bc3
commit d78eb3ee4b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      app.go
  2. 2
      config/config.go
  3. 5
      examples/blog/internal/data/article.go
  4. 2
      examples/blog/internal/data/data.go
  5. 3
      examples/blog/internal/service/article.go
  6. 2
      examples/helloworld/server/main.go
  7. 45
      log/README.md
  8. 50
      log/helper.go
  9. 13
      log/helper_test.go
  10. 30
      log/log.go
  11. 19
      log/log_test.go
  12. 15
      log/std.go
  13. 8
      log/std_test.go
  14. 9
      log/value_test.go
  15. 31
      log/verbose.go
  16. 12
      log/verbose_test.go
  17. 4
      middleware/logging/logging.go
  18. 2
      middleware/recovery/recovery.go
  19. 2
      transport/grpc/resolver/discovery/builder.go
  20. 2
      transport/grpc/resolver/discovery/resolver_test.go
  21. 6
      transport/grpc/server.go
  22. 6
      transport/http/server.go

@ -42,7 +42,7 @@ func New(opts ...Option) *App {
ctx: ctx,
cancel: cancel,
instance: buildInstance(options),
log: log.NewHelper("app", options.logger),
log: log.NewHelper(options.logger),
}
}

@ -54,7 +54,7 @@ func New(opts ...Option) Config {
return &config{
opts: options,
reader: newReader(options),
log: log.NewHelper("config", options.logger),
log: log.NewHelper(options.logger),
}
}

@ -2,9 +2,10 @@ package data
import (
"context"
"time"
"github.com/go-kratos/kratos/examples/blog/internal/biz"
"github.com/go-kratos/kratos/v2/log"
"time"
)
type articleRepo struct {
@ -16,7 +17,7 @@ type articleRepo struct {
func NewArticleRepo(data *Data, logger log.Logger) biz.ArticleRepo {
return &articleRepo{
data: data,
log: log.NewHelper("article_repo", logger),
log: log.NewHelper(logger),
}
}

@ -25,7 +25,7 @@ type Data struct {
// NewData .
func NewData(conf *conf.Data, logger log.Logger) (*Data, func(), error) {
log := log.NewHelper("data", logger)
log := log.NewHelper(logger)
client, err := ent.Open(
conf.Database.Driver,
conf.Database.Source,

@ -2,6 +2,7 @@ package service
import (
"context"
"go.opentelemetry.io/otel"
pb "github.com/go-kratos/kratos/examples/blog/api/blog/v1"
@ -13,7 +14,7 @@ import (
func NewBlogService(article *biz.ArticleUsecase, logger log.Logger) *BlogService {
return &BlogService{
article: article,
log: log.NewHelper("article", logger),
log: log.NewHelper(logger),
}
}

@ -43,7 +43,7 @@ func (s *server) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloRe
func main() {
logger := log.NewStdLogger(os.Stdout)
log := log.NewHelper("main", logger)
log := log.NewHelper(logger)
grpcSrv := grpc.NewServer(
grpc.Address(":9000"),

@ -5,41 +5,20 @@
### Structured logging
```go
logger := log.NewStdLogger(os.Stdout)
logger = log.With(logger, "caller", log.DefaultCaller, "ts", log.DefaultTimestamp)
Logger logger = log.MultiLogger(log.NewStdLogger(os.Stdout), syslog.NewLogger())
// Levels
log.Debug(logger).Log("msg", "foo bar")
log.Info(logger).Log("msg", "foo bar")
log.Warn(logger).Log("msg", "foo bar")
log.Error(logger).Log("msg", "foo bar")
errLogger := log.Error(logger)
errLogger.Log("msg", "xxx")
errLogger.Log("msg", "yyy")
errLogger.Log("msg", "zzz")
errLogger.Log(
"http.scheme", "https",
"http.host", "translate.googleapis.com",
"http.target", "/language/translate",
"http.method", "post",
"http.status_code", 500,
"http.flavor", "1.1.",
"http.user_agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36",
logger = log.With(logger,
"service.name", "hellworld",
"service.version", "v1.0.0",
"ts", log.DefaultTimestamp,
"caller", log.DefaultCaller,
)
// Helper
logger := log.NewHelper("github.com/project/foo", log.DefaultLogger)
logger.Info("hello")
logger.Infof("foo %s", "bar")
logger.Infow("key", "value")
logger.Log(log.LevelInfo, "key", "value")
// Verbose
v := NewVerbose(log.DefaultLogger, 20)
v.V(10).Log("foo", "bar1")
v.V(20).Log("foo", "bar2")
v.V(30).Log("foo", "bar3")
Helper helper = log.NewHelper(logger)
helper.Log(log.LevelInfo, "key", "value")
helper.Info("info message")
helper.Infof("info %s", "message")
helper.Infow("key", "value")
```

@ -6,79 +6,77 @@ import (
// Helper is a logger helper.
type Helper struct {
debug Logger
info Logger
warn Logger
err Logger
logger Logger
}
// NewHelper new a logger helper.
func NewHelper(name string, logger Logger) *Helper {
logger = With(logger, "module", name)
func NewHelper(logger Logger) *Helper {
return &Helper{
debug: Debug(logger),
info: Info(logger),
warn: Warn(logger),
err: Error(logger),
logger: logger,
}
}
// Log .
func (h *Helper) Log(level Level, keyvals ...interface{}) {
h.logger.Log(level, keyvals...)
}
// Debug logs a message at debug level.
func (h *Helper) Debug(a ...interface{}) {
h.debug.Log("msg", fmt.Sprint(a...))
h.logger.Log(LevelDebug, "msg", fmt.Sprint(a...))
}
// Debugf logs a message at debug level.
func (h *Helper) Debugf(format string, a ...interface{}) {
h.debug.Log("msg", fmt.Sprintf(format, a...))
h.logger.Log(LevelDebug, "msg", fmt.Sprintf(format, a...))
}
// Debugw logs a message at debug level.
func (h *Helper) Debugw(kv ...interface{}) {
h.debug.Log(kv...)
func (h *Helper) Debugw(keyvals ...interface{}) {
h.logger.Log(LevelDebug, keyvals...)
}
// Info logs a message at info level.
func (h *Helper) Info(a ...interface{}) {
h.info.Log("msg", fmt.Sprint(a...))
h.logger.Log(LevelInfo, "msg", fmt.Sprint(a...))
}
// Infof logs a message at info level.
func (h *Helper) Infof(format string, a ...interface{}) {
h.info.Log("msg", fmt.Sprintf(format, a...))
h.logger.Log(LevelInfo, "msg", fmt.Sprintf(format, a...))
}
// Infow logs a message at info level.
func (h *Helper) Infow(kv ...interface{}) {
h.info.Log(kv...)
func (h *Helper) Infow(keyvals ...interface{}) {
h.logger.Log(LevelInfo, keyvals...)
}
// Warn logs a message at warn level.
func (h *Helper) Warn(a ...interface{}) {
h.warn.Log("msg", fmt.Sprint(a...))
h.logger.Log(LevelWarn, "msg", fmt.Sprint(a...))
}
// Warnf logs a message at warnf level.
func (h *Helper) Warnf(format string, a ...interface{}) {
h.warn.Log("msg", fmt.Sprintf(format, a...))
h.logger.Log(LevelWarn, "msg", fmt.Sprintf(format, a...))
}
// Warnw logs a message at warnf level.
func (h *Helper) Warnw(kv ...interface{}) {
h.warn.Log(kv...)
func (h *Helper) Warnw(keyvals ...interface{}) {
h.logger.Log(LevelWarn, keyvals...)
}
// Error logs a message at error level.
func (h *Helper) Error(a ...interface{}) {
h.err.Log("msg", fmt.Sprint(a...))
h.logger.Log(LevelError, "msg", fmt.Sprint(a...))
}
// Errorf logs a message at error level.
func (h *Helper) Errorf(format string, a ...interface{}) {
h.err.Log("msg", fmt.Sprintf(format, a...))
h.logger.Log(LevelError, "msg", fmt.Sprintf(format, a...))
}
// Errorw logs a message at error level.
func (h *Helper) Errorw(kv ...interface{}) {
h.err.Log(kv...)
func (h *Helper) Errorw(keyvals ...interface{}) {
h.logger.Log(LevelError, keyvals...)
}

@ -6,16 +6,17 @@ import (
)
func TestHelper(t *testing.T) {
logger := With(DefaultLogger, "caller", DefaultCaller, "ts", DefaultTimestamp)
log := NewHelper("test", logger)
logger := With(DefaultLogger, "ts", DefaultTimestamp, "caller", DefaultCaller)
log := NewHelper(logger)
log.Log(LevelDebug, "msg", "test debug")
log.Debug("test debug")
log.Debugf("test %s", "debug")
log.Debugw("log", "test debug")
}
func TestHelperLevel(t *testing.T) {
log := NewHelper("test", DefaultLogger)
log := NewHelper(DefaultLogger)
log.Debug("test debug")
log.Info("test info")
log.Warn("test warn")
@ -23,21 +24,21 @@ func TestHelperLevel(t *testing.T) {
}
func BenchmarkHelperPrint(b *testing.B) {
log := NewHelper("test", NewStdLogger(ioutil.Discard))
log := NewHelper(NewStdLogger(ioutil.Discard))
for i := 0; i < b.N; i++ {
log.Debug("test")
}
}
func BenchmarkHelperPrintf(b *testing.B) {
log := NewHelper("test", NewStdLogger(ioutil.Discard))
log := NewHelper(NewStdLogger(ioutil.Discard))
for i := 0; i < b.N; i++ {
log.Debugf("%s", "test")
}
}
func BenchmarkHelperPrintw(b *testing.B) {
log := NewHelper("test", NewStdLogger(ioutil.Discard))
log := NewHelper(NewStdLogger(ioutil.Discard))
for i := 0; i < b.N; i++ {
log.Debugw("key", "value")
}

@ -11,7 +11,7 @@ var (
// Logger is a logger interface.
type Logger interface {
Log(kv ...interface{}) error
Log(level Level, keyvals ...interface{}) error
}
type context struct {
@ -20,15 +20,15 @@ type context struct {
hasValuer bool
}
func (c *context) Log(kv ...interface{}) error {
kvs := make([]interface{}, 0, len(c.prefix)+len(kv))
func (c *context) Log(level Level, keyvals ...interface{}) error {
kvs := make([]interface{}, 0, len(c.prefix)+len(keyvals))
kvs = append(kvs, c.prefix...)
if c.hasValuer {
bindValues(kvs)
}
kvs = append(kvs, kv...)
kvs = append(kvs, keyvals...)
for _, l := range c.logs {
if err := l.Log(kvs...); err != nil {
if err := l.Log(level, kvs...); err != nil {
return err
}
}
@ -54,23 +54,3 @@ func With(l Logger, kv ...interface{}) Logger {
func MultiLogger(logs ...Logger) Logger {
return &context{logs: logs}
}
// Debug returns a debug logger.
func Debug(log Logger) Logger {
return With(log, LevelKey, LevelDebug)
}
// Info returns a info logger.
func Info(log Logger) Logger {
return With(log, LevelKey, LevelInfo)
}
// Warn return a warn logger.
func Warn(log Logger) Logger {
return With(log, LevelKey, LevelWarn)
}
// Error returns a error logger.
func Error(log Logger) Logger {
return With(log, LevelKey, LevelError)
}

@ -5,27 +5,16 @@ import (
"testing"
)
func TestLogger(t *testing.T) {
logger := DefaultLogger
Debug(logger).Log("msg", "test debug")
Info(logger).Log("msg", "test info")
Warn(logger).Log("msg", "test warn")
Error(logger).Log("msg", "test error")
}
func TestInfo(t *testing.T) {
logger := DefaultLogger
logger = With(logger, "caller", DefaultCaller, "ts", DefaultTimestamp)
infoLogger := Info(logger)
infoLogger.Log("key1", "value1")
infoLogger.Log("key2", "value2")
infoLogger.Log("key3", "value3")
logger = With(logger, "ts", DefaultTimestamp, "caller", DefaultCaller)
logger.Log(LevelInfo, "key1", "value1")
}
func TestWrapper(t *testing.T) {
out := NewStdLogger(os.Stdout)
err := NewStdLogger(os.Stderr)
l := With(MultiLogger(out, err), "caller", DefaultCaller, "ts", DefaultTimestamp)
l.Log("msg", "test")
l := With(MultiLogger(out, err), "ts", DefaultTimestamp, "caller", DefaultCaller)
l.Log(LevelInfo, "msg", "test")
}

@ -15,7 +15,7 @@ type stdLogger struct {
pool *sync.Pool
}
// NewStdLogger new a std logger with options.
// NewStdLogger new a logger with writer.
func NewStdLogger(w io.Writer) Logger {
return &stdLogger{
log: log.New(w, "", 0),
@ -28,16 +28,17 @@ func NewStdLogger(w io.Writer) Logger {
}
// Log print the kv pairs log.
func (l *stdLogger) Log(kv ...interface{}) error {
if len(kv) == 0 {
func (l *stdLogger) Log(level Level, keyvals ...interface{}) error {
if len(keyvals) == 0 {
return nil
}
if len(kv)%2 != 0 {
kv = append(kv, "")
if len(keyvals)%2 != 0 {
keyvals = append(keyvals, "")
}
buf := l.pool.Get().(*bytes.Buffer)
for i := 0; i < len(kv); i += 2 {
fmt.Fprintf(buf, "%s=%v ", kv[i], kv[i+1])
buf.WriteString(level.String())
for i := 0; i < len(keyvals); i += 2 {
fmt.Fprintf(buf, " %s=%v", keyvals[i], keyvals[i+1])
}
l.log.Output(4, buf.String())
buf.Reset()

@ -6,8 +6,8 @@ func TestStdLogger(t *testing.T) {
logger := DefaultLogger
logger = With(logger, "caller", DefaultCaller, "ts", DefaultTimestamp)
Debug(logger).Log("msg", "test debug")
Info(logger).Log("msg", "test info")
Warn(logger).Log("msg", "test warn")
Error(logger).Log("msg", "test error")
logger.Log(LevelInfo, "msg", "test debug")
logger.Log(LevelInfo, "msg", "test info")
logger.Log(LevelInfo, "msg", "test warn")
logger.Log(LevelInfo, "msg", "test error")
}

@ -4,11 +4,6 @@ import "testing"
func TestValue(t *testing.T) {
logger := DefaultLogger
logger = With(logger, "caller", DefaultCaller, "ts", DefaultTimestamp)
logger.Log("msg", "helloworld")
Debug(logger).Log("msg", "debug value")
Info(logger).Log("msg", "info value")
Warn(logger).Log("msg", "warn value")
Error(logger).Log("msg", "error value")
logger = With(logger, "ts", DefaultTimestamp, "caller", DefaultCaller)
logger.Log(LevelInfo, "msg", "helloworld")
}

@ -1,31 +0,0 @@
package log
// Verbose is a verbose type that implements Logger Print.
type Verbose struct {
log Logger
level Level
enabled bool
}
// NewVerbose new a verbose with level.
func NewVerbose(log Logger, level Level) Verbose {
return Verbose{log: log, level: level}
}
// Enabled will return true if this log level is enabled, guarded by the value of v.
func (v Verbose) Enabled(level Level) bool {
return v.level <= level
}
// V reports whether verbosity at the call site is at least the requested level.
func (v Verbose) V(level Level) Verbose {
return Verbose{log: v.log, enabled: v.Enabled(level)}
}
// Log is equivalent to the Print function, guarded by the value of v.
func (v Verbose) Log(a ...interface{}) error {
if v.enabled {
return v.log.Log(a...)
}
return nil
}

@ -1,12 +0,0 @@
package log
import "testing"
func TestVerbose(t *testing.T) {
logger := With(DefaultLogger, "caller", DefaultCaller, "ts", DefaultTimestamp)
v := NewVerbose(logger, 20)
v.V(10).Log("foo", "bar1")
v.V(20).Log("foo", "bar2")
v.V(30).Log("foo", "bar3")
}

@ -15,7 +15,7 @@ import (
// Server is an server logging middleware.
func Server(l log.Logger) middleware.Middleware {
logger := log.NewHelper("middleware/logging", l)
logger := log.NewHelper(l)
return func(handler middleware.Handler) middleware.Handler {
return func(ctx context.Context, req interface{}) (interface{}, error) {
var (
@ -99,7 +99,7 @@ func Server(l log.Logger) middleware.Middleware {
// Client is an client logging middleware.
func Client(l log.Logger) middleware.Middleware {
logger := log.NewHelper("middleware/logging", l)
logger := log.NewHelper(l)
return func(handler middleware.Handler) middleware.Handler {
return func(ctx context.Context, req interface{}) (interface{}, error) {
var (

@ -46,7 +46,7 @@ func Recovery(opts ...Option) middleware.Middleware {
for _, o := range opts {
o(&options)
}
logger := log.NewHelper("middleware/recovery", options.logger)
logger := log.NewHelper(options.logger)
return func(handler middleware.Handler) middleware.Handler {
return func(ctx context.Context, req interface{}) (reply interface{}, err error) {
defer func() {

@ -46,9 +46,9 @@ func (d *builder) Build(target resolver.Target, cc resolver.ClientConn, opts res
r := &discoveryResolver{
w: w,
cc: cc,
log: log.NewHelper("grpc/resolver/discovery", d.logger),
ctx: ctx,
cancel: cancel,
log: log.NewHelper(d.logger),
}
go r.watch()
return r, nil

@ -45,7 +45,7 @@ func TestWatch(t *testing.T) {
r := &discoveryResolver{
w: &testWatch{},
cc: &testClientConn{te: t},
log: log.NewHelper("grpc/resolver/discovery", log.DefaultLogger),
log: log.NewHelper(log.DefaultLogger),
ctx: ctx,
cancel: cancel,
}

@ -19,8 +19,6 @@ import (
"google.golang.org/grpc/reflection"
)
const loggerName = "transport/grpc"
var _ transport.Server = (*Server)(nil)
// ServerOption is gRPC server option.
@ -50,7 +48,7 @@ func Timeout(timeout time.Duration) ServerOption {
// Logger with server logger.
func Logger(logger log.Logger) ServerOption {
return func(s *Server) {
s.log = log.NewHelper(loggerName, logger)
s.log = log.NewHelper(logger)
}
}
@ -88,11 +86,11 @@ func NewServer(opts ...ServerOption) *Server {
network: "tcp",
address: ":0",
timeout: time.Second,
log: log.NewHelper(loggerName, log.DefaultLogger),
middleware: middleware.Chain(
recovery.Recovery(),
),
health: health.NewServer(),
log: log.NewHelper(log.DefaultLogger),
}
for _, o := range opts {
o(srv)

@ -15,8 +15,6 @@ import (
"github.com/gorilla/mux"
)
const loggerName = "transport/http"
var _ transport.Server = (*Server)(nil)
// ServerOption is HTTP server option.
@ -46,7 +44,7 @@ func Timeout(timeout time.Duration) ServerOption {
// Logger with server logger.
func Logger(logger log.Logger) ServerOption {
return func(s *Server) {
s.log = log.NewHelper(loggerName, logger)
s.log = log.NewHelper(logger)
}
}
@ -67,7 +65,7 @@ func NewServer(opts ...ServerOption) *Server {
network: "tcp",
address: ":0",
timeout: time.Second,
log: log.NewHelper(loggerName, log.DefaultLogger),
log: log.NewHelper(log.DefaultLogger),
}
for _, o := range opts {
o(srv)

Loading…
Cancel
Save