Skip to content

Commit

Permalink
Implement string to level helper (#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
instabledesign authored Oct 22, 2019
1 parent 36c1d76 commit 9095664
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 21 deletions.
57 changes: 37 additions & 20 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,31 +21,48 @@ const (
DebugLevel
)

// Level represent log Entry level
var stringToLevel = map[string]Level{
"emergency": EmergencyLevel,
"alert": AlertLevel,
"critical": CriticalLevel,
"error": ErrorLevel,
"warning": WarningLevel,
"notice": NoticeLevel,
"info": InfoLevel,
"debug": DebugLevel,
}

var levelToString = map[Level]string{
EmergencyLevel: "emergency",
AlertLevel: "alert",
CriticalLevel: "critical",
ErrorLevel: "error",
WarningLevel: "warning",
NoticeLevel: "notice",
InfoLevel: "info",
DebugLevel: "debug",
}

// LevelString represent log Entry level as string
type LevelString string

// Level will return log Level for string or DebugLevel if unknown value
func (l LevelString) Level() Level {
if v, ok := stringToLevel[string(l)]; ok {
return v
}
return DebugLevel
}

// Level represent log Entry levelString
type Level int8

// String will return Level as string
func (l Level) String() string {
switch l {
case DebugLevel:
return "debug"
case InfoLevel:
return "info"
case NoticeLevel:
return "notice"
case WarningLevel:
return "warning"
case ErrorLevel:
return "error"
case CriticalLevel:
return "critical"
case AlertLevel:
return "alert"
case EmergencyLevel:
return "emergency"
default:
return fmt.Sprintf("level(%d)", l)
if v, ok := levelToString[l]; ok {
return v
}
return fmt.Sprintf("level(%d)", l)
}

// LogInterface define simplest logger contract
Expand Down
23 changes: 22 additions & 1 deletion logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,27 @@ import (
"github.com/gol4ng/logger/middleware"
)

func TestLevelString_Level(t *testing.T) {
tests := []struct {
levelString logger.LevelString
expected logger.Level
}{
{levelString: "debug", expected: logger.DebugLevel},
{levelString: "info", expected: logger.InfoLevel},
{levelString: "notice", expected: logger.NoticeLevel},
{levelString: "warning", expected: logger.WarningLevel},
{levelString: "error", expected: logger.ErrorLevel},
{levelString: "critical", expected: logger.CriticalLevel},
{levelString: "alert", expected: logger.AlertLevel},
{levelString: "emergency", expected: logger.EmergencyLevel},
{levelString: "anothervalue", expected: logger.DebugLevel},
}

for _, tt := range tests {
assert.Equal(t, tt.expected, tt.levelString.Level())
}
}

func TestLevel_String(t *testing.T) {
tests := []struct {
level logger.Level
Expand Down Expand Up @@ -77,7 +98,7 @@ func TestLogger_Log(t *testing.T) {
level: logger.EmergencyLevel,
},
{
name: "test Log(custom level)",
name: "test Log(custom levelString)",
level: logger.Level(127),
},
}
Expand Down

0 comments on commit 9095664

Please sign in to comment.