Skip to content

Commit

Permalink
feat: default logger
Browse files Browse the repository at this point in the history
  • Loading branch information
eliasfeijo committed Jan 19, 2024
1 parent deba833 commit 4d431e9
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 26 deletions.
2 changes: 1 addition & 1 deletion examples/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

func main() {
logger := NewLogger(log.Debug)
logger := log.NewDefaultLogger(log.Debug)
log.SetLogger(logger)

if len(os.Args) < 2 {
Expand Down
48 changes: 23 additions & 25 deletions examples/logger.go → log/logger.go
Original file line number Diff line number Diff line change
@@ -1,68 +1,66 @@
package main
package log

import (
"fmt"
"os"

"github.com/bancodobrasil/goauth/log"
)

// Logger is a simple logger that prints to stdout
type Logger struct {
LogLevel log.LogLevel
type DefaultLogger struct {
LogLevel LogLevel
}

func NewLogger(logLevel log.LogLevel) *Logger {
if logLevel < log.Debug {
logLevel = log.Debug
} else if logLevel > log.Panic {
logLevel = log.Panic
func NewDefaultLogger(logLevel LogLevel) *DefaultLogger {
if logLevel < Debug {
logLevel = Debug
} else if logLevel > Panic {
logLevel = Panic
}
return &Logger{logLevel}
return &DefaultLogger{logLevel}
}

// Log prints a line to stdout
func (l *Logger) Log(level log.LogLevel, args ...any) {
func (l *DefaultLogger) Log(level LogLevel, args ...any) {
if level < l.LogLevel {
return
}
switch level {
case log.Debug:
case Debug:
fmt.Println("DEBUG:", fmt.Sprint(args...))
case log.Info:
case Info:
fmt.Println("INFO:", fmt.Sprint(args...))
case log.Warn:
case Warn:
fmt.Println("WARN:", fmt.Sprint(args...))
case log.Error:
case Error:
fmt.Println("ERROR:", fmt.Sprint(args...))
case log.Fatal:
case Fatal:
fmt.Println("FATAL:", fmt.Sprint(args...))
os.Exit(1)
case log.Panic:
case Panic:
panic(fmt.Sprint(args...))
default:
fmt.Println(args...)
}
}

// Logf prints a formatted line to stdout
func (l *Logger) Logf(level log.LogLevel, format string, args ...any) {
func (l *DefaultLogger) Logf(level LogLevel, format string, args ...any) {
if level < l.LogLevel {
return
}
switch level {
case log.Debug:
case Debug:
fmt.Printf("DEBUG: "+format, args...)
case log.Info:
case Info:
fmt.Printf("INFO: "+format, args...)
case log.Warn:
case Warn:
fmt.Printf("WARN: "+format, args...)
case log.Error:
case Error:
fmt.Printf("ERROR: "+format, args...)
case log.Fatal:
case Fatal:
fmt.Printf("FATAL: "+format, args...)
os.Exit(1)
case log.Panic:
case Panic:
panic(fmt.Sprintf(format, args...))
default:
fmt.Printf(format, args...)
Expand Down

0 comments on commit 4d431e9

Please sign in to comment.