stdlib.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package log
  2. import (
  3. "io"
  4. "github.com/go-kit/log"
  5. )
  6. // StdlibWriter implements io.Writer by invoking the stdlib log.Print. It's
  7. // designed to be passed to a Go kit logger as the writer, for cases where
  8. // it's necessary to redirect all Go kit log output to the stdlib logger.
  9. //
  10. // If you have any choice in the matter, you shouldn't use this. Prefer to
  11. // redirect the stdlib log to the Go kit logger via NewStdlibAdapter.
  12. type StdlibWriter = log.StdlibWriter
  13. // StdlibAdapter wraps a Logger and allows it to be passed to the stdlib
  14. // logger's SetOutput. It will extract date/timestamps, filenames, and
  15. // messages, and place them under relevant keys.
  16. type StdlibAdapter = log.StdlibAdapter
  17. // StdlibAdapterOption sets a parameter for the StdlibAdapter.
  18. type StdlibAdapterOption = log.StdlibAdapterOption
  19. // TimestampKey sets the key for the timestamp field. By default, it's "ts".
  20. func TimestampKey(key string) StdlibAdapterOption {
  21. return log.TimestampKey(key)
  22. }
  23. // FileKey sets the key for the file and line field. By default, it's "caller".
  24. func FileKey(key string) StdlibAdapterOption {
  25. return log.FileKey(key)
  26. }
  27. // MessageKey sets the key for the actual log message. By default, it's "msg".
  28. func MessageKey(key string) StdlibAdapterOption {
  29. return log.MessageKey(key)
  30. }
  31. // Prefix configures the adapter to parse a prefix from stdlib log events. If
  32. // you provide a non-empty prefix to the stdlib logger, then your should provide
  33. // that same prefix to the adapter via this option.
  34. //
  35. // By default, the prefix isn't included in the msg key. Set joinPrefixToMsg to
  36. // true if you want to include the parsed prefix in the msg.
  37. func Prefix(prefix string, joinPrefixToMsg bool) StdlibAdapterOption {
  38. return log.Prefix(prefix, joinPrefixToMsg)
  39. }
  40. // NewStdlibAdapter returns a new StdlibAdapter wrapper around the passed
  41. // logger. It's designed to be passed to log.SetOutput.
  42. func NewStdlibAdapter(logger Logger, options ...StdlibAdapterOption) io.Writer {
  43. return log.NewStdlibAdapter(logger, options...)
  44. }