Best Ginkgo code snippet using formatter.newColorable
log.go
Source:log.go
1package log2import (3 "errors"4 "fmt"5 "os"6 "path/filepath"7 "time"8 "github.com/mattn/go-colorable"9 log "github.com/sirupsen/logrus"10)11type logFileWriter struct {12 infoWriter *os.File13 debugWriter *os.File14 errorWriter *os.File15 warnWriter *os.File16 rootPath string17 fileDate string //æ¥æ18 entryLevel string19}20const (21 infoLevel = "info"22 debugLevel = "debug"23 errorLevel = "error"24 warnLevel = "warn"25)26var (27 fileWriter = &logFileWriter{rootPath: "./scopeLog", entryLevel: infoLevel}28 stdout = colorable.NewColorableStdout()29)30type LoggerFormat struct {31 logWriter *logFileWriter32}33func (s *LoggerFormat) Format(entry *log.Entry) ([]byte, error) {34 timestamp := time.Now().Local().Format("2006-01-02 15:04:05.000")35 var file string36 var length int37 if entry.Caller != nil {38 file = filepath.Base(entry.Caller.File)39 length = entry.Caller.Line40 }41 //d为1 表示é«äº®42 //red: (prefix=31,b=40,d=1)43 //green (f=32,b=40,d=1)44 //yellow (f=33,b=40,d=1)45 //cyan (f=34,b=40,d=1)46 var colorFomat string47 switch entry.Level {48 case log.DebugLevel:49 //cyan50 colorFomat = fmt.Sprintf("%c[%d;%d;%dm", 0x1B, 1, 1, 34)51 s.logWriter.entryLevel = debugLevel52 case log.InfoLevel:53 //green54 colorFomat = fmt.Sprintf("%c[%d;%d;%dm", 0x1B, 1, 1, 32)55 s.logWriter.entryLevel = infoLevel56 case log.ErrorLevel:57 //red [æ¾ç¤ºæ¹å¼ï¼1ï¼é«äº®ï¼ãèæ¯é¢è²(é»è²:40)ãé¢è² ]58 colorFomat = fmt.Sprintf("%c[%d;%d;%dm", 0x1B, 1, 1, 31)59 s.logWriter.entryLevel = errorLevel60 case log.WarnLevel:61 //yellow62 colorFomat = fmt.Sprintf("%c[%d;%d;%dm", 0x1B, 1, 1, 33)63 s.logWriter.entryLevel = warnLevel64 }65 msg := fmt.Sprintf("%s[%s:] [%s:%d] %s \u001B[0m msg:%s \n", colorFomat, entry.Level.String(), file, length, timestamp, entry.Message)66 return []byte(msg), nil67}68func (p *logFileWriter) Write(data []byte) (n int, err error) {69 if p == nil {70 return 0, errors.New("logFileWriter is nil")71 }72 //å¤ææ¯å¦éè¦åæ¢æ¥æ73 fileDate := time.Now().Format("20060102")74 if p.fileDate != fileDate {75 switch p.entryLevel {76 case infoLevel:77 err = os.MkdirAll(fmt.Sprintf("%s/%s", p.rootPath, infoLevel), os.ModePerm)78 if err != nil {79 fmt.Errorf("%s\n", err.Error())80 return 0, err81 }82 infofileName := fmt.Sprintf("%s/%s/%s-%s.logger", p.rootPath, infoLevel, infoLevel, fileDate)83 p.infoWriter, err = os.OpenFile(infofileName, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0600)84 checkError(err)85 writer := colorable.NewColorable(fileWriter.infoWriter)86 n, err = writer.Write(data)87 case errorLevel:88 err = os.MkdirAll(fmt.Sprintf("%s/%s", p.rootPath, errorLevel), os.ModePerm)89 if err != nil {90 fmt.Errorf("%s\n", err.Error())91 return 0, err92 }93 errorfileName := fmt.Sprintf("%s/%s/%s-%s.logger", p.rootPath, errorLevel, errorLevel, fileDate)94 p.errorWriter, err = os.OpenFile(errorfileName, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0600)95 checkError(err)96 writer := colorable.NewColorable(fileWriter.errorWriter)97 n, err = writer.Write(data)98 case debugLevel:99 err = os.MkdirAll(fmt.Sprintf("%s/%s", p.rootPath, debugLevel), os.ModePerm)100 if err != nil {101 fmt.Errorf("%s\n", err.Error())102 return 0, err103 }104 debugfileName := fmt.Sprintf("%s/%s/%s-%s.logger", p.rootPath, debugLevel, debugLevel, fileDate)105 p.debugWriter, err = os.OpenFile(debugfileName, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0600)106 checkError(err)107 writer := colorable.NewColorable(fileWriter.debugWriter)108 n, err = writer.Write(data)109 case warnLevel:110 err = os.MkdirAll(fmt.Sprintf("%s/%s", p.rootPath, warnLevel), os.ModePerm)111 if err != nil {112 fmt.Errorf("%s\n", err.Error())113 return 0, err114 }115 warnfileName := fmt.Sprintf("%s/%s/%s-%s.logger", p.rootPath, warnLevel, warnLevel, fileDate)116 p.warnWriter, err = os.OpenFile(warnfileName, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0600)117 checkError(err)118 writer := colorable.NewColorable(fileWriter.warnWriter)119 n, err = writer.Write(data)120 default:121 fmt.Errorf("unexpected logger level")122 }123 }124 return125}126func checkError(err error) {127 if err != nil {128 fmt.Errorf("%s\n", err)129 return130 }131}132//åå§åæ¥å¿133func Init() {134 rootPath := "./scopeLog"135 log.SetLevel(log.DebugLevel)136 //å建ç®å½137 err := os.MkdirAll(fmt.Sprintf("%s/%s", rootPath, infoLevel), os.ModePerm)138 checkError(err)139 err = os.MkdirAll(fmt.Sprintf("%s/%s", rootPath, errorLevel), os.ModePerm)140 checkError(err)141 err = os.MkdirAll(fmt.Sprintf("%s/%s", rootPath, debugLevel), os.ModePerm)142 checkError(err)143 err = os.MkdirAll(fmt.Sprintf("%s/%s", rootPath, warnLevel), os.ModePerm)144 checkError(err)145 log.SetFormatter(&LoggerFormat{146 logWriter: fileWriter,147 })148 log.SetReportCaller(true)149 log.SetOutput(fileWriter)150}...
formatter.go
Source:formatter.go
...5 "regexp"6 "strings"7)8// ColorableStdOut and ColorableStdErr enable color output support on Windows9var ColorableStdOut = newColorable(os.Stdout)10var ColorableStdErr = newColorable(os.Stderr)11const COLS = 8012type ColorMode uint813const (14 ColorModeNone ColorMode = iota15 ColorModeTerminal16 ColorModePassthrough17)18var SingletonFormatter = New(ColorModeTerminal)19func F(format string, args ...interface{}) string {20 return SingletonFormatter.F(format, args...)21}22func Fi(indentation uint, format string, args ...interface{}) string {23 return SingletonFormatter.Fi(indentation, format, args...)24}...
newColorable
Using AI Code Generation
1import (2func main() {3 formatter := color.New(color.FgHiBlue)4 formatter.Printf("This is a %s5 log.SetOutput(colorable.NewColorable(formatter))6 log.Println("This is a formatted log entry")7}
newColorable
Using AI Code Generation
1import (2func main() {3 f := color.New(color.FgRed, color.Bold, color.Underline)4 fmt.Println(f.Sprintf("This is a %s", "formatted string"))5}6import (7func main() {8 f := color.New(color.FgRed, color.Bold, color.Underline)9 fmt.Println(f.Sprintf("This is a %s", "formatted string"))10}11import (12func main() {13 f := color.New(color.FgRed, color.Bold, color.Underline)14 fmt.Println(f.Sprintf("This is a %s", "formatted string"))15}16import (17func main() {18 f := color.New(color.FgRed, color.Bold, color.Underline)19 fmt.Println(f.Sprintf("This is a %s", "formatted string"))20}21import (22func main() {23 f := color.New(color.FgRed, color.Bold
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!