logger: change fatal to error
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/pr Build is passing

and make the logger not make the program exit

Signed-off-by: Sam Therapy <sam@samtherapy.net>
This commit is contained in:
Sam Therapy 2022-07-24 18:54:10 +02:00
parent fd9afea5b0
commit c3198326ec
Signed by: sam
GPG key ID: 4D8B07C18F31ACBD
4 changed files with 18 additions and 24 deletions

View file

@ -40,15 +40,15 @@ func (l *Logger) level() Level {
func (l *Logger) UnMarshalLevel(lv Level) (string, error) {
switch lv {
case 0:
return "FATAL ", nil
case 1:
return "ERROR ", nil
case 1:
return "WARN ", nil
case 2:
return "INFO ", nil
case 3:
return "DEBUG ", nil
}
return "", fmt.Errorf("invalid log level choice")
return "", fmt.Errorf("invalid log level")
}
func (l *Logger) IsLevel(level Level) bool {
@ -56,18 +56,18 @@ func (l *Logger) IsLevel(level Level) bool {
}
var AllLevels = []Level{
FatalLevel,
ErrorLevel,
ErrLevel,
WarnLevel,
InfoLevel,
DebugLevel,
}
const (
// Fatal logs (will call exit(1)).
FatalLevel Level = iota
ErrLevel Level = iota
// Error logs.
ErrorLevel
WarnLevel
// What is going on level.
InfoLevel

View file

@ -30,28 +30,22 @@ func (l *Logger) Println(level Level, v ...any) {
case 0:
err := l.Printer(0, fmt.Sprintln(v...)) //Fatal level
if err != nil {
// TODO: Make this not a panic
panic(err)
fmt.Fprintln(os.Stderr, "FATAL: Logger failed: ", err)
}
os.Exit(1)
case 1:
err := l.Printer(1, fmt.Sprintln(v...)) //Error level
if err != nil {
// TODO: Make this not a panic
panic(err)
fmt.Fprintln(os.Stderr, "FATAL: Logger failed: ", err)
}
os.Exit(2)
case 2:
err := l.Printer(2, fmt.Sprintln(v...)) //Info level
if err != nil {
// TODO: Make this not a panic
panic(err)
fmt.Fprintln(os.Stderr, "FATAL: Logger failed: ", err)
}
case 3:
err := l.Printer(3, fmt.Sprintln(v...)) //Debug level
if err != nil {
// TODO: Make this not a panic
panic(err)
fmt.Fprintln(os.Stderr, "FATAL: Logger failed: ", err)
}
default:
break
@ -141,10 +135,10 @@ func (l *Logger) Info(v ...any) {
// Call print directly with Error level
func (l *Logger) Error(v ...any) {
l.Println(ErrorLevel, v...)
l.Println(WarnLevel, v...)
}
// Call print directly with Fatal level
func (l *Logger) Fatal(v ...any) {
l.Println(FatalLevel, v...)
l.Println(ErrLevel, v...)
}

View file

@ -41,7 +41,7 @@ func TestUnmarshalLevels(t *testing.T) {
lv, err := logger.UnMarshalLevel(logawl.Level(9001))
assert.Equal(t, "", lv)
assert.ErrorContains(t, err, "invalid log level choice")
assert.ErrorContains(t, err, "invalid log level")
}
func TestLogger(t *testing.T) {

View file

@ -33,7 +33,7 @@ func main() {
resp, err := query.CreateQuery(opts)
if err != nil {
opts.Logger.Fatal(err)
os.Exit(1)
os.Exit(9)
}
switch {
case opts.JSON:
@ -41,7 +41,7 @@ func main() {
json, err := json.MarshalIndent(resp.DNS, "", " ")
if err != nil {
opts.Logger.Fatal(err)
os.Exit(1)
os.Exit(10)
}
fmt.Println(string(json))
case opts.XML:
@ -49,7 +49,7 @@ func main() {
xml, err := xml.MarshalIndent(resp.DNS, "", " ")
if err != nil {
opts.Logger.Fatal(err)
os.Exit(1)
os.Exit(10)
}
fmt.Println(string(xml))
case opts.YAML:
@ -57,7 +57,7 @@ func main() {
yaml, err := yaml.Marshal(resp.DNS)
if err != nil {
opts.Logger.Fatal(err)
os.Exit(1)
os.Exit(10)
}
fmt.Println(string(yaml))
default: