You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

57 lines
1.6 KiB

  1. package log
  2. import (
  3. "io"
  4. "sync"
  5. )
  6. const (
  7. // LogFormatPlain defines a logging format used for human-readable text-based
  8. // logging that is not structured. Typically, this format is used for development
  9. // and testing purposes.
  10. LogFormatPlain string = "plain"
  11. // LogFormatText defines a logging format used for human-readable text-based
  12. // logging that is not structured. Typically, this format is used for development
  13. // and testing purposes.
  14. LogFormatText string = "text"
  15. // LogFormatJSON defines a logging format for structured JSON-based logging
  16. // that is typically used in production environments, which can be sent to
  17. // logging facilities that support complex log parsing and querying.
  18. LogFormatJSON string = "json"
  19. // Supported loging levels
  20. LogLevelDebug = "debug"
  21. LogLevelInfo = "info"
  22. LogLevelWarn = "warn"
  23. LogLevelError = "error"
  24. )
  25. // Logger defines a generic logging interface compatible with Tendermint.
  26. type Logger interface {
  27. Debug(msg string, keyVals ...interface{})
  28. Info(msg string, keyVals ...interface{})
  29. Error(msg string, keyVals ...interface{})
  30. With(keyVals ...interface{}) Logger
  31. }
  32. // syncWriter wraps an io.Writer that can be used in a Logger that is safe for
  33. // concurrent use by multiple goroutines.
  34. type syncWriter struct {
  35. sync.Mutex
  36. io.Writer
  37. }
  38. func newSyncWriter(w io.Writer) io.Writer {
  39. return &syncWriter{Writer: w}
  40. }
  41. // Write writes p to the underlying io.Writer. If another write is already in
  42. // progress, the calling goroutine blocks until the syncWriter is available.
  43. func (w *syncWriter) Write(p []byte) (int, error) {
  44. w.Lock()
  45. defer w.Unlock()
  46. return w.Writer.Write(p)
  47. }