Skip to content

Commit

Permalink
lots of various fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Keloran committed Feb 8, 2024
1 parent a2fc60e commit e5d316f
Show file tree
Hide file tree
Showing 13 changed files with 401 additions and 153 deletions.
28 changes: 14 additions & 14 deletions logs/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,6 @@ const (
defaultSkipDepth = 3
)

func (b BugFixes) Unwrap(e error) error {
u, ok := e.(interface {
Unwrap() error
})
if !ok {
return nil
}

return u.Unwrap()
}

type BugFixes struct {
FormattedLog string `json:"log"`
Level string `json:"level"`
Expand All @@ -47,7 +36,7 @@ type BugFixes struct {
SkipDepthOverride int
}

func NewBugFixes(b BugFixes, err error) error {
func NewBugFixes(err error) error {
if err == nil {
return nil
}
Expand Down Expand Up @@ -123,7 +112,18 @@ func ConvertLevelFromString(s string) int {
}
}

func (b *BugFixes) skipDepth(depth int) {
func (b BugFixes) UnwrapIt(e error) error {
u, ok := e.(interface {
Unwrap() error
})
if !ok {
return nil
}

return u.Unwrap()
}

func (b BugFixes) skipDepth(depth int) {
_, file, line, _ := runtime.Caller(depth)
b.File = file
b.LineNumber = line
Expand Down Expand Up @@ -168,7 +168,7 @@ func (b BugFixes) DoReporting() {
b.sendLog()
}

func (b *BugFixes) logFormat() {
func (b BugFixes) logFormat() {
out := bytes.Buffer{}
lf := logfmt.NewEncoder(&out)

Expand Down
56 changes: 56 additions & 0 deletions logs/logging_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package logs_test

import (
"errors"
"fmt"
"github.com/bugfixes/go-bugfixes/logs"
"testing"
)

func TestUnwrap(t *testing.T) {
err := fmt.Errorf("%w error", errors.New("error"))
wrappedError := logs.BugFixes{
Err: err,
}
unwrappedError := wrappedError.UnwrapIt(err)

if unwrappedError.Error() != "error" {
t.Fatalf("Expected unwrapped error message to be 'error' instead got %v", unwrappedError.Error())
}
}

func TestNewBugFixes(t *testing.T) {
err := errors.New("error")
bugFixesError := logs.NewBugFixes(err)

if bugFixesError == nil {
t.Fatal("Expected BugFixes error to be 'error'")
}
}

func TestConvertLevelFromString(t *testing.T) {
tests := []struct {
input string
output int
}{
{"log", 1},
{"debug", 1},
{"info", 2},
{"warn", 3},
{"error", 4},
{"crash", 5},
{"panic", 5},
{"fatal", 5},
{"unknown", 9},
{"10", 9},
{"unrecognized", 9},
}

for _, test := range tests {
converted := logs.ConvertLevelFromString(test.input)

if converted != test.output {
t.Fatalf("Expected '%v' to convert to %d, got %d", test.input, test.output, converted)
}
}
}
63 changes: 6 additions & 57 deletions logs/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,57 +6,6 @@ import (
"strings"
)

//func SkipLog(level string) bool {
// type LogLevel struct {
// String string
// Int int
// }
// logLevels := []LogLevel{
// {"log", 0},
// {"debug", 1},
// {"info", 2},
// {"warn", 3},
// {"error", 4},
// {"fatal", 5},
// {"unknown", 9},
// }
//
// // default log level
// logLevel := 0
// appLogLevel := 0
//
// logLevelAppRead := os.Getenv("LOG_LEVEL")
// if logLevelAppRead != "" {
// i, err := strconv.Atoi(logLevelAppRead)
// if err != nil {
// appLogLevel = 0
// } else {
// appLogLevel = i
// }
// }
// i, err := strconv.Atoi(level)
// if err != nil {
// logLevel = 0
// } else {
// logLevel = i
// }
//
// for _, logLevel := range logLevels {
// if logLevel.String == level {
// return false
// }
// if logLevel.Int == appLogLevel {
// return false
// }
// }
//
// if logLevel >= appLogLevel {
// return false
// }
//
// return true
//}

func Local(skipDepthOverride ...int) BugFixes {
if len(skipDepthOverride) > 0 && skipDepthOverride[0] != 0 {
return BugFixes{
Expand Down Expand Up @@ -96,16 +45,16 @@ func Errorf(format string, inputs ...interface{}) error {

// </editor-fold>

// <editor-fold desc="Info">
// Info <editor-fold desc="Info">
func (b BugFixes) Info(inputs ...interface{}) string {
format := strings.Repeat("%v, ", len(inputs))
format = strings.TrimRight(format, ", ") // remove trailing comma and space
return b.Infof(format, inputs...)
}
func Info(inputs ...interface{}) {
func Info(inputs ...interface{}) string {
format := strings.Repeat("%v, ", len(inputs))
format = strings.TrimRight(format, ", ") // remove trailing comma and space
Infof(format, inputs...)
return Infof(format, inputs...)
}
func (b BugFixes) Infof(format string, inputs ...interface{}) string {
b.Level = "info"
Expand All @@ -122,7 +71,7 @@ func Infof(format string, inputs ...interface{}) string {

// </editor-fold>

// <editor-fold desc="Debug">
// Debug <editor-fold desc="Debug">
func (b BugFixes) Debug(inputs ...interface{}) string {
format := strings.Repeat("%v, ", len(inputs))
format = strings.TrimRight(format, ", ") // remove trailing comma and space
Expand All @@ -149,7 +98,7 @@ func Debugf(format string, inputs ...interface{}) string {

// </editor-fold>

// <editor-fold desc="Log">
// Log <editor-fold desc="Log">
func (b BugFixes) Log(inputs ...interface{}) string {
format := strings.Repeat("%v, ", len(inputs))
format = strings.TrimRight(format, ", ") // remove trailing comma and space
Expand All @@ -175,7 +124,7 @@ func Logf(format string, inputs ...interface{}) string {

// </editor-fold>

// <editor-fold desc="Warn">
// Warn <editor-fold desc="Warn">
func (b BugFixes) Warn(inputs ...interface{}) string {
format := strings.Repeat("%v, ", len(inputs))
format = strings.TrimRight(format, ", ") // remove trailing comma and space
Expand Down
Loading

0 comments on commit e5d316f

Please sign in to comment.