Skip to content
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

Improve logging usage #496

Merged
merged 2 commits into from Oct 23, 2017
Merged

Improve logging usage #496

merged 2 commits into from Oct 23, 2017

Conversation

mz-pdm
Copy link
Contributor

@mz-pdm mz-pdm commented Oct 6, 2017

No description provided.

@kubevirt-bot
Copy link
Contributor

Can one of the admins verify this patch?

@petrkotas
Copy link
Contributor

petrkotas commented Oct 6, 2017

I will copy the commit message from first commit here, it describes the goals of this commit and it got lost by github.

Improve logging usage

The structured logger is good for producing comprehensive logs, however it is
not that good for being used directly in the code. Let's introduce some
changes that:

  • Make the code shorter, so it's easier to read.
  • Make the logging lines shorter, so the logging messages can be read without
    horizontal scrolling.
  • Reduce the chain of method calls so one can see easily also code other than
    logging in editors highlighting method calls.
  • Reduce code duplication.

To not make logging too messy, methods for setting message levels are turned
into terminal methods. Those methods are almost always used, so they can be
used as such rather than using extra Msg method call. In rare cases when we
don't want to use typical logging patterns, new Level method can be used.

It would be nice to access logging methods from objects being logged.
However due to Go limitations we can't do that easily, so we have to use
something like vm.Log().Info' rather than simply vm.Info'.

@petrkotas
Copy link
Contributor

I like this patch. The logging is now more readable and shorter to write.

@davidvossel
Copy link
Member

retest this please

Copy link
Member

@davidvossel davidvossel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall this looks fine to me. In the future, if we start using filtered loggers other than the DefaultLogger, we might want to re-evaluate some of the helper functions that abstract away that the DefaultLogger is being used, but for now I don't see any problems with the helper functions.

The only thing that caught my eye was the VM object's log function. I'm not sure that's a pattern we want to start using. I'd be okay with still having to specify Object() to avoid adding Log() functions for each cluster object we work with.

@@ -297,8 +323,8 @@ func TestObject(t *testing.T) {
setUp()
log := MakeLogger(MockLogger{})
log.SetLogLevel(DEBUG)
vm := v1.VirtualMachine{}
log.Object(&vm).Log("test", "message")
vm := newVM()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why change this?

@mz-pdm
Copy link
Contributor Author

mz-pdm commented Oct 9, 2017 via email

@davidvossel
Copy link
Member

Despite that I'm not particularly happy with it too, it lacks some
elegance. But it's IMO still better to use o.Log() than let's say
logging.Object(o), for the reasons mentioned in the commit message.

I agree that it o.Log() looks nicer than logging.Object(o). I don't feel good about making our logging package a dependency for things like /pkg/api/v1 now though. That's what we have to do in order to get this functionality. It's also why you had to work around the circular dependency.

@rmohr
Copy link
Member

rmohr commented Oct 10, 2017

I can understand the desire to make the logging lines a little bit shorter, however, I am also not very happy with the changes here.

We are using a structured logger, and I like that with the builder we have clearly structured way on what logs what. E.g. the .Object(vm), logs object information. If we later on also want to log the controller, we can simply do something like .Controller(migration).Object(vm), or .Object(vm).ManagedBy(migration). I also explicitly like the separation of .Reason() and .Msg(), because it also corresponds to what will be logged in which fields.

Some thoughts on how to make the lines a little bit shorter:

  • Having a the logging.DefaultLogger() is very long. Renaming logging to e.g. log and have a global Log variable which is set to logging. DefaultLogger().
  • Since V() only makes sense in combination with Info(), let's combine it

The outcome would be:

log.Log.Info().With("costm_key", "custom_val" ).Msg("whatever") # defaults to InfoV(1)
log.Log.InfoV(3).With("costm_key", "custom_val" ).Msg("whatever")
log.Log.Error().Reason(err).Msg("lala") # reason is not a must, but msg is.

It can be made even shorter by having a log.Error(), log.Info(), ... global method for the default logger in the log package.

If it is confusing for people, that .Msg() or .Msgf() are the the last ones executed, renaming it to .Log() or .Logf() would also be an option for me.

@mpolednik
Copy link
Contributor

Since V() only makes sense in combination with Info(), let's combine it

Please let's keep at least some consistency in the Go universe and keep V() a function.

As for the logging itself, if we really want structured logging (which I'm not fan of, not with the code burden that glog doesn't carry) AND a bit of consistency with kube*, I could imagine getting rid of message and making Info, Error, Warning etc. terminals in the builder. That would lead to logging lines such as

logging.DefaultLogger().Object(VM).Reason(Err).Error("VM isn't running")

The advantage of that is if you remove the structured part, you'll arrive to a point where the line looks similarly to other logging frameworks:

logging.DefaultLogger. ... .Error("VM isn't running")
---------------------/
imagine this being *log

@rmohr
Copy link
Member

rmohr commented Oct 10, 2017

logging.DefaultLogger().Object(VM).Reason(Err).Error("VM isn't running")

+1

As for the logging itself, if we really want structured logging (which I'm not fan of, not with the code burden that glog doesn't carry)

I really want to use structured logging.

@mz-pdm
Copy link
Contributor Author

mz-pdm commented Oct 10, 2017 via email

@mz-pdm
Copy link
Contributor Author

mz-pdm commented Oct 10, 2017 via email

@rmohr
Copy link
Member

rmohr commented Oct 10, 2017

  • Changing logging.DefaultLogger() to log.Log.

Works for me

  • Making Info(), Error(), etc. terminals.

Works for me

  • Nobody has protested against using additional arguments for formatted
    strings so I can think I can keep that too.

I would suggest Error() and Errorf() if that is what you mean.

@mz-pdm
Copy link
Contributor Author

mz-pdm commented Oct 12, 2017

I believe I made all the agreed changes in the updated version of the patch.

Copy link
Member

@davidvossel davidvossel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

works for me 👍

Copy link
Member

@rmohr rmohr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need a rebase, but looks good otherwise.

@davidvossel
Copy link
Member

It sounds like we're in agreement this should go in.

Since this touches so many files, we should merge this immediately once it is rebased.

The structured logger is good for producing comprehensive logs, however it is
not that good for being used directly in the code.  Let's introduce some
changes that:

- Make the code shorter, so it's easier to read.
- Make the logging lines shorter, so the logging messages can be read without
  horizontal scrolling.
- Reduce the chain of method calls so one can see easily also code other than
  logging in editors highlighting method calls.
- Reduce code duplication.

To not make logging too messy, methods for setting message levels are turned
into terminal methods (and their formatting argument variants are introduced).
Those methods are almost always used, so they can be used as such rather than
using extra Msg method call.  In rare cases when we don't want to use typical
logging patterns, new Level method can be used.

`logging' package is renamed to `log' and `Log' instance of DefaultLogger() is
created to make log lines significantly shorter.
@mz-pdm
Copy link
Contributor Author

mz-pdm commented Oct 23, 2017

Rebased and updated.

@rmohr
Copy link
Member

rmohr commented Oct 23, 2017

ok to test

@rmohr rmohr merged commit 8db0023 into kubevirt:master Oct 23, 2017
@rmohr
Copy link
Member

rmohr commented Oct 23, 2017

@mz-pdm thanks!

@rmohr rmohr mentioned this pull request Oct 23, 2017
kubevirt-bot pushed a commit to kubevirt-bot/kubevirt that referenced this pull request Nov 6, 2020
update kubernetes libraries 1.11.2 (same as KubeVirt)
kubevirt-bot pushed a commit to kubevirt-bot/kubevirt that referenced this pull request Dec 7, 2021
* Copy files over for 1.20

Signed-off-by: Daniel Hiller <dhiller@redhat.com>

* Change from experimental-kustomize to experimental-patches

kubeadm init --experimental-kustomize was deprecated in 1.20 therefore we
resort to using strategic patches.

Signed-off-by: Daniel Hiller <dhiller@redhat.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants