-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: refactor logging system with conditional builds
- Remove logging implementation from `logger.go` - Create `logger_default.go` with the previous logging implementation from `logger.go` - Create `logger_slog.go` with a new logging implementation using `slog` for the `Infof` method - Add build tags to differentiate between `logger_default.go` and `logger_slog.go` based on Go version Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
- Loading branch information
Showing
3 changed files
with
99 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
//go:build !go1.21 | ||
|
||
package graceful | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"os" | ||
) | ||
|
||
// NewLogger for simple logger. | ||
func NewLogger() Logger { | ||
return defaultLogger{ | ||
infoLogger: log.New(os.Stdout, "INFO: ", log.Ldate|log.Ltime|log.Lshortfile), | ||
errorLogger: log.New(os.Stderr, "ERROR: ", log.Ldate|log.Ltime|log.Lshortfile), | ||
fatalLogger: log.New(os.Stderr, "FATAL: ", log.Ldate|log.Ltime|log.Lshortfile), | ||
} | ||
} | ||
|
||
type defaultLogger struct { | ||
infoLogger *log.Logger | ||
errorLogger *log.Logger | ||
fatalLogger *log.Logger | ||
} | ||
|
||
func (l defaultLogger) Infof(format string, args ...interface{}) { | ||
l.infoLogger.Printf(format, args...) | ||
} | ||
|
||
func (l defaultLogger) Errorf(format string, args ...interface{}) { | ||
l.errorLogger.Printf(format, args...) | ||
} | ||
|
||
func (l defaultLogger) Fatalf(format string, args ...interface{}) { | ||
l.fatalLogger.Fatalf(format, args...) | ||
} | ||
|
||
func (l defaultLogger) Info(args ...interface{}) { | ||
l.infoLogger.Println(fmt.Sprint(args...)) | ||
} | ||
|
||
func (l defaultLogger) Error(args ...interface{}) { | ||
l.errorLogger.Println(fmt.Sprint(args...)) | ||
} | ||
|
||
func (l defaultLogger) Fatal(args ...interface{}) { | ||
l.fatalLogger.Println(fmt.Sprint(args...)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
//go:build go1.21 | ||
|
||
package graceful | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"log/slog" | ||
"os" | ||
) | ||
|
||
// NewLogger for simple logger. | ||
func NewLogger() Logger { | ||
return defaultLogger{ | ||
infoLogger: log.New(os.Stdout, "INFO: ", log.Ldate|log.Ltime|log.Lshortfile), | ||
errorLogger: log.New(os.Stderr, "ERROR: ", log.Ldate|log.Ltime|log.Lshortfile), | ||
fatalLogger: log.New(os.Stderr, "FATAL: ", log.Ldate|log.Ltime|log.Lshortfile), | ||
} | ||
} | ||
|
||
type defaultLogger struct { | ||
infoLogger *log.Logger | ||
errorLogger *log.Logger | ||
fatalLogger *log.Logger | ||
} | ||
|
||
func (l defaultLogger) Infof(format string, args ...interface{}) { | ||
slog.Info(format, args...) | ||
} | ||
|
||
func (l defaultLogger) Errorf(format string, args ...interface{}) { | ||
l.errorLogger.Printf(format, args...) | ||
} | ||
|
||
func (l defaultLogger) Fatalf(format string, args ...interface{}) { | ||
l.fatalLogger.Fatalf(format, args...) | ||
} | ||
|
||
func (l defaultLogger) Info(args ...interface{}) { | ||
l.infoLogger.Println(fmt.Sprint(args...)) | ||
} | ||
|
||
func (l defaultLogger) Error(args ...interface{}) { | ||
l.errorLogger.Println(fmt.Sprint(args...)) | ||
} | ||
|
||
func (l defaultLogger) Fatal(args ...interface{}) { | ||
l.fatalLogger.Println(fmt.Sprint(args...)) | ||
} |