github.com/iikira/iikira-go-utils@v0.0.0-20230610031953-f2cb11cde33a/utils/log_colorable_prefix.go (about)

     1  package utils
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/fatih/color"
     6  	"github.com/iikira/iikira-go-utils/utils/csttime"
     7  	"log"
     8  )
     9  
    10  var (
    11  	// ErrorColor 设置输出错误的颜色
    12  	ErrorColor = color.New(color.FgRed).SprintFunc()
    13  )
    14  
    15  // 自定义log writer
    16  type logWriter struct{}
    17  
    18  func (logWriter) Write(bytes []byte) (int, error) {
    19  	return fmt.Fprint(color.Output, "["+csttime.BeijingTimeOption("Refer")+"] "+string(bytes))
    20  }
    21  
    22  // SetLogPrefix 设置日志输出的时间前缀
    23  func SetLogPrefix() {
    24  	log.SetFlags(0)
    25  	log.SetOutput(new(logWriter))
    26  }