github.com/LanceLRQ/deer-common@v0.0.9-0.20210319081233-e8222ac018a8/logger/logging.go (about)

     1  package logger
     2  
     3  import (
     4  	"fmt"
     5  	"time"
     6  )
     7  
     8  const (
     9  	LogLevelDebug = iota + 1
    10  	LogLevelInfo
    11  	LogLevelWarn
    12  	LogLevelError
    13  )
    14  
    15  var LogLevelMapping = []string{
    16  	"",
    17  	"DEBUG",
    18  	"INFO",
    19  	"WARN",
    20  	"ERROR",
    21  }
    22  
    23  var LogLevelStrMapping = map[string]int{
    24  	"debug": LogLevelDebug,
    25  	"info":  LogLevelInfo,
    26  	"warn":  LogLevelWarn,
    27  	"error": LogLevelError,
    28  }
    29  
    30  // 用于记录评测日志的工具
    31  type JudgeLogger struct {
    32  	// 日志数据
    33  	logs []JudgeLogItem
    34  	// T-0时间
    35  	startTime time.Time
    36  	// 是否打印日志
    37  	swPrint bool
    38  	// 设置日志打印的等级,默认是全部
    39  	printLevel int
    40  }
    41  
    42  // 创建评测日志
    43  func NewJudgeLogger() *JudgeLogger {
    44  	logger := JudgeLogger{}
    45  	logger.logs = make([]JudgeLogItem, 0, 5)
    46  	return &logger
    47  }
    48  
    49  // 输出Log的基础函数
    50  func (logger *JudgeLogger) Log(level int, msg string) {
    51  	nowTime := time.Now()
    52  	if len(logger.logs) <= 0 {
    53  		// 如果还没有写入过日志,则以这个时间作为起点
    54  		logger.startTime = nowTime
    55  	}
    56  	timeDistance := nowTime.Sub(logger.startTime)
    57  	log := JudgeLogItem{
    58  		Message:   msg,
    59  		Level:     level,
    60  		Timestamp: float64(timeDistance.Nanoseconds()) / 1000000000.0,
    61  	}
    62  	logger.logs = append(logger.logs, log)
    63  	if logger.swPrint && level >= logger.printLevel {
    64  		fmt.Printf(
    65  			"[%s] %s %s\n",
    66  			logger.getDurationTimeStr(timeDistance),
    67  			LogLevelMapping[level],
    68  			msg,
    69  		)
    70  	}
    71  }
    72  
    73  func (logger *JudgeLogger) Logf(level int, msg string, args ...interface{}) {
    74  	logger.Log(level, fmt.Sprintf(msg, args...))
    75  }
    76  
    77  func (logger *JudgeLogger) Debug(msg string) {
    78  	logger.Log(LogLevelDebug, msg)
    79  }
    80  
    81  func (logger *JudgeLogger) Info(msg string) {
    82  	logger.Log(LogLevelInfo, msg)
    83  }
    84  
    85  func (logger *JudgeLogger) Warn(msg string) {
    86  	logger.Log(LogLevelWarn, msg)
    87  }
    88  
    89  func (logger *JudgeLogger) Error(msg string) {
    90  	logger.Log(LogLevelError, msg)
    91  }
    92  
    93  func (logger *JudgeLogger) Debugf(msg string, args ...interface{}) {
    94  	logger.Logf(LogLevelDebug, msg, args...)
    95  }
    96  
    97  func (logger *JudgeLogger) Infof(msg string, args ...interface{}) {
    98  	logger.Logf(LogLevelInfo, msg, args...)
    99  }
   100  
   101  func (logger *JudgeLogger) Warnf(msg string, args ...interface{}) {
   102  	logger.Logf(LogLevelWarn, msg, args...)
   103  }
   104  
   105  func (logger *JudgeLogger) Errorf(msg string, args ...interface{}) {
   106  	logger.Logf(LogLevelError, msg, args...)
   107  }
   108  
   109  func (logger *JudgeLogger) GetLogs() []JudgeLogItem {
   110  	return logger.logs
   111  }
   112  
   113  func (logger *JudgeLogger) getDurationTimeStr(d time.Duration) string {
   114  	m := d / time.Minute
   115  	s := d / time.Second % 60
   116  	ms := d / time.Millisecond % 1000
   117  	return fmt.Sprintf("%02d:%02d.%03d", m, s, ms)
   118  }
   119  
   120  func (logger *JudgeLogger) SetStdoutPrint(swPrint bool) {
   121  	logger.swPrint = swPrint
   122  }
   123  
   124  // 设置日志等级,会打印它和比它大级别的日志。比如设置为 WARN,则WARN和ERROR会被输出。
   125  func (logger *JudgeLogger) SetLogLevel(level int) {
   126  	logger.printLevel = level
   127  }