bitbucket.org/number571/tendermint@v0.8.14/libs/log/logger.go (about)

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