mediamtx/internal/conf/log_level.go

65 lines
1.1 KiB
Go
Raw Normal View History

package conf
import (
"encoding/json"
"fmt"
2023-05-16 14:14:20 +00:00
"github.com/bluenviron/mediamtx/internal/logger"
)
// LogLevel is the logLevel parameter.
type LogLevel logger.Level
// MarshalJSON implements json.Marshaler.
func (d LogLevel) MarshalJSON() ([]byte, error) {
var out string
switch d {
2021-10-28 15:29:27 +00:00
case LogLevel(logger.Error):
out = "error"
case LogLevel(logger.Warn):
out = "warn"
case LogLevel(logger.Info):
out = "info"
default:
out = "debug"
}
return json.Marshal(out)
}
// UnmarshalJSON implements json.Unmarshaler.
func (d *LogLevel) UnmarshalJSON(b []byte) error {
var in string
if err := json.Unmarshal(b, &in); err != nil {
return err
}
switch in {
2021-10-28 15:29:27 +00:00
case "error":
*d = LogLevel(logger.Error)
case "warn":
*d = LogLevel(logger.Warn)
case "info":
*d = LogLevel(logger.Info)
case "debug":
*d = LogLevel(logger.Debug)
default:
return fmt.Errorf("invalid log level: '%s'", in)
}
return nil
}
// UnmarshalEnv implements env.Unmarshaler.
func (d *LogLevel) UnmarshalEnv(_ string, v string) error {
return d.UnmarshalJSON([]byte(`"` + v + `"`))
}