github.com/keltia/go-ipfs@v0.3.8-0.20150909044612-210793031c63/thirdparty/eventlog/option.go (about)

     1  package eventlog
     2  
     3  import (
     4  	"io"
     5  	"os"
     6  
     7  	"github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/Sirupsen/logrus"
     8  )
     9  
    10  // init sets up sane defaults
    11  func init() {
    12  	Configure(TextFormatter)
    13  	Configure(Output(os.Stderr))
    14  	// has the effect of disabling logging since we log event entries at Info
    15  	// level by convention
    16  	Configure(LevelError)
    17  }
    18  
    19  // Global writer group for logs to output to
    20  var WriterGroup = new(MirrorWriter)
    21  
    22  type Option func()
    23  
    24  // Configure applies the provided options sequentially from left to right
    25  func Configure(options ...Option) {
    26  	for _, f := range options {
    27  		f()
    28  	}
    29  }
    30  
    31  // LdJSONFormatter Option formats the event log as line-delimited JSON
    32  var LdJSONFormatter = func() {
    33  	logrus.SetFormatter(&PoliteJSONFormatter{})
    34  }
    35  
    36  // TextFormatter Option formats the event log as human-readable plain-text
    37  var TextFormatter = func() {
    38  	logrus.SetFormatter(&logrus.TextFormatter{})
    39  }
    40  
    41  func Output(w io.Writer) Option {
    42  	return func() {
    43  		logrus.SetOutput(w)
    44  		// TODO return previous Output option
    45  	}
    46  }
    47  
    48  // LevelDebug Option sets the log level to debug
    49  var LevelDebug = func() {
    50  	logrus.SetLevel(logrus.DebugLevel)
    51  }
    52  
    53  // LevelDebug Option sets the log level to error
    54  var LevelError = func() {
    55  	logrus.SetLevel(logrus.ErrorLevel)
    56  }
    57  
    58  // LevelDebug Option sets the log level to info
    59  var LevelInfo = func() {
    60  	logrus.SetLevel(logrus.InfoLevel)
    61  }