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

Add an source to all log statements #103

Merged
merged 1 commit into from
Feb 23, 2018
Merged

Conversation

markmandel
Copy link
Member

@markmandel markmandel commented Feb 22, 2018

This includes a logger as a data members for all objects, which sets a default "source" field on every log statement so that it's easy to track where log messages are coming from.

This is particularly useful as the overall controller for Agones gets more complicated, and we have many interacting controllers in a single binary.

Log statements now look like this:

{"level":"info","msg":"Wait for cache sync","source":"*gameservers.Controller","time":"2018-02-22T22:58:58Z"}
{"level":"info","msg":"Running","origin":"*gameservers.PortAllocator","time":"2018-02-22T22:58:58Z"}
{"level":"info","msg":"Flush cache sync, before syncing gameserver and node state","source":"*gameservers.PortAllocator","time":"2018-02-22T22:58:58Z"}
{"level":"info","msg":"Adding Node to port allocations","node":"agones","source":"*gameservers.PortAllocator","time":"2018-02-22T22:58:58Z"}
{"level":"info","msg":"Resetting Port Allocation","origin":"*gameservers.PortAllocator","time":"2018-02-22T22:58:58Z"}
{"level":"info","msg":"Starting workers...","source":"*gameservers.Controller","time":"2018-02-22T22:58:58Z"}
{"level":"info","msg":"Starting worker","source":"*gameservers.HealthController","time":"2018-02-22T22:58:58Z"}

So now you can see the context of the log statement through the "source" field.

Would love to hear your thoughts on if this is a good idea, if there is a better word than "origin", or anything else - @dzlier-gcp @enocom @Kuqd

@markmandel markmandel added the kind/cleanup Refactoring code, fixing up documentation, etc label Feb 22, 2018
@cyriltovena
Copy link
Collaborator

cyriltovena commented Feb 23, 2018

Source or Origin are good.
The idea is also good I wish it could pick up the filename automagically but its also simple enough.

👍

@markmandel
Copy link
Member Author

So pkg/errors tracks stracktraces when errors occurs - so we get filesnames and line numbers when an actual error occurs 👍 But the origin lets us know what is happening when things are going well, which is good.

Copy link
Contributor

@enocom enocom left a comment

Choose a reason for hiding this comment

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

There are a couple of small things worth considering. Otherwise, the consolidation of logrus use into the runtime package is very nice.

var (
log = runtime.NewLoggerWithOrigin("main")
)

func init() {
logrus.SetFormatter(&logrus.JSONFormatter{})
Copy link
Contributor

Choose a reason for hiding this comment

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

Now that the logging concerns are encapsulated within the runtime package, I think it would be nice to move this line into runtime as well. This means all uses of logrus are gathered in a single place.

@@ -46,6 +46,10 @@ const (
keyFileFlag = "key-file"
)

var (
log = runtime.NewLoggerWithOrigin("main")
Copy link
Contributor

Choose a reason for hiding this comment

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

Granted the log package isn't being used here, but to avoid an ambiguity logger might be better.

var (
log = runtime.NewLoggerWithOrigin("main")
)

func init() {
logrus.SetFormatter(&logrus.JSONFormatter{})
Copy link
Contributor

@enocom enocom Feb 23, 2018

Choose a reason for hiding this comment

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

See above about moving this line into the runtime package as well as renaming log to logger.

@markmandel
Copy link
Member Author

@Kuqd - I think I like source better. I'll make that change.

@enocom I like all the above as well, I'll also make those changes.

👍

This includes a `logger` as a data members for all objects,
which sets a default "source" field on every log statement
so that it's easy to track where log messages are coming from.

This is particularly useful as the controller for Agones
gets more complicated.
@markmandel markmandel changed the title Add an origin to all log statements Add an source to all log statements Feb 23, 2018
@markmandel
Copy link
Member Author

Changes made! PTAL!

Copy link
Contributor

@enocom enocom left a comment

Choose a reason for hiding this comment

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

LGTM

@enocom enocom merged commit 4e12060 into master Feb 23, 2018
@enocom enocom deleted the feature/logging-origin branch February 23, 2018 23:14
@markmandel markmandel added this to the 0.1 milestone May 19, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/cleanup Refactoring code, fixing up documentation, etc
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants