2019-10-07 22:14:47 +08:00
|
|
|
// Copyright (c) Mainflux
|
2018-08-26 19:15:48 +08:00
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
2018-04-04 16:15:23 +08:00
|
|
|
package logger
|
|
|
|
|
2018-09-27 00:58:51 +08:00
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2018-04-04 16:15:23 +08:00
|
|
|
const (
|
|
|
|
// Error level is used when logging errors.
|
|
|
|
Error Level = iota + 1
|
|
|
|
// Warn level is used when logging warnings.
|
|
|
|
Warn
|
|
|
|
// Info level is used when logging info data.
|
|
|
|
Info
|
2018-09-27 00:58:51 +08:00
|
|
|
// Debug level is used when logging debugging info.
|
|
|
|
Debug
|
2018-04-04 16:15:23 +08:00
|
|
|
)
|
|
|
|
|
2022-03-04 00:13:46 +08:00
|
|
|
// ErrInvalidLogLevel indicates an unrecognized log level.
|
2018-09-27 00:58:51 +08:00
|
|
|
var ErrInvalidLogLevel = errors.New("unrecognized log level")
|
|
|
|
|
2018-04-04 16:15:23 +08:00
|
|
|
// Level represents severity level while logging.
|
|
|
|
type Level int
|
|
|
|
|
|
|
|
var levels = map[Level]string{
|
|
|
|
Error: "error",
|
|
|
|
Warn: "warn",
|
|
|
|
Info: "info",
|
2018-09-27 00:58:51 +08:00
|
|
|
Debug: "debug",
|
2018-04-04 16:15:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (lvl Level) String() string {
|
|
|
|
return levels[lvl]
|
|
|
|
}
|
2018-09-27 00:58:51 +08:00
|
|
|
|
|
|
|
func (lvl Level) isAllowed(logLevel Level) bool {
|
|
|
|
return lvl <= logLevel
|
|
|
|
}
|
|
|
|
|
2021-01-26 08:07:06 +08:00
|
|
|
// UnmarshalText returns log Level for the given string representation.
|
2018-09-27 00:58:51 +08:00
|
|
|
func (lvl *Level) UnmarshalText(text string) error {
|
2021-01-26 08:07:06 +08:00
|
|
|
switch strings.ToLower(text) {
|
2018-09-27 00:58:51 +08:00
|
|
|
case "debug":
|
|
|
|
*lvl = Debug
|
|
|
|
case "info":
|
|
|
|
*lvl = Info
|
|
|
|
case "warn":
|
|
|
|
*lvl = Warn
|
|
|
|
case "error":
|
|
|
|
*lvl = Error
|
|
|
|
default:
|
|
|
|
return ErrInvalidLogLevel
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|