-
Notifications
You must be signed in to change notification settings - Fork 19
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
Logging #47
Logging #47
Conversation
autopilot/autopilot.go
Outdated
err = logger.Sync() | ||
closeFn() | ||
return | ||
} |
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.
this is a siad/skyd pattern that I am trying to avoid replicating. 😅
The rule of thumb is: Don't call constructors within a constructor. Doing so is convenient for the caller (since they can pass one dir
argument instead of a bunch of interfaces), but it has three big downsides:
- It prevents you from using interfaces. In this case, we are stuck with a logger that writes to a file, even during testing.
- It forces the constructed object to be responsible for cleaning up its dependencies. (Should an autopilot really be responsible for closing a logger?)
- It prevents you from sharing a dependency. What if we want all of our subsystems to share a logger?
There are exceptions, of course. But to me, this is a pretty clear example of where we should be passing a Logger
interface instead of calling a constructor within a constructor.
I think the constructor-within-a-constructor pattern derives from a more general design philosophy of building systems out of "actors," by which I mean "black boxes that have a lot of state and make a lot of decisions." I'm moving away from that philosophy, and towards systems built from "dumb" components, which have less state and make fewer decisions. Essentially, the goal is to centralize the complexity of the system by moving it out of the individual components.
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.
That makes sense 👍 - couldn't pass an interface because the guys at Uber decided to remove the interface and make it a struct. Should be fine though, it actually cleaned up the cleanup function, thanks!
This PR adds structured logging to the autopilot. I thought of using logrus but apparently that's in maintenance mode. Seems like zap is the new go-to for structured logging and it's super fast.
The idea is to use
named
loggers that add a "component" context to the log entries. Structured logging is definitely a bonus since we'll be able to easily parse relevant info for a single host or subsystem. I played around with the config a little bit but kept it simple for now. There's also azaptest
package that allows creating a test logger to which you can passtesting.T
, which is pretty nice because you get logs only in case of failure or when you run it with-v
. Removed that though for the time being.