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

DOCS-1827: Update Go module logging examples #2632

Merged
merged 3 commits into from
Mar 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions docs/registry/create/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ func (b *myBase) Reconfigure(ctx context.Context, deps resource.Dependencies, co

geometries, err := kinematicbase.CollisionGeometry(conf.Frame)
if err != nil {
b.logger.Warnf("base %v %s", b.Name(), err.Error())
b.logger.CWarnf(ctx, "base %v %s", b.Name(), err.Error())
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is there a reason why these methods are named with a C at the beginning? It feels rather unintuitive

Copy link
Member

Choose a reason for hiding this comment

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

It was a decision the NetCode team made a while ago. Most loggers have methods that look like Debug, Debugf, and Debugw for all log levels. Most loggers also do not take a context.Context. We wanted to take a context on our logging methods to pass some "extra" information along with each log that could help with debugging the path a single operation was taking through the RDK. When an alternative version of a method that is otherwise identical to the original but offers a context.Context param is written in Golang, it's common practice to name that method either FooCtx of CFoo. We went with the latter.

}
b.geometries = geometries

Expand Down Expand Up @@ -497,7 +497,7 @@ func (b *myBase) SetVelocity(ctx context.Context, linear, angular r3.Vector, ext

// SetPower computes relative power between the wheels and sets power for both motors.
func (b *myBase) SetPower(ctx context.Context, linear, angular r3.Vector, extra map[string]interface{}) error {
b.logger.Debugf("SetPower Linear: %.2f Angular: %.2f", linear.Y, angular.Z)
b.logger.CDebugf(ctx, "SetPower Linear: %.2f Angular: %.2f", linear.Y, angular.Z)
if math.Abs(linear.Y) < 0.01 && math.Abs(angular.Z) < 0.01 {
return b.Stop(ctx, extra)
}
Expand All @@ -509,7 +509,7 @@ func (b *myBase) SetPower(ctx context.Context, linear, angular r3.Vector, extra

// Stop halts motion.
func (b *myBase) Stop(ctx context.Context, extra map[string]interface{}) error {
b.logger.Debug("Stop")
b.logger.CDebug(ctx, "Stop")
err1 := b.left.Stop(ctx, extra)
err2 := b.right.Stop(ctx, extra)
return multierr.Combine(err1, err2)
Expand Down Expand Up @@ -1060,13 +1060,15 @@ func init() {
resource.RegisterComponent(...)
}
// Finally, when you need to log, use the functions on your component's logger:
fn (c *component) someFunction(a int) {
fn (c *component) someFunction(ctx context.Context, a int) {
// Log with severity info:
c.logger.Infof("performing some function with a=%v",a)
c.logger.CInfof(ctx, "performing some function with a=%v", a)
// Log with severity debug (using value wrapping):
c.logger.Debugw("performing some function","a",a)
c.logger.CDebugw(ctx, "performing some function", "a" ,a)
// Log with severity warn:
c.logger.CWarnw(ctx, "encountered warning for component", "name", c.Name())
// Log with severity error without a parameter:
c.logger.Errorln("performing some function")
c.logger.CError(ctx, "encountered an error")
}
```

Expand Down
Loading