-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
session: allow log configuration using Logger interface.
The driver now provides a Logger interface allowing to supply a custom logger as part of (Session)ConnConfig. Driver also provides three implementations of Logger by itself: - DefaultLogger, logging only warnings - DebugLogger, logging everything that can be useful for debugging, it is used by default in tests - NopLogger, logging nothing Fixes #271
- Loading branch information
Showing
12 changed files
with
148 additions
and
59 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package log | ||
|
||
import ( | ||
"log" | ||
"os" | ||
) | ||
|
||
type Logger interface { | ||
Info(v ...any) | ||
Infof(format string, v ...any) | ||
Infoln(v ...any) | ||
|
||
Warn(v ...any) | ||
Warnf(format string, v ...any) | ||
Warnln(v ...any) | ||
} | ||
|
||
// DefaultLogger only logs warnings and critical errors. | ||
type DefaultLogger struct { | ||
warn *log.Logger | ||
} | ||
|
||
func NewDefaultLogger() *DefaultLogger { | ||
res := &DefaultLogger{ | ||
warn: log.New(os.Stderr, "WARNING ", log.LstdFlags), | ||
} | ||
return res | ||
} | ||
|
||
func (logger *DefaultLogger) Info(v ...any) {} | ||
func (logger *DefaultLogger) Infof(format string, v ...any) {} | ||
func (logger *DefaultLogger) Infoln(v ...any) {} | ||
|
||
func (logger *DefaultLogger) Warn(v ...any) { logger.warn.Print(v...) } | ||
func (logger *DefaultLogger) Warnf(format string, v ...any) { logger.warn.Printf(format, v...) } | ||
func (logger *DefaultLogger) Warnln(v ...any) { logger.warn.Println(v...) } | ||
|
||
// DebugLogger logs both warnings and information about important events in driver's runtime. | ||
type DebugLogger struct { | ||
info *log.Logger | ||
warn *log.Logger | ||
} | ||
|
||
func NewDebugLogger() *DebugLogger { | ||
res := &DebugLogger{ | ||
info: log.New(os.Stderr, "INFO ", log.LstdFlags), | ||
warn: log.New(os.Stderr, "WARNING ", log.LstdFlags), | ||
} | ||
return res | ||
} | ||
|
||
func (logger *DebugLogger) Info(v ...any) { logger.info.Print(v...) } | ||
func (logger *DebugLogger) Infof(format string, v ...any) { logger.info.Printf(format, v...) } | ||
func (logger *DebugLogger) Infoln(v ...any) { logger.info.Println(v...) } | ||
|
||
func (logger *DebugLogger) Warn(v ...any) { logger.warn.Print(v...) } | ||
func (logger *DebugLogger) Warnf(format string, v ...any) { logger.warn.Printf(format, v...) } | ||
func (logger *DebugLogger) Warnln(v ...any) { logger.warn.Println(v...) } | ||
|
||
// NopLogger doesn't log anything. | ||
type NopLogger struct{} | ||
|
||
func (NopLogger) Info(v ...any) {} | ||
func (NopLogger) Infof(format string, v ...any) {} | ||
func (NopLogger) Infoln(v ...any) {} | ||
|
||
func (NopLogger) Warn(v ...any) {} | ||
func (NopLogger) Warnf(format string, v ...any) {} | ||
func (NopLogger) Warnln(v ...any) {} |
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
Oops, something went wrong.