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 definition to log.Print() func #293

Merged
merged 7 commits into from
Sep 20, 2019
Merged

Add definition to log.Print() func #293

merged 7 commits into from
Sep 20, 2019

Conversation

SupriyaKasten
Copy link
Contributor

Change Overview

Function definition for log.Print() func

Pull request type

Please check the type of change your PR introduces:

  • Work in Progress
  • Refactoring (no functional changes, no api changes)
  • Trivial/Minor
  • Bugfix
  • Feature
  • Documentation

Test Plan

  • Manual
  • Unit test
  • E2E

Copy link
Contributor

@tdmanv tdmanv left a comment

Choose a reason for hiding this comment

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

Should we just always set the entry? it seems easier than handling it everywhere

pkg/log/log.go Outdated
func (l *logger) Print(msg string, fields field.Fields) {
logFields := make(logrus.Fields)
if l.ctx != nil {
ctxFields := field.FromContext(l.ctx)
Copy link
Contributor

Choose a reason for hiding this comment

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

does this handle nil? Can we make it handle nil?

pkg/log/log.go Outdated
@@ -46,23 +48,62 @@ func WithContext(ctx context.Context) {
Info().WithContext(ctx)
}

func (l *logger) Print(msg string) {
func (l *logger) Print(msg string, fields field.Fields) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Please decouple accepting fields as a param and extracting the fields from the context.

Copy link
Contributor

Choose a reason for hiding this comment

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

For now, let's have the Print signature be:

Suggested change
func (l *logger) Print(msg string, fields field.Fields) {
func (l *logger) Print(msg string) {

The fields argument is not immediately needed, right? They will be extracted from the context, no?
We can easily extend the Print function later to accept other arguments. However, we have not gone through the exercise of evaluating the tradeoffs of different alternatives.
We decided to use WithContext() and WithError() for now as a mean to achieve the immediate desired functionality.

func (l *logger) Print(msg string) {
logFields := make(logrus.Fields)
if ctxFields := field.FromContext(l.ctx); ctxFields != nil {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
if ctxFields := field.FromContext(l.ctx); ctxFields != nil {
if ctxFields := field.FromContext(l.ctx); len(ctxFields) != 0 {

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@tdmanv ctxFields is of type field.Fieldser interface. I think len(ctxFields) will fail.

Copy link
Contributor

Choose a reason for hiding this comment

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

you're right! please ignore this comment

@mergify mergify bot merged commit d98c912 into master Sep 20, 2019
@@ -46,23 +48,50 @@ func WithContext(ctx context.Context) {
Info().WithContext(ctx)
}

func WithError(err error) {
Error().WithError(err)
Copy link
Contributor

Choose a reason for hiding this comment

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

Default to INFO for now.
We should have a discussion about whether the ERROR level is even needed in the context of (micro-)services.
The general guideline is that we should not use the ERROR level and the package implementation should discourage it and eventually not allow it.

}
}

func (l *logger) WithContext(ctx context.Context) Logger {
l.ctx = ctx
if l.entry != nil {
Copy link
Contributor

Choose a reason for hiding this comment

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

Later we can figure out an idiomatic way of avoiding having to always check whether entry is nil or not.
And in particular, it is desirable to make "logger" immutable. That's mostly implementation and would be compatible with the API currently exposed.

@julio-lopez julio-lopez deleted the log-print-func branch September 20, 2019 23:02
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.

None yet

3 participants