Make all logging consistent and now only using the log kit pkg #389
Conversation
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.
Thanks, it is starting to look much better 👍
There was a little bit of oversight on my end. We want to have proper logging only for the mine and dataserver command as these are the only long running ones. For the other cmds that just execute an action and exit would be better to keep the use of fmt.Print
as this will present the result in a cleaner way than with a log line.
With this PR we want to also remove cfg.LogLevel
from the config.
so instead of createLogger
we will have 2 functions NewLogger
and ApplyFilter
func NewLogger() log.Logger{
logger := log.NewLogfmtLogger(log.NewSyncWriter(os.Stderr))
return log.With(logger, "ts", log.TimestampFormat(func() time.Time { return time.Now().UTC() }, "Jan 02 15:04:05.99"), "caller", log.DefaultCaller)
}
func ApplyFilter(logLevel string) (log.Logger,error) {
lvl := level.AllowInfo()
if level,ok:= cfg.Logger[ComponentName];ok{
switch level {
case "error":
lvl = level.AllowError()
case "warn":
lvl = level.AllowWarn()
case "info":
lvl = level.AllowInfo()
case "debug":
lvl = level.AllowDebug()
default:
return nil,errors.Errorf("unexpected log level:%v",level)
}
}
return level.NewFilter(logger, lvl),nil
}
basically each component in the cli has its own log level set by the config file.
In each major component constructor that takes a logger as a parameter should have something like:
const ComponentName = "trackers"
New(logger log.Logger, ctx...., cfg){
logger,err = ApplyFilter(cfg,ComponentName,logger)
}
This granular setup is important only for the dataserver and mine command as these are the only long-running commands, all the others just execute a single action and exit.
tl-dr - all commands should use NewLogger
in their Run()
Additionaly mine and dataserver cmds should also use ApplyFilter
in the constructors of some internal components where the logging might need more granular filtering.
At the end the logrus import should be removed "github.com/sirupsen/logrus"
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.
To avoid circular imports create a package logger and put ApplyFilter
and New
in it so we have logger.ApplyFilter
and logger.New
the logger and config should be passed to the component constructors use global constant for the ComponentName
So in tl-dr in cmds.go
use lgr:=util.NewLogger()
and cfg:=config.GetConfig
Then pass these when you initialize a component. for example on package ops
package ops
New(lgr,cfg){
lgr = logger.ApplyFilter(cfg, ComponentName, lgr)
lgr = log.With(lgr, "component", ComponentName)
}
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.
can you also update the configs/config.json
and also need to remove the logLevel flag from the cli flags.
Pull Request Test Coverage Report for Build 540039912
💛 - Coveralls |
Signed-off-by: Krasi Georgiev <8903888+krasi-georgiev@users.noreply.github.com>
42ab1a1
to
83bce6d
Compare
83bce6d
to
e744c8e
Compare
fixes: #185
When changing from fmt.Pr and Logrus to the go-kit logger i tried to follow the flow (with minimal code change), in terms of editing comments and making code changes.
There was one fmt.Pr i did not change, it was in the cmd/telliot/cmd.go Run function (i think), because it seemed to actually want to print info about the programs version.
Also by the end of this i had an extra file, that i have no idea if it actually is part of the project, it seemed to appear after i pulled some changes to make sure my code was auto mergable.
I did not remove it (even tho i cant find it in your repo), because when i realised it seemed entierly new i had already editeded its fmt.Pr* and did not have the heart to potentially discard my extra work. File in question pkg/db/localDataProxy.go