github.com/neatio-net/neatio@v1.7.3-0.20231114194659-f4d7a2226baa/chain/log/logger_chain.go (about)

     1  package log
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  	"sync"
     7  
     8  	"github.com/mattn/go-colorable"
     9  )
    10  
    11  var loggerMap sync.Map
    12  
    13  func newChainLogger(chainID string) Logger {
    14  
    15  	chainLogger := &logger{[]interface{}{}, new(swapHandler)}
    16  
    17  	loggerMap.Store(chainID, chainLogger)
    18  
    19  	return chainLogger
    20  }
    21  
    22  func NewLogger(chainID, logDir string, logLevel int, fileLine bool, vmodule, backtrace string) Logger {
    23  
    24  	PrintOrigins(fileLine)
    25  
    26  	output := colorable.NewColorableStdout()
    27  	ostream := StreamHandler(output, TerminalFormat(true))
    28  	glogger := NewGlogHandler(ostream)
    29  
    30  	if logDir != "" {
    31  
    32  		preimages_logDir := filepath.Join(logDir, "preimages")
    33  		if err := os.MkdirAll(preimages_logDir, 0700); err != nil {
    34  			panic(err)
    35  		}
    36  		preimageFileHandler := Must.FileHandler(filepath.Join(preimages_logDir, "preimages.log"), TerminalFormat(false))
    37  		preimageFilter := MatchFilterHandler("module", "preimages", preimageFileHandler)
    38  
    39  		rfh, err := RotatingFileHandler(
    40  			logDir,
    41  			10*1024*1024,
    42  			TerminalFormat(false),
    43  		)
    44  		if err != nil {
    45  			panic(err)
    46  		}
    47  		glogger.SetHandler(MultiHandler(ostream, rfh, preimageFilter))
    48  	}
    49  	glogger.Verbosity(Lvl(logLevel))
    50  	glogger.Vmodule(vmodule)
    51  	glogger.BacktraceAt(backtrace)
    52  
    53  	var logger Logger
    54  	if chainID == "" {
    55  		logger = Root()
    56  		logger.SetHandler(glogger)
    57  	} else {
    58  		logger = newChainLogger(chainID)
    59  		logger.SetHandler(glogger)
    60  	}
    61  
    62  	return logger
    63  }
    64  
    65  func GetLogger(chainID string) Logger {
    66  	logger, find := loggerMap.Load(chainID)
    67  	if find {
    68  		return logger.(Logger)
    69  	} else {
    70  		return nil
    71  	}
    72  }
    73  
    74  func RangeLogger(f func(key, value interface{}) bool) {
    75  	loggerMap.Range(f)
    76  }