github.com/BlockABC/godash@v0.0.0-20191112120524-f4aa3a32c566/database/log.go (about)

     1  // Copyright (c) 2013-2016 The btcsuite developers
     2  // Copyright (c) 2016 The Dash developers
     3  // Use of this source code is governed by an ISC
     4  // license that can be found in the LICENSE file.
     5  
     6  package database
     7  
     8  import (
     9  	"errors"
    10  	"io"
    11  
    12  	"github.com/btcsuite/btclog"
    13  )
    14  
    15  // log is a logger that is initialized with no output filters.  This
    16  // means the package will not perform any logging by default until the caller
    17  // requests it.
    18  var log btclog.Logger
    19  
    20  // The default amount of logging is none.
    21  func init() {
    22  	DisableLog()
    23  }
    24  
    25  // DisableLog disables all library log output.  Logging output is disabled
    26  // by default until either UseLogger or SetLogWriter are called.
    27  func DisableLog() {
    28  	log = btclog.Disabled
    29  }
    30  
    31  // UseLogger uses a specified Logger to output package logging info.
    32  // This should be used in preference to SetLogWriter if the caller is also
    33  // using btclog.
    34  func UseLogger(logger btclog.Logger) {
    35  	log = logger
    36  
    37  	// Update the logger for the registered drivers.
    38  	for _, drv := range drivers {
    39  		if drv.UseLogger != nil {
    40  			drv.UseLogger(logger)
    41  		}
    42  	}
    43  }
    44  
    45  // SetLogWriter uses a specified io.Writer to output package logging info.
    46  // This allows a caller to direct package logging output without needing a
    47  // dependency on seelog.  If the caller is also using btclog, UseLogger should
    48  // be used instead.
    49  func SetLogWriter(w io.Writer, level string) error {
    50  	if w == nil {
    51  		return errors.New("nil writer")
    52  	}
    53  
    54  	lvl, ok := btclog.LogLevelFromString(level)
    55  	if !ok {
    56  		return errors.New("invalid log level")
    57  	}
    58  
    59  	l, err := btclog.NewLoggerFromWriter(w, lvl)
    60  	if err != nil {
    61  		return err
    62  	}
    63  
    64  	UseLogger(l)
    65  	return nil
    66  }