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

feat: structured logs with uber/zap #848

Closed
wants to merge 2 commits into from
Closed

feat: structured logs with uber/zap #848

wants to merge 2 commits into from

Conversation

iwpnd
Copy link
Member

@iwpnd iwpnd commented May 7, 2022

Hi 👋

As discussed in #831 I added the option to use structured logs with uber/zap while falling back to the currently implemented default if --logger is not specifically used. Log levels conform to the current implementation with the exception of log level TRACE that is not supported by zap. I use Debug as a fall back here.

Looking forward for your feedback

@iwpnd iwpnd requested review from gdey and ARolek as code owners May 7, 2022 13:46
@coveralls
Copy link

coveralls commented May 7, 2022

Pull Request Test Coverage Report for Build 3f60b86ad-PR-848

  • 7 of 73 (9.59%) changed or added relevant lines in 3 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage decreased (-0.2%) to 45.32%

Changes Missing Coverage Covered Lines Changed/Added Lines %
internal/log/log.go 5 10 50.0%
cmd/tegola/cmd/root.go 2 18 11.11%
internal/log/zap.go 0 45 0.0%
Totals Coverage Status
Change from base Build 7ac37b942: -0.2%
Covered Lines: 5583
Relevant Lines: 12319

💛 - Coveralls

fix: init log level

chore: remove coverage.out :s
Copy link
Member

@ARolek ARolek left a comment

Choose a reason for hiding this comment

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

Looking good in here. I have a few minor comments inline. The main thing I want to think through here is, do we really want to support 2 types of loggers? I guess that might be nice for a transition period, but it seems like we should maybe rally around a single logging implementation. Maybe we deprecate the old one in favor of the new?

What do you think @gdey?

internal/log/log.go Outdated Show resolved Hide resolved
// SetOutput...
// To satisfy log.Interface
func (z *ZapLogger) SetOutput(io.Writer) {
return
Copy link
Member

Choose a reason for hiding this comment

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

Interesting. Does zap not support changing the output location?

Copy link
Member Author

@iwpnd iwpnd May 11, 2022

Choose a reason for hiding this comment

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

You can set the output location within the zap config before instantiating the logger. See here.

Something like:

zapconfig.OutputPaths = []string{
    "/var/log/myproject/myproject.log",
  }

would also be possible here.

Copy link
Member

Choose a reason for hiding this comment

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

I think we should drop SetOutput from the Interface{}, we don't have any calls to it, and having the output set during the configuration of the logger makes the most sense.

return fmt.Errorf("invalid logger %s", logger)
}
}

func getLogLevelFromString(level string) (log.Level, error) {
switch level {
case "TRACE":
Copy link
Member

Choose a reason for hiding this comment

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

Do we have some consts we can key off of for these log levels?

Copy link
Member Author

Choose a reason for hiding this comment

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

Not sure if I understand your right here. Log levels are numbers ranging from -2 to 3 e.g. log.TRACE equals -2 and we have to derive this from the users input string.
We could add additional consts like TRACE_STRING for that switch statement I guess?

@iwpnd
Copy link
Member Author

iwpnd commented May 11, 2022

The main thing I want to think through here is, do we really want to support 2 types of loggers?

While zap is fast, text logs have the edge in terms of performance because json serialization has an overhead. Giving the user the option is only fair as not every users wants to reap the benefits of json logs, or doesn't even care at all.

But ultimately that's your decision to make of course. :)

@ARolek ARolek changed the base branch from v0.15.x to master May 18, 2022 17:39
@iwpnd
Copy link
Member Author

iwpnd commented Jun 7, 2022

Do you want to move forward with this or drop @ARolek @gdey ?

@ARolek
Copy link
Member

ARolek commented Jun 8, 2022

@iwpnd we should move forward with it! I know others have asked about this as well. Let me bump @gdey so he can provide some input as well.

Copy link
Member

@gdey gdey left a comment

Choose a reason for hiding this comment

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

I think we should drop the SetOutput from the log.Interface

@@ -46,6 +58,8 @@ func init() {
"path or http url to a config file, or \"-\" for stdin")
RootCmd.PersistentFlags().StringVar(&logLevel, "log-level", "INFO",
"work in progress - set log level to: TRACE, DEBUG, INFO, WARN or ERROR")
RootCmd.PersistentFlags().StringVar(&logger, "logger", "standard",
Copy link
Member

Choose a reason for hiding this comment

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

minor nit, might want to use the log.STANDARD constant here.

// SetOutput...
// To satisfy log.Interface
func (z *ZapLogger) SetOutput(io.Writer) {
return
Copy link
Member

Choose a reason for hiding this comment

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

I think we should drop SetOutput from the Interface{}, we don't have any calls to it, and having the output set during the configuration of the logger makes the most sense.

This pull request was closed.
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

4 participants