Interface

To make it easy to integrate with various of logging libraries, the Logger interface only requires the Print method. You could either use the logger that we provided, or implement the Logger interface by your own.

  1. type Logger interface {
  2. Print(pairs ...interface{})
  3. }

Usage

Print to Standard Output

You could use NewHelper create a log.Helper to wrap the logger struct, the helper will provide many log methods of different logging levels.

  1. logger := log.NewStdLogger()
  2. log := log.NewHelper("module_name", logger)
  3. // Levels
  4. log.Info("some log")
  5. log.Infof("format %s", "some log")
  6. log.Infow("field_name", "some log")

Print to Fluentd

To print to fluentd, you could “github.com/go-kratos/fluent”.

  1. import "github.com/go-kratos/fluent"
  2. addr := "unix:///var/run/fluent/fluent.sock"
  3. logger,err := fluent.NewLogger(addr)
  4. if err != nil {
  5. return
  6. }
  7. log := log.NewHelper("module_name", logger)
  8. // Levels
  9. log.Info("some log")
  10. log.Infof("format %s", "some log")
  11. log.Infow("field_name", "some log")

Logging middleware

To enable logging information of every requests, you could add logging.Server() in http.ServerOption.

  1. var opts = []http.ServerOption{
  2. http.Middleware(
  3. middleware.Chain(
  4. logging.Server(),
  5. ),
  6. ),
  7. }
  8. http.NewServer(opts...)