From c3198326eca3da6f185eee42a8e8c2f034142c1c Mon Sep 17 00:00:00 2001 From: Sam Therapy Date: Sun, 24 Jul 2022 18:54:10 +0200 Subject: [PATCH] logger: change fatal to error and make the logger not make the program exit Signed-off-by: Sam Therapy --- logawl/logawl.go | 14 +++++++------- logawl/logger.go | 18 ++++++------------ logawl/logging_test.go | 2 +- main.go | 8 ++++---- 4 files changed, 18 insertions(+), 24 deletions(-) diff --git a/logawl/logawl.go b/logawl/logawl.go index 5cbe445..70c9416 100644 --- a/logawl/logawl.go +++ b/logawl/logawl.go @@ -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 diff --git a/logawl/logger.go b/logawl/logger.go index c2311c8..ddae454 100644 --- a/logawl/logger.go +++ b/logawl/logger.go @@ -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...) } diff --git a/logawl/logging_test.go b/logawl/logging_test.go index 202feca..ffbcf60 100644 --- a/logawl/logging_test.go +++ b/logawl/logging_test.go @@ -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) { diff --git a/main.go b/main.go index 9583128..923045a 100644 --- a/main.go +++ b/main.go @@ -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: