Minor (complete) refactor #38
4 changed files with 18 additions and 24 deletions
|
@ -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
|
||||
|
|
|
@ -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...)
|
||||
}
|
||||
|
|
|
@ -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) {
|
||||
|
|
8
main.go
8
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:
|
||||
|
|
Loading…
Reference in a new issue