github.com/mundipagg/boleto-api@v0.0.0-20230620145841-3f9ec742599f/log/tracer.go (about) 1 package log 2 3 import ( 4 "fmt" 5 6 "regexp" 7 "strconv" 8 "time" 9 10 "github.com/mundipagg/boleto-api/config" 11 seq "github.com/mundipagg/tracer-seq-writer" 12 splunk "github.com/mundipagg/tracer-splunk-writer" 13 14 "github.com/mralves/tracer" 15 16 bsq "github.com/mundipagg/tracer-seq-writer/buffer" 17 bsp "github.com/mundipagg/tracer-splunk-writer/buffer" 18 ) 19 20 type Safe struct { 21 tracer.Writer 22 } 23 24 func (s *Safe) Write(entry tracer.Entry) { 25 defer func() { 26 err := recover() 27 if err != nil { 28 fmt.Printf("%v", err) 29 } 30 }() 31 s.Writer.Write(entry) 32 } 33 34 func configureTracer() { 35 var writers []tracer.Writer 36 tracer.DefaultContext.OverwriteChildren() 37 38 WaitTimeLog := toInt(config.Get().WaitSecondsRetentationLog, 1) 39 40 if config.Get().SeqEnabled == true { 41 writers = append(writers, &Safe{seq.New(seq.Config{ 42 Timeout: 3 * time.Second, 43 MinimumLevel: tracer.Debug, 44 DefaultProperties: LogEntry{ 45 "Application": config.Get().ApplicationName, 46 "Environment": config.Get().Environment, 47 "Domain": config.Get().SEQDomain, 48 "MachineName": config.Get().MachineName, 49 "BuildVersion": config.Get().BuildVersion, 50 }, 51 Application: config.Get().ApplicationName, 52 Key: config.Get().SEQAPIKey, 53 Address: config.Get().SEQUrl, 54 Buffer: bsq.Config{ 55 OnWait: 2, 56 BackOff: time.Duration(WaitTimeLog) * time.Second, 57 Expiration: 5 * time.Second, 58 }, 59 })}) 60 } 61 62 if config.Get().SplunkEnabled == true { 63 writers = append(writers, &Safe{splunk.New(splunk.Config{ 64 Timeout: 3 * time.Second, 65 MinimumLevel: tracer.Debug, 66 ConfigLineLog: LogEntry{ 67 "host": config.Get().MachineName, 68 "source": "BoletoOnline", 69 "sourcetype": config.Get().SplunkSourceType, 70 "index": config.Get().SplunkIndex, 71 }, 72 DefaultPropertiesSplunk: LogEntry{ 73 "ProcessName": "BoletoApi", 74 "ProductCompany": "Mundipagg", 75 "ProductName": "BoletoOnline", 76 "ProductVersion": "1.0", 77 }, 78 DefaultPropertiesApp: LogEntry{ 79 "Application": config.Get().ApplicationName, 80 "Environment": config.Get().Environment, 81 "Domain": config.Get().SEQDomain, 82 "MachineName": config.Get().MachineName, 83 "BuildVersion": config.Get().BuildVersion, 84 }, 85 Application: config.Get().ApplicationName, 86 Key: config.Get().SplunkKey, 87 Address: config.Get().SplunkAddress, 88 Buffer: bsp.Config{ 89 OnWait: 2, 90 BackOff: time.Duration(WaitTimeLog) * time.Second, 91 Expiration: 5 * time.Second, 92 }, 93 })}) 94 } 95 96 for _, writer := range writers { 97 tracer.RegisterWriter(writer) 98 } 99 } 100 101 func toInt(str string, defaultValue ...int) int { 102 if isBlank(str) { 103 return 0 104 } 105 i, err := strconv.Atoi(str) 106 if err != nil { 107 if len(defaultValue) > 0 { 108 return defaultValue[0] 109 } 110 panic(err) 111 } 112 return i 113 } 114 115 var emptyOrWhitespacePattern = regexp.MustCompile(`^\s*$`) 116 117 // Function to check if a string is empty or contain only whitespaces. 118 func isBlank(str string) bool { 119 return emptyOrWhitespacePattern.MatchString(str) 120 }