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 a LoggerWithMaxLevel interface #39

Merged
merged 3 commits into from
Jul 15, 2022
Merged

Conversation

thallgren
Copy link
Contributor

This interface is meant to be used by Logger implementations that
will discard messages above a certain log level. A prominent example
is the current logrus wrapper.

The interface is motivated by the need to sometimes skip resource
consuming string formatting for messages that the logger will discard.
Example:

if dlog.MaxLogLevel(ctx) >= dlog.LogLevelTrace {
  // use a string buffer to build a trace message from some network
  // package, then log it with
  dlog.Trace(ctx, msg)
}

The max-level can also be usedful when an alternative logger must
be created for some third party module that implements its own logger
API.

Improves performance by using a fixed array of log levels instead of
a map to avoid a hash lookup on each log call.
This interface is meant to be used by `Logger` implementations that
will discard messages above a certain log level. A prominent example
is the current logrus wrapper.

The interface is motivated by the need to sometimes skip resource
consuming string formatting for messages that the logger will discard.
Example:

    if dlog.MaxLogLevel(ctx) >= dlog.LogLevelTrace {
      // use a string buffer to build a trace message from some network
      // package, then log it with
      dlog.Trace(ctx, msg)
    }

The max-level can also be usedful when an alternative logger must
be created for some third party module that implements its own logger
API.
@thallgren thallgren requested a review from LukeShu July 4, 2022 11:01
dlog/logger.go Outdated
@@ -84,6 +84,19 @@ type OptimizedLogger interface {
UnformattedLogf(level LogLevel, format string, args ...interface{})
}

// LoggerWithMaxLevel can be implemented by loggers that define a maximum
// level that will be logged, i.e. if a logger defines a max-level of
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: "i.e." is used where you might say "in other words"; "e.g." is for where you might say "for example".

dlog/logger.go Outdated
// LoggerWithMaxLevel can be implemented by loggers that define a maximum
// level that will be logged, i.e. if a logger defines a max-level of
// LogLevelInfo, then only LogLevelError, LogLevelWarn, and LogLevelInfo will
// be logged, and LogLevelDebug and LogLevelTrace will be discarded.
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: since this comma is delimiting a set of comma-separated lists (LogLevelError, LogLevelWarn, and LogLevelInfo vs LogLevelDebug and LogLevelTrace), it should be a semicolon to indicate that it is higher precedence than the other commas.

type LoggerWithMaxLevel interface {
// MaxLevel return the maximum loglevel that will be logged
MaxLevel() LogLevel
}
Copy link
Contributor

Choose a reason for hiding this comment

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

My only suggestion is that I'd consider including this in the OptimizedLogger interface rather than creating a separate interface. I'm not sure I would, but I'd consider it more deeply; and then include in the doc comment the reasoning for landing whichever way I did.

Copy link
Contributor

Choose a reason for hiding this comment

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

(in other words: please document your rationale)

Copy link
Contributor

@LukeShu LukeShu left a comment

Choose a reason for hiding this comment

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

Looks good! My only comments are about documentation; I'm 100% satisfied with the implementation.

- Use semicolon to show comma priority
- Use e.g. instead of i.e.
- Add motivation for the extra interface to docs comment.
@thallgren thallgren merged commit b09ab2e into master Jul 15, 2022
@thallgren thallgren deleted the thallgren/max-log-level branch July 15, 2022 02:25
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