-
Notifications
You must be signed in to change notification settings - Fork 39
Enhanced Logging Interface #80
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
aaeb082
5e8be8a
6d14636
8eeb8b6
59965a7
401c496
b70fb8d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,6 +19,44 @@ import ( | |
| "github.com/grid-x/serial" | ||
| ) | ||
|
|
||
| type logger struct { | ||
| } | ||
|
|
||
| func newLogger(out io.Writer, prefix string, flag int) logger { | ||
| log.SetOutput(out) | ||
| log.SetPrefix(prefix) | ||
| log.SetFlags(flag) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. any reason why you are using
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In addition, is there a reason why this logger implements more methods than those specified in the interface? Maybe it makes sense to simply create a slog instance, this would match the defined interface. In the modbus cli case, this would be a breaking change, which in my opinion is acceptable. logger := slog.Default()
logger.Info(...)
logger.Debug(...)
logger.Error(...) |
||
| return logger{} | ||
| } | ||
|
|
||
| func (l logger) Printf(format string, v ...interface{}) { | ||
| log.Printf(format, v...) | ||
| } | ||
|
|
||
| func (l logger) Println(format string, v ...interface{}) { | ||
| log.Printf(format, v...) | ||
| } | ||
|
|
||
| func (l logger) Debug(format string, v ...interface{}) { | ||
| log.Printf("DEBUG: "+format, v...) | ||
| } | ||
|
|
||
| func (l logger) Info(format string, v ...interface{}) { | ||
| log.Printf("INFO: "+format, v...) | ||
| } | ||
|
|
||
| func (l logger) Error(format string, v ...interface{}) { | ||
| log.Printf("ERROR: "+format, v...) | ||
| } | ||
|
|
||
| func (l logger) Fatalf(format string, v ...interface{}) { | ||
| log.Fatalf(format, v...) | ||
| } | ||
|
|
||
| func (l logger) Fatal(v ...interface{}) { | ||
| log.Fatal(v...) | ||
| } | ||
|
|
||
| func main() { | ||
| var opt option | ||
| // general | ||
|
|
@@ -64,7 +102,7 @@ func main() { | |
| return | ||
| } | ||
|
|
||
| logger := log.New(os.Stdout, "", 0) | ||
| logger := newLogger(os.Stdout, "", 0) | ||
| if *register > math.MaxUint16 || *register < 0 { | ||
| logger.Fatalf("invalid register value: %d", *register) | ||
| } | ||
|
|
@@ -408,10 +446,6 @@ func resultToString(r []byte, order binary.ByteOrder, forcedOrder string, varTyp | |
| return "", fmt.Errorf("unsupported datatype: %s", varType) | ||
| } | ||
|
|
||
| type logger interface { | ||
| Printf(format string, v ...interface{}) | ||
| } | ||
|
|
||
| type option struct { | ||
| address string | ||
| slaveID int | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
From my point of view, it would be a good improvement if the available API calls would take the context into account.
Does it perhaps make sense to include the context already in the logger? The Slog impl. itself would fulfills it too.