2023-06-14 18:40:37 +08:00
|
|
|
// Copyright (c) Mainflux
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
2018-09-27 00:58:51 +08:00
|
|
|
package logger
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
2023-02-23 03:50:51 +08:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2018-09-27 00:58:51 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestUnmarshalText(t *testing.T) {
|
2023-02-23 03:50:51 +08:00
|
|
|
cases := []struct {
|
|
|
|
desc string
|
2018-09-27 00:58:51 +08:00
|
|
|
input string
|
|
|
|
output Level
|
|
|
|
err error
|
|
|
|
}{
|
2023-02-23 03:50:51 +08:00
|
|
|
{
|
|
|
|
desc: "select log level Not_A_Level",
|
|
|
|
input: "Not_A_Level",
|
|
|
|
output: 0,
|
|
|
|
err: ErrInvalidLogLevel,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "select log level Bad_Input",
|
|
|
|
input: "Bad_Input",
|
|
|
|
output: 0,
|
|
|
|
err: ErrInvalidLogLevel,
|
|
|
|
},
|
2018-09-27 00:58:51 +08:00
|
|
|
|
2023-02-23 03:50:51 +08:00
|
|
|
{
|
|
|
|
desc: "select log level debug",
|
|
|
|
input: "debug",
|
|
|
|
output: Debug,
|
|
|
|
err: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "select log level DEBUG",
|
|
|
|
input: "DEBUG",
|
|
|
|
output: Debug,
|
|
|
|
err: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "select log level info",
|
|
|
|
input: "info",
|
|
|
|
output: Info,
|
|
|
|
err: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "select log level INFO",
|
|
|
|
input: "INFO",
|
|
|
|
output: Info,
|
|
|
|
err: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "select log level warn",
|
|
|
|
input: "warn",
|
|
|
|
output: Warn,
|
|
|
|
err: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "select log level WARN",
|
|
|
|
input: "WARN",
|
|
|
|
output: Warn,
|
|
|
|
err: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "select log level Error",
|
|
|
|
input: "Error",
|
|
|
|
output: Error,
|
|
|
|
err: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "select log level ERROR",
|
|
|
|
input: "ERROR",
|
|
|
|
output: Error,
|
|
|
|
err: nil,
|
|
|
|
},
|
2018-09-27 00:58:51 +08:00
|
|
|
}
|
|
|
|
|
2023-02-23 03:50:51 +08:00
|
|
|
for _, tc := range cases {
|
2018-09-27 02:22:26 +08:00
|
|
|
var logLevel Level
|
2018-09-27 00:58:51 +08:00
|
|
|
err := logLevel.UnmarshalText(tc.input)
|
2023-02-23 03:50:51 +08:00
|
|
|
assert.Equal(t, tc.output, logLevel, fmt.Sprintf("%s: expected %s got %d", tc.desc, tc.output, logLevel))
|
|
|
|
assert.Equal(t, tc.err, err, fmt.Sprintf("%s: expected %s got %d", tc.desc, tc.err, err))
|
2018-09-27 00:58:51 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLevelIsAllowed(t *testing.T) {
|
2023-02-23 03:50:51 +08:00
|
|
|
cases := []struct {
|
|
|
|
desc string
|
2018-09-27 00:58:51 +08:00
|
|
|
requestedLevel Level
|
|
|
|
allowedLevel Level
|
|
|
|
output bool
|
|
|
|
}{
|
2023-02-23 03:50:51 +08:00
|
|
|
{
|
|
|
|
desc: "log debug when level debug",
|
|
|
|
requestedLevel: Debug,
|
|
|
|
allowedLevel: Debug,
|
|
|
|
output: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "log info when level debug",
|
|
|
|
requestedLevel: Info,
|
|
|
|
allowedLevel: Debug,
|
|
|
|
output: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "log warn when level debug",
|
|
|
|
requestedLevel: Warn,
|
|
|
|
allowedLevel: Debug,
|
|
|
|
output: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "log error when level debug",
|
|
|
|
requestedLevel: Error,
|
|
|
|
allowedLevel: Debug,
|
|
|
|
output: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "log warn when level info",
|
|
|
|
requestedLevel: Warn,
|
|
|
|
allowedLevel: Info,
|
|
|
|
output: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "log error when level warn",
|
|
|
|
requestedLevel: Error,
|
|
|
|
allowedLevel: Warn,
|
|
|
|
output: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "log error when level error",
|
|
|
|
requestedLevel: Error,
|
|
|
|
allowedLevel: Error,
|
|
|
|
output: true,
|
|
|
|
},
|
2018-09-27 00:58:51 +08:00
|
|
|
|
2023-02-23 03:50:51 +08:00
|
|
|
{
|
|
|
|
desc: "log debug when level error",
|
|
|
|
requestedLevel: Debug,
|
|
|
|
allowedLevel: Error,
|
|
|
|
output: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "log info when level error",
|
|
|
|
requestedLevel: Info,
|
|
|
|
allowedLevel: Error,
|
|
|
|
output: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "log warn when level error",
|
|
|
|
requestedLevel: Warn,
|
|
|
|
allowedLevel: Error,
|
|
|
|
output: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "log debug when level warn",
|
|
|
|
requestedLevel: Debug,
|
|
|
|
allowedLevel: Warn,
|
|
|
|
output: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "log info when level warn",
|
|
|
|
requestedLevel: Info,
|
|
|
|
allowedLevel: Warn,
|
|
|
|
output: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "log debug when level info",
|
|
|
|
requestedLevel: Debug,
|
|
|
|
allowedLevel: Info,
|
|
|
|
output: false,
|
|
|
|
},
|
2018-09-27 00:58:51 +08:00
|
|
|
}
|
2023-02-23 03:50:51 +08:00
|
|
|
for _, tc := range cases {
|
2018-09-27 00:58:51 +08:00
|
|
|
result := tc.requestedLevel.isAllowed(tc.allowedLevel)
|
2023-02-23 03:50:51 +08:00
|
|
|
assert.Equal(t, tc.output, result, fmt.Sprintf("%s: expected %t got %t", tc.desc, tc.output, result))
|
2018-09-27 00:58:51 +08:00
|
|
|
}
|
|
|
|
}
|