Skip to content

Commit

Permalink
feat: parse level from string (#11)
Browse files Browse the repository at this point in the history
* Read log level from env var

* Fixes #9.

* With these changes, the log level will be able to read from the env var with
`LOG_LEVEL` key.

Signed-off-by: Gökhan Özeloğlu <gokhan.ozeloglu@deliveryhero.com>

* Replace os.Getenv with t.Getenv

* Update .gitignore.

Signed-off-by: Gökhan Özeloğlu <gokhan.ozeloglu@deliveryhero.com>

* Add `WithLevelFromString` option

* `readLevelFromEnv` function is removed.

* Tests updated.

Signed-off-by: Gökhan Özeloğlu <gokhan.ozeloglu@deliveryhero.com>

* Add `ParseLevel` function

* This function converts string to Level type.

* Unit test implemented.

Signed-off-by: Gökhan Özeloğlu <gokhan.ozeloglu@deliveryhero.com>

* Remove switch-case blocks

* Replace swich-case blocks with `ParseLevel` function in `WithLevelFromString`.

Signed-off-by: Gökhan Özeloğlu <gokhan.ozeloglu@deliveryhero.com>

* Remove `WithLevelFromString` option

Signed-off-by: Gökhan Özeloğlu <gokhan.ozeloglu@deliveryhero.com>

* Update tests

* Revert changes in `log_test.go`.

* Update test cases in `level_test.go`.

Signed-off-by: Gökhan Özeloğlu <gokhan.ozeloglu@deliveryhero.com>

* Remove empty line

Signed-off-by: Gökhan Özeloğlu <gokhan.ozeloglu@deliveryhero.com>

* Remove env var related test cases

Signed-off-by: Gökhan Özeloğlu <gokhan.ozeloglu@deliveryhero.com>

* Remove unneeded line

Signed-off-by: Gökhan Özeloğlu <gokhan.ozeloglu@deliveryhero.com>

---------

Signed-off-by: Gökhan Özeloğlu <gokhan.ozeloglu@deliveryhero.com>
  • Loading branch information
gozeloglu authored Feb 24, 2023
1 parent 685b3e2 commit baccac6
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
20 changes: 20 additions & 0 deletions level.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package log

import "strings"

// Level is a logging level.
type Level int32

Expand Down Expand Up @@ -35,3 +37,21 @@ func (l Level) String() string {
return ""
}
}

// ParseLevel converts level in string to Level type. Default level is InfoLevel.
func ParseLevel(level string) Level {
switch strings.ToLower(level) {
case DebugLevel.String():
return DebugLevel
case InfoLevel.String():
return InfoLevel
case WarnLevel.String():
return WarnLevel
case ErrorLevel.String():
return ErrorLevel
case FatalLevel.String():
return FatalLevel
default:
return InfoLevel
}
}
49 changes: 49 additions & 0 deletions level_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,52 @@ func TestDefaultLevel(t *testing.T) {
var level Level
assert.Equal(t, InfoLevel, level)
}
func TestParseLevel(t *testing.T) {
testCases := []struct {
name string
level string
expLevel Level
}{
{
name: "Parse debug",
level: "debug",
expLevel: DebugLevel,
},
{
name: "Parse info",
level: "Info",
expLevel: InfoLevel,
},
{
name: "Parse warn",
level: "WARN",
expLevel: WarnLevel,
},
{
name: "Parse error",
level: "error",
expLevel: ErrorLevel,
},
{
name: "Parse fatal",
level: "FATAL",
expLevel: FatalLevel,
},
{
name: "Default",
level: "",
expLevel: InfoLevel,
},
{
name: "Wrong level, set INFO",
level: "WRONG_LEVEL",
expLevel: InfoLevel,
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
assert.Equal(t, tc.expLevel, ParseLevel(tc.level))
})
}
}

0 comments on commit baccac6

Please sign in to comment.