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

SNOW-625282: Make logrus dependency optional or drop it all together #1286

Open
wants to merge 17 commits into
base: master
Choose a base branch
from

Conversation

rickr-sigma
Copy link

Description

The following changes decouple logrs from SFLogger, with the eventual goal to allow per context logging control (in a followup PR) by a user provided SFLogger.

This PR is not (yet) complete but is presented as a proof-of-concept. Further cleanup will be performed if this approach is deemed acceptable. One thing to be aware of is that certain interfaces are no longer supported in particular the functional variants of logging, and the WithTime, WithFields, WithError, Panic and Fatal which were all ONLY being exercised in unit tests.

Checklist

  • Created tests which fail without the change (if possible)
  • Extended the README / documentation, if necessary

@rickr-sigma rickr-sigma requested a review from a team as a code owner January 6, 2025 20:08
Copy link

github-actions bot commented Jan 6, 2025

All contributors have signed the CLA ✍️ ✅
Posted by the CLA Assistant Lite bot.

driver_test.go Outdated Show resolved Hide resolved
easy_logging_test.go Outdated Show resolved Hide resolved
log.go Outdated Show resolved Hide resolved
log.go Outdated
Comment on lines 284 to 288
func (log *defaultLogger) TraceFn(fn rlog.LogFunction) {
if log.enabled {
log.inner.TraceFn(fn)
}
}
Copy link
Author

Choose a reason for hiding this comment

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

all Fn variants removed; again unused in prod code.

log.go Outdated
Comment on lines 410 to 419
// AddHook adds a hook to the logger hooks.
func (log *defaultLogger) AddHook(hook rlog.Hook) {
log.inner.AddHook(hook)

}

// IsLevelEnabled checks if the log level of the logger is greater than the level param
func (log *defaultLogger) IsLevelEnabled(level rlog.Level) bool {
return log.inner.IsLevelEnabled(level)
}
Copy link
Author

Choose a reason for hiding this comment

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

unused.

log.go Outdated
Comment on lines 422 to 434
func (log *defaultLogger) SetFormatter(formatter rlog.Formatter) {
log.inner.SetFormatter(formatter)
}

// SetOutput sets the logger output.
func (log *defaultLogger) SetOutput(output io.Writer) {
log.inner.SetOutput(output)
}

func (log *defaultLogger) SetReportCaller(reportCaller bool) {
log.inner.SetReportCaller(reportCaller)
}

Copy link
Author

@rickr-sigma rickr-sigma Jan 6, 2025

Choose a reason for hiding this comment

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

SetFormatter / SetReportCaller only have 1 internal usage and do not need to be part of the interface.

@rickr-sigma
Copy link
Author

I have read the CLA Document and I hereby sign the CLA

ctx_test.go Outdated Show resolved Hide resolved
driver_test.go Outdated Show resolved Hide resolved
log.go Outdated Show resolved Hide resolved
log.go Show resolved Hide resolved
log.go Show resolved Hide resolved
log.go Show resolved Hide resolved
SetLogLevel(level string) error
GetLogLevel() string
WithContext(ctx context.Context) *rlog.Entry
WithContext(ctx ...context.Context) LogEntry
Copy link
Collaborator

Choose a reason for hiding this comment

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

It is backward compatible, isn't it? Previously this function returned a struct, now it returns the interface which is implemented by logrus struct, right?

Copy link
Author

Choose a reason for hiding this comment

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

This is probably the biggest change. Every field in the rlog.Entry struct is exported and modifiable. With a LogEntry you can only emit a log line via trace/debug/info/... .

The rationale behind this change is that ultimately we want to be able to have callers of the driver be able to have full control over the logger and this is only possible when interfaces are exposed, or at the very least its a lot easier.

If we're still looking for a means of exposing the underlying logrus Entry we could ensure that a cast from LogEntry works. I'll wire up a unit test to make sure this is straightforward enough. It doesn't provide 100% code compatibility, but at least it provides an out for anyone that has directly manipulated the logrus Entries.

Copy link
Author

Choose a reason for hiding this comment

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

Added ConvertibleEntry interface so that callers can convert the underlying LogEntry into a logrus Entry struct. Test case TestLogEntryConversion() shows how to do this.

@rickr-sigma
Copy link
Author

@sfc-gh-pfus please have another look, as this version now maximizes backwards compatibility.

The breaking change is now narrowed down to WithContext/WithField/WithFields/WithError which now return LogEntry rather than the underlying logrus Entry struct.

The function ToEntry() on the ConvertibleEntry interface can be used to obtain the wrapped logrus Entry struct from a LogEntry but it requires a change on the caller side (i.e. 1 additional method call). See TestLogEntryConversion for an example.

@@ -40,12 +40,59 @@ func RegisterLogContextHook(contextKey string, ctxExtractor ClientLogContextHook
// logger.WithContext is used
var LogKeys = [...]contextKey{SFSessionIDKey, SFSessionUserKey}

// SFLogger Snowflake logger interface to expose FieldLogger defined in logrus
// Fields
Copy link
Collaborator

Choose a reason for hiding this comment

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

Any comment? Why do we need another public var?

Copy link
Author

Choose a reason for hiding this comment

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

LogKeys ? It was added in 22Dec20 looks like a convenience for the iteration. At the very least we could make it package local or even fold it into the context2Fields() func, which contains the only use.

Copy link
Collaborator

Choose a reason for hiding this comment

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

No, I meant Fields :)

Copy link
Author

Choose a reason for hiding this comment

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

the Fields type matches the definition of logrus Fields and exists for backward compatibility. I.e. the existing withXXX methods on FieldLogger are;

	WithField(key string, value interface{}) *Entry
	WithFields(fields Fields) *Entry
	WithError(err error) *Entry

so keeping the interfaces as close as possible we have SFLogger containing;

	WithField(key string, value interface{}) LogEntry
	WithFields(fields Fields) LogEntry
	WithError(err error) LogEntry

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ok, so just leave a comment above the var :) Also, if we want to release it as a non compatible change, we don't have to write in a comment that it is a backward compatibility to logrus.

Regardless the variable itself, I see two use cases.

  1. Global map with fields actually global, like instance name/host, OS or app version - that is fine.
  2. WithFields used to propagate fields to the actual log entry. I can't see a good examples, but my intuition says that in some cases it may be helpful, so probably also good to have it.

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.

2 participants