-
Notifications
You must be signed in to change notification settings - Fork 115
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
Feature: Add log api #144
Feature: Add log api #144
Conversation
Codecov Report
@@ Coverage Diff @@
## master #144 +/- ##
==========================================
- Coverage 78.75% 78.57% -0.18%
==========================================
Files 26 27 +1
Lines 1784 1802 +18
==========================================
+ Hits 1405 1416 +11
- Misses 252 259 +7
Partials 127 127
Continue to review full report at Codecov.
|
c17067d
to
eb2a27c
Compare
@andig, thanks for the PR! |
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.
a naming nit: there are now two directories that contain log implementation "/api/log" and "/internal/log" ... these are tightly connected to each other
/api/log
is (unlike others in /api) not used for influxDB API, but for API for the client API/internal/log
might not be considered internal anymore
... refactoring to a new simple /log directly might look better
Makes sense? |
I plan to use Uber zap.Logger as a default Logger. As it has not its own interface, it still makes sense to add a logger interface. The best place is to put it to the |
// Use of this source code is governed by MIT | ||
// license that can be found in the LICENSE file. | ||
|
||
package log |
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.
Move it to the /logger
, please.
type logLogger struct{} | ||
|
||
func (l *logLogger) Debugf(format string, v ...interface{}) { | ||
log.Print("[D]! ", fmt.Sprintf(format, v...)) | ||
} | ||
|
||
func (l *logLogger) Debug(msg string) { | ||
log.Print("[D]! ", msg) | ||
} | ||
|
||
func (l *logLogger) Infof(format string, v ...interface{}) { | ||
log.Print("[I]! ", fmt.Sprintf(format, v...)) | ||
} | ||
|
||
func (l *logLogger) Info(msg string) { | ||
log.Print("[I]! ", msg) | ||
} | ||
|
||
func (l *logLogger) Warnf(format string, v ...interface{}) { | ||
log.Print("[W]! ", fmt.Sprintf(format, v...)) | ||
} | ||
|
||
func (l *logLogger) Warn(msg string) { | ||
log.Print("[W]! ", msg) | ||
} | ||
|
||
func (l *logLogger) Errorf(format string, v ...interface{}) { | ||
log.Print("[E]! ", fmt.Sprintf(format, v...)) | ||
} | ||
|
||
func (l *logLogger) Error(msg string) { | ||
log.Print("[E]! ", msg) | ||
} |
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.
I think I undestand your idea behind having logLogger
and LogAdapater
, but it seems too much for just a simple logging mechanism.
Just keep the original implementation as the default implementation.
I would also create LogConfig
structure that will hold logging configuration. There is the LogLevel
property and I will add also Prefix
to prefix all logged lines. This will configure the logger via SetConfig
func.
var Log LogAdapter | ||
|
||
// Logger provides filtered and categorized logging API. | ||
// It logs to standard logger, only errors by default | ||
type Logger struct { | ||
// LogAdapter provides filtered and categorized logging API. | ||
// It logs using the api logger instance. Only errors are logged by default. | ||
type LogAdapter struct { | ||
debugLevel uint | ||
} | ||
|
||
// SetDebugLevel to filter log messages. Each level mean to log all categories bellow | ||
// 0 errors , 1 - warning, 2 - info, 3 - debug | ||
func (l *Logger) SetDebugLevel(debugLevel uint) { | ||
|
||
func (l *LogAdapter) SetDebugLevel(debugLevel uint) { | ||
l.debugLevel = debugLevel | ||
} | ||
|
||
func (l *Logger) Debugf(format string, v ...interface{}) { | ||
if l.debugLevel > 2 { | ||
log.Print("[D]! ", fmt.Sprintf(format, v...)) | ||
func (l *LogAdapter) Debugf(format string, v ...interface{}) { | ||
if log.Log != nil && l.debugLevel > 2 { | ||
log.Log.Debugf(format, v...) | ||
} | ||
} | ||
func (l *Logger) Debug(msg string) { | ||
if l.debugLevel > 2 { | ||
log.Print("[D]! ", msg) | ||
|
||
func (l *LogAdapter) Debug(msg string) { | ||
if log.Log != nil && l.debugLevel > 2 { | ||
log.Log.Debug(msg) | ||
} | ||
} | ||
|
||
func (l *Logger) Infof(format string, v ...interface{}) { | ||
if l.debugLevel > 1 { | ||
log.Print("[I]! ", fmt.Sprintf(format, v...)) | ||
func (l *LogAdapter) Infof(format string, v ...interface{}) { | ||
if log.Log != nil && l.debugLevel > 1 { | ||
log.Log.Infof(format, v...) | ||
} | ||
} | ||
func (l *Logger) Info(msg string) { | ||
if l.debugLevel > 1 { | ||
log.Print("[I]! ", msg) | ||
|
||
func (l *LogAdapter) Info(msg string) { | ||
if log.Log != nil && l.debugLevel > 1 { | ||
log.Log.Info(msg) | ||
} | ||
} | ||
|
||
func (l *Logger) Warnf(format string, v ...interface{}) { | ||
if l.debugLevel > 0 { | ||
log.Print("[W]! ", fmt.Sprintf(format, v...)) | ||
func (l *LogAdapter) Warnf(format string, v ...interface{}) { | ||
if log.Log != nil && l.debugLevel > 0 { | ||
log.Log.Warnf(format, v...) | ||
} | ||
} | ||
func (l *Logger) Warn(msg string) { | ||
if l.debugLevel > 0 { | ||
log.Print("[W]! ", msg) | ||
|
||
func (l *LogAdapter) Warn(msg string) { | ||
if log.Log != nil && l.debugLevel > 0 { | ||
log.Log.Warn(msg) | ||
} | ||
} | ||
|
||
func (l *Logger) Errorf(format string, v ...interface{}) { | ||
log.Print("[E]! ", fmt.Sprintf(format, v...)) | ||
func (l *LogAdapter) Errorf(format string, v ...interface{}) { | ||
if log.Log != nil { | ||
log.Log.Errorf(format, v...) | ||
} | ||
} | ||
|
||
func (l *Logger) Error(msg string) { | ||
log.Print("[E]! ", msg) | ||
func (l *LogAdapter) Error(msg string) { | ||
if log.Log != nil { | ||
log.Log.Error(msg) | ||
} | ||
} |
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.
There should be the default implementation with the original code. init()
could initially set logger.Log
@vlastahajek appreciate your comments but not sure I fully follow. I was trying to achieve
If that makes sense I could take another stab at it |
@andig, my comments mainly target the fact your proposal introduces two logging objects, the default implementation, In fact, in the actual state, the As the filtering capability, and also prefixing, is part of the logger responsibilities, the logger should handle that directly. |
Prefixing: ack Filtering: I think it comes down to where you want the SetLogLevel handled. Imho that is part of the client duties- it should only log what it is supposed to log. With that approach the adapter is needed or a lot if if/elses across the codebase. Same if you want to be able to nil the logger. I'd propose to keep the log interface slim and handle SetLogLevel in this library. |
As Logger defines what levels are supported, Logger should be responsible for filtering of those levels. |
@andig, thanks for your effort. I had to complete this, so I implemented that according to my thoughts. I hope it still will be useful for your needs. |
Thank you- just didn‘t get around to actually finish this, sorry. |
Fix #104:
Logger
apiLogAdapter
that connected client with public loggerSetDebugLevel
part ofLogAdapter
Briefly describe your proposed changes: