Skip to content

Commit

Permalink
Fix logger
Browse files Browse the repository at this point in the history
  • Loading branch information
Oluwole Fadeyi committed Jun 17, 2023
1 parent 31c58a9 commit ac29c7d
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 19 deletions.
18 changes: 9 additions & 9 deletions internal/logging/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Package logging uses the logr.Logger interface to integrate with different loggi


<a name="ContextWithLogger"></a>
## func [ContextWithLogger](<https://github.com/tfadeyi/sloth-simple-comments/blob/main/internal/logging/logging.go#L21>)
## func [ContextWithLogger](<https://github.com/tfadeyi/sloth-simple-comments/blob/main/internal/logging/logging.go#L25>)

```go
func ContextWithLogger(ctx context.Context, l Logger) context.Context
Expand All @@ -32,7 +32,7 @@ func ContextWithLogger(ctx context.Context, l Logger) context.Context
ContextWithLogger wraps the logr NewContext function

<a name="IsValidLevel"></a>
## func [IsValidLevel](<https://github.com/tfadeyi/sloth-simple-comments/blob/main/internal/logging/logging.go#L83>)
## func [IsValidLevel](<https://github.com/tfadeyi/sloth-simple-comments/blob/main/internal/logging/logging.go#L87>)

```go
func IsValidLevel(lvl string) bool
Expand All @@ -53,7 +53,7 @@ type Logger struct {
```

<a name="LoggerFromContext"></a>
### func [LoggerFromContext](<https://github.com/tfadeyi/sloth-simple-comments/blob/main/internal/logging/logging.go#L26>)
### func [LoggerFromContext](<https://github.com/tfadeyi/sloth-simple-comments/blob/main/internal/logging/logging.go#L30>)

```go
func LoggerFromContext(ctx context.Context) Logger
Expand All @@ -62,7 +62,7 @@ func LoggerFromContext(ctx context.Context) Logger
LoggerFromContext wraps the LoggerFromContext or creates a Zap production logger

<a name="NewStandardLogger"></a>
### func [NewStandardLogger](<https://github.com/tfadeyi/sloth-simple-comments/blob/main/internal/logging/logging.go#L35>)
### func [NewStandardLogger](<https://github.com/tfadeyi/sloth-simple-comments/blob/main/internal/logging/logging.go#L39>)

```go
func NewStandardLogger() Logger
Expand All @@ -71,7 +71,7 @@ func NewStandardLogger() Logger
NewStandardLogger wraps the creation of a Golang standard logger

<a name="Logger.Debug"></a>
### func \(\*Logger\) [Debug](<https://github.com/tfadeyi/sloth-simple-comments/blob/main/internal/logging/logging.go#L64>)
### func \(\*Logger\) [Debug](<https://github.com/tfadeyi/sloth-simple-comments/blob/main/internal/logging/logging.go#L68>)

```go
func (l *Logger) Debug(msg string, keysAndValues ...interface{})
Expand All @@ -80,7 +80,7 @@ func (l *Logger) Debug(msg string, keysAndValues ...interface{})
Debug logs the message using the info debug level \(1\)

<a name="Logger.Info"></a>
### func \(\*Logger\) [Info](<https://github.com/tfadeyi/sloth-simple-comments/blob/main/internal/logging/logging.go#L54>)
### func \(\*Logger\) [Info](<https://github.com/tfadeyi/sloth-simple-comments/blob/main/internal/logging/logging.go#L58>)

```go
func (l *Logger) Info(msg string, keysAndValues ...interface{})
Expand All @@ -89,7 +89,7 @@ func (l *Logger) Info(msg string, keysAndValues ...interface{})
Info logs the message using the info log level \(2\)

<a name="Logger.SetLevel"></a>
### func \(\*Logger\) [SetLevel](<https://github.com/tfadeyi/sloth-simple-comments/blob/main/internal/logging/logging.go#L46>)
### func \(\*Logger\) [SetLevel](<https://github.com/tfadeyi/sloth-simple-comments/blob/main/internal/logging/logging.go#L50>)

```go
func (l *Logger) SetLevel(lvl string) Logger
Expand All @@ -98,7 +98,7 @@ func (l *Logger) SetLevel(lvl string) Logger
SetLevel sets the global level against which all info logs will be compared. If this is greater than or equal to the "V" of the logger, the message will be logged. A higher value here means more logs will be written

<a name="Logger.Warn"></a>
### func \(\*Logger\) [Warn](<https://github.com/tfadeyi/sloth-simple-comments/blob/main/internal/logging/logging.go#L59>)
### func \(\*Logger\) [Warn](<https://github.com/tfadeyi/sloth-simple-comments/blob/main/internal/logging/logging.go#L63>)

```go
func (l *Logger) Warn(err error, keysAndValues ...interface{})
Expand All @@ -107,7 +107,7 @@ func (l *Logger) Warn(err error, keysAndValues ...interface{})
Warn logs the error using the warning log level \(3\)

<a name="Logger.WithName"></a>
### func \(\*Logger\) [WithName](<https://github.com/tfadeyi/sloth-simple-comments/blob/main/internal/logging/logging.go#L39>)
### func \(\*Logger\) [WithName](<https://github.com/tfadeyi/sloth-simple-comments/blob/main/internal/logging/logging.go#L43>)

```go
func (l *Logger) WithName(name string) Logger
Expand Down
8 changes: 6 additions & 2 deletions internal/logging/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ type (
}
)

const (
debug = stdr.MessageClass(5)
)

// ContextWithLogger wraps the logr NewContext function
func ContextWithLogger(ctx context.Context, l Logger) context.Context {
return logr.NewContext(ctx, l.Logger)
Expand Down Expand Up @@ -62,15 +66,15 @@ func (l *Logger) Warn(err error, keysAndValues ...interface{}) {

// Debug logs the message using the info debug level (1)
func (l *Logger) Debug(msg string, keysAndValues ...interface{}) {
l.V(5).Info(msg, keysAndValues...)
l.V(int(debug)).Info(msg, keysAndValues...)
}

func findLogLevel(lvl string) stdr.MessageClass {
switch strings.TrimSpace(strings.ToLower(lvl)) {
case "info":
return stdr.Info
case "debug":
return stdr.MessageClass(5)
return debug
case "warn":
return stdr.Error
case "none":
Expand Down
6 changes: 3 additions & 3 deletions internal/logging/logging_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,15 @@ func TestFindLevel(t *testing.T) {
t.Parallel()

t.Run("Successfully return stdr.All for debug as input", func(t *testing.T) {
assert.Equal(t, stdr.All, findLogLevel("debug"))
assert.Equal(t, debug, findLogLevel("debug"))
})

t.Run("Successfully return stdr.All for debug(with whitespace) as input", func(t *testing.T) {
assert.Equal(t, stdr.All, findLogLevel(" debug "))
assert.Equal(t, debug, findLogLevel(" debug "))
})

t.Run("Successfully return stdr.All for DEBUG as input", func(t *testing.T) {
assert.Equal(t, stdr.All, findLogLevel("DEBUG"))
assert.Equal(t, debug, findLogLevel("DEBUG"))
})

t.Run("Successfully return stdr.Error for warn as input", func(t *testing.T) {
Expand Down
21 changes: 16 additions & 5 deletions internal/version/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,38 @@ Package version, returns the build info of the binary

- [Variables](<#variables>)
- [func BuildInfo\(\) string](<#BuildInfo>)
- [func Info\(\) string](<#Info>)


## Variables

<a name="Version"></a>Values injected at build\-time
<a name="Platform"></a>Values injected at build\-time

```go
var (
Version string = "dev"
Commit string = "unknown"
Date string = "unknown"
Platform string = "unknown"
Version string = "dev"
Commit string = "unknown"
Date string = "unknown"
)
```

<a name="BuildInfo"></a>
## func [BuildInfo](<https://github.com/tfadeyi/sloth-simple-comments/blob/main/internal/version/version.go#L13>)
## func [BuildInfo](<https://github.com/tfadeyi/sloth-simple-comments/blob/main/internal/version/version.go#L16>)

```go
func BuildInfo() string
```

BuildInfo returns the binary build information

<a name="Info"></a>
## func [Info](<https://github.com/tfadeyi/sloth-simple-comments/blob/main/internal/version/version.go#L26>)

```go
func Info() string
```

Info returns simple version information of the binary

Generated by [gomarkdoc](<https://github.com/princjef/gomarkdoc>)
5 changes: 5 additions & 0 deletions internal/version/version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,8 @@ Host machine: %s
Commit: %s
Built at: %s`, Platform, Version, Commit, Date), info)
}

func TestInfo(t *testing.T) {
info := Info()
assert.Equal(t, Version, info)
}

0 comments on commit ac29c7d

Please sign in to comment.