gitee.com/zhongguo168a/gocodes@v0.0.0-20230609140523-e1828349603f/myx/logx/logrus/logurs.go (about)

     1  package logrus
     2  
     3  import (
     4  	"fmt"
     5  	"gitee.com/zhongguo168a/gocodes/myx/logx/iface"
     6  	rotatelogs "github.com/lestrrat-go/file-rotatelogs"
     7  	log "github.com/sirupsen/logrus"
     8  	"os"
     9  	"strings"
    10  	"time"
    11  )
    12  
    13  func init() {
    14  
    15  	//log.SetFormatter(new(Formatter))
    16  	//log.SetReportCaller(false)
    17  }
    18  
    19  func NewLogger() (obj *Logger) {
    20  	obj = &Logger{}
    21  	l := log.New()
    22  	obj.entity = l
    23  	l.SetLevel(log.DebugLevel)
    24  
    25  	l.SetFormatter(&log.JSONFormatter{
    26  		TimestampFormat: "2006/01/02 15:04:05",
    27  	})
    28  	return
    29  }
    30  
    31  type Logger struct {
    32  	Name   string
    33  	entity log.FieldLogger
    34  }
    35  
    36  func (l *Logger) GetEntity() interface{} {
    37  	return l.entity
    38  }
    39  
    40  func (l *Logger) UseFileRotation(path string) {
    41  
    42  	/* 日志轮转相关函数
    43  	`WithLinkName` 为最新的日志建立软连接
    44  	`WithRotationTime` 设置日志分割的时间,隔多久分割一次
    45  	WithMaxAge 和 WithRotationCount二者只能设置一个
    46  	  `WithMaxAge` 设置文件清理前的最长保存时间
    47  	  `WithRotationCount` 设置文件清理前最多保存的个数
    48  	*/
    49  	// 下面配置日志每隔 1 分钟轮转一个新文件,保留最近 3 分钟的日志文件,多余的自动清理掉。
    50  	//writer, _ := rotatelogs.New(
    51  	//	path+".%Y%m%d%H%M",
    52  	//	rotatelogs.WithLinkName(path),
    53  	//	rotatelogs.WithMaxAge(time.Duration(180)*time.Second),
    54  	//	rotatelogs.WithRotationTime(time.Duration(60)*time.Second),
    55  	//)
    56  
    57  	writer, _ := rotatelogs.New(
    58  		path+"_%Y%m%d%H%M.log",
    59  		rotatelogs.WithLinkName(path),
    60  		rotatelogs.WithRotationCount(7),
    61  		rotatelogs.WithRotationTime(time.Duration(24)*time.Hour),
    62  	)
    63  
    64  	logger := l.entity.(*log.Logger)
    65  	logger.SetOutput(writer)
    66  	//log.SetFormatter(&log.JSONFormatter{})
    67  }
    68  
    69  func (l *Logger) Ok(a ...interface{}) {
    70  	l.entity.Print("[ok   ] ")
    71  	l.entity.Println(a...)
    72  }
    73  
    74  func (l *Logger) Fail(a ...interface{}) {
    75  	l.entity.Print("[fail ] ")
    76  	l.entity.Println(a...)
    77  }
    78  
    79  func (l *Logger) Msg(a ...interface{}) {
    80  	l.entity.Println(a...)
    81  }
    82  
    83  func (l *Logger) Debug(a ...interface{}) {
    84  	l.entity.Debugln(a...)
    85  }
    86  
    87  func (l *Logger) Warn(a ...interface{}) {
    88  	l.entity.Warnln(a...)
    89  }
    90  func (l *Logger) Info(a ...interface{}) {
    91  	l.entity.Println(a...)
    92  }
    93  
    94  func (l *Logger) Error(a ...interface{}) {
    95  	l.entity.Errorln(a...)
    96  }
    97  
    98  func (l *Logger) Fatal(a ...interface{}) {
    99  	l.entity.Fatalln(a...)
   100  	os.Exit(-1)
   101  }
   102  
   103  func (l *Logger) With(m map[string]interface{}) iface.ILogger {
   104  	n := &Logger{}
   105  	n.entity = l.entity.WithFields(m)
   106  	return n
   107  }
   108  
   109  type Formatter struct{}
   110  
   111  func (s *Formatter) Format(entry *log.Entry) ([]byte, error) {
   112  	timestamp := time.Now().Local().Format("2006/01/02 15:04:05")
   113  	msg := fmt.Sprintf("%s [%s] %s\n", timestamp, strings.ToUpper(entry.Level.String()), entry.Message)
   114  	return []byte(msg), nil
   115  }