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

Decoupled logrus and added a logger interface #12

Merged
merged 2 commits into from
Nov 21, 2018
Merged

Conversation

owais
Copy link
Contributor

@owais owais commented Nov 21, 2018

Fixed #10

- `producer.Config` now takes a producer.Logger interface instead of
a Logrus instance.
- If not logger is provided to config, producer uses a standard library
logger by default (`producer.StandardLogger`)
- `loggers` sub package ships implementations for zap and logrus.

@owais
Copy link
Contributor Author

owais commented Nov 21, 2018

@a8m Added a logger interface and a couple of implementations (logrus and stdlib). Verified that both work. I think the lib should only log info and error messages and not try to deal with all possible "levels".

I'm not sure if the lib should include implementations for popular loggers such as logurs, zap, etc as it would pull in those dependencies whether the users need them or not. Probably they should be published as independent packages. Thoughts?

@owais owais force-pushed the decouple-logrus branch 4 times, most recently from 8ab4460 to de2fc25 Compare November 21, 2018 16:05
@owais owais changed the title Prototype implementation of decoupled logger Decoupled logrus and added a logger interface Nov 21, 2018
Copy link
Owner

@a8m a8m left a comment

Choose a reason for hiding this comment

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

Hey @owais, thanks so much for your contribution. I've added a few comments.
Another thing, imo it makes more sense to leave all logging code under the root (producer) package.
As a user of this package, I prefer to not import a generic/common package (e.g: logging) that will conflict with other packages.

standard.go Outdated Show resolved Hide resolved
logger.go Outdated
}

// Value represents a key:value pair used by the Logger interface
type Value struct {
Copy link
Owner

Choose a reason for hiding this comment

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

I would rename this struct into something like LogEntry, since it's a bit confusing that it's under producer namespace.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure. I moved it from the loggers package to top level but didn't rename,.

logger.go Outdated
// Logger represents a simple interface used by kinesis-producer to handle logging
type Logger interface {
Info(msg string, values ...Value)
Error(msg string, err error, values ...Value)
Copy link
Owner

@a8m a8m Nov 21, 2018

Choose a reason for hiding this comment

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

Suggested change
Error(msg string, err error, values ...Value)
Info(args ...interface{})
Error(args ...interface{})

What do you say about changing the interface signature to something like this? this will save the Value entry and the logrus/zap implementations.
You can even make it a field-logger by treat the args param as a list of key-value pairs.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I personally don't like it as almost all loggers at least expect a string so taking interface{} where you know you need a string doesn't sound too good to me. Also, I think having a struct to define a key value pair is better than an even number of args and then grouping them inside the function. I think it's cleaner for both the API and the internal implementation. I don't see much value in doing it but you are maintaining the code so I can do what makes your workflow better.

@owais
Copy link
Contributor Author

owais commented Nov 21, 2018

As a user of this package, I prefer to not import a generic/common package (e.g: logging) that will conflict with other packages.

The reason I put it inside a sub-package was to avoid making zap and logrus dependencies (in near future). I was thinking that producer/loggers can be a separate go package hosted in the same repo. Depending on kinesis-producer would not automatically install loggers meaning producer itself would not depend on logrus, zap and any other implementations people might contribute. I don't know how it would work exactly but I hope having go.mod at both the root and inside loggers should support this. (plan to add support for go.mod as well in the coming weeks).

@owais
Copy link
Contributor Author

owais commented Nov 21, 2018

That said, I can move it to the root package for now and we can move it back or out of the repo if needed/wanted in future.

- `producer.Config` now takes a producer.Logger interface instead of
a Logrus instance.
- If not logger is provided to config, producer uses a standard library
logger by default (`producer.StandardLogger`)
- `loggers` sub package ships implementations for zap and logrus.
@owais
Copy link
Contributor Author

owais commented Nov 21, 2018

@a8m Incorporated some suggestions. Let me know your decision on root vs sub-package for logger implementations. I personally think it should actually be like the following but am open to moving everything to root'

root/
  - library_code 
  - loggers/
      - interface_and_stock_logger.go
      - logrus/
         - logrus.go
      - zap/
         - zap.go

@a8m
Copy link
Owner

a8m commented Nov 21, 2018

The reason I put it inside a sub-package was to avoid making zap and logrus dependencies (in near future).

I agree with this, and that's why I suggested creating an interface that common log packages support by default, and we should add only the default implementation, which is the std-log.

About directories structure, my only problem with this is that I don't want to create a package with common name, or to conflict with logrus or zap, so people will need to alias it to something else.

@owais
Copy link
Contributor Author

owais commented Nov 21, 2018

OK, makes sense. In that case, we could use a less common but somewhat ugly names. Something like:

root/
  - library_code 
  - interface_and_stock_logger.go
  - go.mod (eventually)
  - loggers/
      - kpLogrus/
         - logrus.go
         - go.mod (eventually)
      - kpZap/
         - zap.go
         - go.mod (eventually)

@a8m
Copy link
Owner

a8m commented Nov 21, 2018

Go's packages are usually lowercased with no underscore or mixedCaps (ref). But yeah, I agree, pkzap looks a bit hacky, but I don't have a better suggestion...

@a8m a8m merged commit 2b76ac6 into a8m:master Nov 21, 2018
@a8m
Copy link
Owner

a8m commented Nov 21, 2018

Thanks for the contribution and your patient @owais.

@owais owais deleted the decouple-logrus branch November 21, 2018 22:05
@zhouzhuojie zhouzhuojie mentioned this pull request Feb 12, 2019
8 tasks
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