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

refactor: drop rawObj parameter from logger's methods #848

Merged
merged 1 commit into from
Nov 5, 2024

Conversation

pmalek
Copy link
Member

@pmalek pmalek commented Nov 5, 2024

What this PR does / why we need it:

This PR removes the rawObj parameter - which was unused anyway since #727 has been merged - from logger's methods.

1 important change here is that now, Info(), Debug(), Trace() and Error() accept only the (optional error for Error()) logger instance, message (string) and key value pairs as variadic parameter. The last part makes this error prone and susceptible for providing an invalid argument lists (i.e. odd number of arguments).

We could potentially alleviate this with making these functions accept a strongly typed:

type KV struct {
	K string
	V any
}

but then we'd need to allocate on each log call like so:

func Info(logger logr.Logger, msg string, kvs ... KV) {
	keyValues := make([]interface{}, 0, len(kvs)*2)
	for _, kv := range kvs {
		keyValues = append(keyValues, kv.K, kv.V)
	}
	_log(logger, logging.InfoLevel, msg, keyValues...)
}

Feel free to drop your comments on this performance vs type safety trade off.

Which issue this PR fixes

Fixes #

Special notes for your reviewer:

Code handling the development mode logger setting makes the dev mode logger not have several fields which might be beneficial for debugging/observability.

This makes the following difference between development mode enabled and disabled on logs:

  • development mode disabled
{
	"level": "debug",
	"ts": "2024-11-05T12:24:42.434+0100",
	"logger": "dataplane",
	"msg": "reconciliation complete for DataPlane resource",
	"controller": "dataplane",
	"controllerGroup": "gateway-operator.konghq.com",
	"controllerKind": "DataPlane",
	"DataPlane": {
		"name": "dataplane-example",
		"namespace": "default"
	},
	"namespace": "default",
	"name": "dataplane-example",
	"reconcileID": "2641ef78-cebe-4f69-a907-5e2cd84eb846"
}
  • development mode enabled
{
	"level": "\u001b[35mDEBUG\u001b[0m",
	"ts": "2024-11-05T12:31:13.633+0100",
	"logger": "DATAPLANE",
	"msg": "reconciliation complete for DataPlane resource"
}

we might want to remove that code to make those uniform.

PR Readiness Checklist:

Complete these before marking the PR as ready to review:

  • the CHANGELOG.md release notes have been updated to reflect significant changes

@pmalek pmalek self-assigned this Nov 5, 2024
@pmalek pmalek requested a review from a team as a code owner November 5, 2024 11:40
@pmalek pmalek mentioned this pull request Nov 5, 2024
Copy link
Contributor

@czeslavo czeslavo left a comment

Choose a reason for hiding this comment

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

Yeah, variadic key value pairs are hard to tackle without performance penalty... :/ We've lived with them up until now, so I guess we can leave it as is. I was looking for a linter that could potentially detect this, but I haven't found anything suitable.

@pmalek pmalek merged commit 52f9b09 into main Nov 5, 2024
21 checks passed
@pmalek pmalek deleted the logger-refactor branch November 5, 2024 14:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants