-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement logger adapter (#67)
* feat: implement logger adapter * improve test coverage
- Loading branch information
Showing
8 changed files
with
408 additions
and
6 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
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,80 @@ | ||
package logger | ||
|
||
import ( | ||
"log" | ||
"os" | ||
"sync/atomic" | ||
) | ||
|
||
var defaultLogger atomic.Value | ||
|
||
func init() { | ||
defaultLogger.Store(NewSimpleLogger( | ||
log.New(os.Stdout, "", log.LstdFlags|log.Lshortfile), LevelInfo), | ||
) | ||
} | ||
|
||
// Default returns the default Logger. | ||
func Default() Logger { | ||
return defaultLogger.Load().(Logger) | ||
} | ||
|
||
// SetDefault makes l the default Logger. | ||
func SetDefault(l Logger) { | ||
defaultLogger.Store(l) | ||
} | ||
|
||
// Trace logs at LevelTrace. | ||
func Trace(msg any) { | ||
Default().Trace(msg) | ||
} | ||
|
||
// Tracef logs at LevelTrace. | ||
func Tracef(format string, args ...any) { | ||
Default().Tracef(format, args...) | ||
} | ||
|
||
// Debug logs at LevelDebug. | ||
func Debug(msg any) { | ||
Default().Debug(msg) | ||
} | ||
|
||
// Debugf logs at LevelDebug. | ||
func Debugf(format string, args ...any) { | ||
Default().Debugf(format, args...) | ||
} | ||
|
||
// Info logs at LevelInfo. | ||
func Info(msg any) { | ||
Default().Info(msg) | ||
} | ||
|
||
// Infof logs at LevelInfo. | ||
func Infof(format string, args ...any) { | ||
Default().Infof(format, args...) | ||
} | ||
|
||
// Warn logs at LevelWarn. | ||
func Warn(msg any) { | ||
Default().Warn(msg) | ||
} | ||
|
||
// Warnf logs at LevelWarn. | ||
func Warnf(format string, args ...any) { | ||
Default().Warnf(format, args...) | ||
} | ||
|
||
// Error logs at LevelError. | ||
func Error(msg any) { | ||
Default().Error(msg) | ||
} | ||
|
||
// Errorf logs at LevelError. | ||
func Errorf(format string, args ...any) { | ||
Default().Errorf(format, args...) | ||
} | ||
|
||
// Enabled reports whether the logger handles records at the given level. | ||
func Enabled(level Level) bool { | ||
return Default().Enabled(level) | ||
} |
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,15 @@ | ||
package logger | ||
|
||
// A Level is the importance or severity of a log event. | ||
// The higher the level, the more important or severe the event. | ||
type Level int | ||
|
||
// Names for common log levels. | ||
const ( | ||
LevelTrace Level = -8 | ||
LevelDebug Level = -4 | ||
LevelInfo Level = 0 | ||
LevelWarn Level = 4 | ||
LevelError Level = 8 | ||
LevelOff Level = 12 | ||
) |
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,38 @@ | ||
package logger | ||
|
||
// A Logger handles log records. | ||
type Logger interface { | ||
|
||
// Trace logs at LevelTrace. | ||
Trace(msg any) | ||
|
||
// Tracef logs at LevelTrace. | ||
Tracef(format string, args ...any) | ||
|
||
// Debug logs at LevelDebug. | ||
Debug(msg any) | ||
|
||
// Debugf logs at LevelDebug. | ||
Debugf(format string, args ...any) | ||
|
||
// Info logs at LevelInfo. | ||
Info(msg any) | ||
|
||
// Infof logs at LevelInfo. | ||
Infof(format string, args ...any) | ||
|
||
// Warn logs at LevelWarn. | ||
Warn(msg any) | ||
|
||
// Warnf logs at LevelWarn. | ||
Warnf(format string, args ...any) | ||
|
||
// Error logs at LevelError. | ||
Error(msg any) | ||
|
||
// Errorf logs at LevelError. | ||
Errorf(format string, args ...any) | ||
|
||
// Enabled reports whether the logger handles records at the given level. | ||
Enabled(level Level) bool | ||
} |
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,122 @@ | ||
package logger_test | ||
|
||
import ( | ||
"bytes" | ||
"io" | ||
"log" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/reugn/go-quartz/quartz/logger" | ||
) | ||
|
||
func TestSimpleLogger(t *testing.T) { | ||
var b bytes.Buffer | ||
stdLogger := log.New(&b, "", log.LstdFlags) | ||
logger.SetDefault(logger.NewSimpleLogger(stdLogger, logger.LevelInfo)) | ||
|
||
logger.Trace("Trace") | ||
assertEmpty(&b, t) | ||
logger.Tracef("Trace%s", "f") | ||
assertEmpty(&b, t) | ||
|
||
logger.Debug("Debug") | ||
assertEmpty(&b, t) | ||
logger.Debugf("Debug%s", "f") | ||
assertEmpty(&b, t) | ||
|
||
logger.Info("Info") | ||
assertNotEmpty(&b, t) | ||
logger.Infof("Info%s", "f") | ||
assertNotEmpty(&b, t) | ||
|
||
logger.Warn("Warn") | ||
assertNotEmpty(&b, t) | ||
logger.Warnf("Warn%s", "f") | ||
assertNotEmpty(&b, t) | ||
|
||
logger.Error("Error") | ||
assertNotEmpty(&b, t) | ||
logger.Errorf("Error%s", "f") | ||
assertNotEmpty(&b, t) | ||
} | ||
|
||
func TestLoggerOff(t *testing.T) { | ||
var b bytes.Buffer | ||
stdLogger := log.New(&b, "", log.LstdFlags) | ||
logger.SetDefault(logger.NewSimpleLogger(stdLogger, logger.LevelOff)) | ||
|
||
if logger.Enabled(logger.LevelError) { | ||
t.Fatal("LevelError is enabled") | ||
} | ||
logger.Error("Error") | ||
assertEmpty(&b, t) | ||
logger.Errorf("Error%s", "f") | ||
assertEmpty(&b, t) | ||
} | ||
|
||
func TestLogFormat(t *testing.T) { | ||
var b bytes.Buffer | ||
stdLogger := log.New(&b, "", log.LstdFlags) | ||
logr := logger.NewSimpleLogger(stdLogger, logger.LevelTrace) | ||
logger.SetDefault(logr) | ||
|
||
empty := struct{}{} | ||
logr.Trace("Trace") | ||
assertNotEmpty(&b, t) | ||
logr.Tracef("Tracef: %s, %d, %v, %v", "a", 1, true, empty) | ||
checkLogFormat(&b, t) | ||
logger.Tracef("Tracef: %s, %d, %v, %v", "a", 1, true, empty) | ||
checkLogFormat(&b, t) | ||
|
||
logr.Debug("Debug") | ||
assertNotEmpty(&b, t) | ||
logr.Debugf("Debugf: %s, %d, %v, %v", "a", 1, true, empty) | ||
checkLogFormat(&b, t) | ||
logger.Debugf("Debugf: %s, %d, %v, %v", "a", 1, true, empty) | ||
checkLogFormat(&b, t) | ||
|
||
logr.Infof("Infof: %s, %d, %v, %v", "a", 1, true, empty) | ||
checkLogFormat(&b, t) | ||
logger.Infof("Infof: %s, %d, %v, %v", "a", 1, true, empty) | ||
checkLogFormat(&b, t) | ||
|
||
logr.Warnf("Warnf: %s, %d, %v, %v", "a", 1, true, empty) | ||
checkLogFormat(&b, t) | ||
logger.Warnf("Warnf: %s, %d, %v, %v", "a", 1, true, empty) | ||
checkLogFormat(&b, t) | ||
|
||
logr.Errorf("Errorf: %s, %d, %v, %v", "a", 1, true, empty) | ||
checkLogFormat(&b, t) | ||
logger.Errorf("Errorf: %s, %d, %v, %v", "a", 1, true, empty) | ||
checkLogFormat(&b, t) | ||
} | ||
|
||
func assertEmpty(r io.Reader, t *testing.T) { | ||
logMsg := readAll(r, t) | ||
if logMsg != "" { | ||
t.Fatalf("Log msg is not empty: %s", logMsg) | ||
} | ||
} | ||
|
||
func assertNotEmpty(r io.Reader, t *testing.T) { | ||
logMsg := readAll(r, t) | ||
if logMsg == "" { | ||
t.Fatal("Log msg is empty") | ||
} | ||
} | ||
|
||
func checkLogFormat(r io.Reader, t *testing.T) { | ||
logMsg := readAll(r, t) | ||
if !strings.Contains(logMsg, "a, 1, true, {}") { | ||
t.Fatalf("Invalid log format: %s", logMsg) | ||
} | ||
} | ||
|
||
func readAll(r io.Reader, t *testing.T) string { | ||
bytes, err := io.ReadAll(r) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
return string(bytes) | ||
} |
Oops, something went wrong.