github.com/turingchain2020/turingchain@v1.1.21/common/log/log.go (about)

     1  // Copyright Turing Corp. 2018 All Rights Reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // Package log 日志相关接口以及函数
     6  package log
     7  
     8  import (
     9  	"os"
    10  
    11  	"github.com/turingchain2020/turingchain/common/log/log15"
    12  	"github.com/turingchain2020/turingchain/types"
    13  	lumberjack "gopkg.in/natefinch/lumberjack.v2"
    14  )
    15  
    16  var (
    17  	// 保存日志处理器的引用,方便后续调整日志信息,而不重新初始化
    18  	fileHandler    *log15.Handler
    19  	consoleHandler *log15.Handler
    20  )
    21  
    22  func init() {
    23  	//resetWithLogLevel("error")
    24  }
    25  
    26  //SetLogLevel 设置控制台日志输出级别
    27  func SetLogLevel(logLevel string) {
    28  	handler := getConsoleLogHandler(logLevel)
    29  	(*handler).SetMaxLevel(int(getLevel(logLevel)))
    30  	log15.Root().SetHandler(*handler)
    31  }
    32  
    33  //SetFileLog 设置文件日志和控制台日志信息
    34  func SetFileLog(log *types.Log) {
    35  	if log == nil {
    36  		log = &types.Log{LogFile: "logs/turingchain.log"}
    37  	}
    38  	if log.LogFile == "" {
    39  		SetLogLevel(log.LogConsoleLevel)
    40  	} else {
    41  		resetLog(log)
    42  	}
    43  }
    44  
    45  // 清空原来所有的日志Handler,根据配置文件信息重置文件和控制台日志
    46  func resetLog(log *types.Log) {
    47  	fillDefaultValue(log)
    48  	log15.Root().SetHandler(log15.MultiHandler(*getConsoleLogHandler(log.LogConsoleLevel), *getFileLogHandler(log)))
    49  }
    50  
    51  // 保证默认性况下为error级别,防止打印太多日志
    52  func fillDefaultValue(log *types.Log) {
    53  	if log.Loglevel == "" {
    54  		log.Loglevel = log15.LvlError.String()
    55  	}
    56  	if log.LogConsoleLevel == "" {
    57  		log.LogConsoleLevel = log15.LvlError.String()
    58  	}
    59  }
    60  
    61  func isWindows() bool {
    62  	return os.PathSeparator == '\\' && os.PathListSeparator == ';'
    63  }
    64  
    65  func getConsoleLogHandler(logLevel string) *log15.Handler {
    66  	if consoleHandler != nil {
    67  		return consoleHandler
    68  	}
    69  	format := log15.TerminalFormat()
    70  	if isWindows() {
    71  		format = log15.LogfmtFormat()
    72  	}
    73  	stdouth := log15.LvlFilterHandler(
    74  		getLevel(logLevel),
    75  		log15.StreamHandler(os.Stdout, format),
    76  	)
    77  
    78  	consoleHandler = &stdouth
    79  
    80  	return &stdouth
    81  }
    82  
    83  func getFileLogHandler(log *types.Log) *log15.Handler {
    84  	if fileHandler != nil {
    85  		return fileHandler
    86  	}
    87  
    88  	rotateLogger := &lumberjack.Logger{
    89  		Filename:   log.LogFile,
    90  		MaxSize:    int(log.MaxFileSize),
    91  		MaxBackups: int(log.MaxBackups),
    92  		MaxAge:     int(log.MaxAge),
    93  		LocalTime:  log.LocalTime,
    94  		Compress:   log.Compress,
    95  	}
    96  
    97  	fileh := log15.LvlFilterHandler(
    98  		getLevel(log.Loglevel),
    99  		log15.StreamHandler(rotateLogger, log15.LogfmtFormat()),
   100  	)
   101  
   102  	// 增加打印调用源文件、方法和代码行的判断
   103  	if log.CallerFile {
   104  		fileh = log15.CallerFileHandler(fileh)
   105  	}
   106  	if log.CallerFunction {
   107  		fileh = log15.CallerFuncHandler(fileh)
   108  	}
   109  
   110  	fileHandler = &fileh
   111  
   112  	return &fileh
   113  }
   114  
   115  func getLevel(lvlString string) log15.Lvl {
   116  	lvl, err := log15.LvlFromString(lvlString)
   117  	if err != nil {
   118  		// 日志级别配置不正确时默认为error级别
   119  		return log15.LvlError
   120  	}
   121  	return lvl
   122  }
   123  
   124  //New new
   125  func New(ctx ...interface{}) log15.Logger {
   126  	return NewMain(ctx...)
   127  }
   128  
   129  //NewMain new
   130  func NewMain(ctx ...interface{}) log15.Logger {
   131  	return log15.Root().New(ctx...)
   132  }