Skip to content

Offer the ability to pass through standard capnslog when embedding. #53

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

Merged
merged 2 commits into from
Apr 18, 2016

Conversation

purpleidea
Copy link
Contributor

This resolves: etcd-io/etcd#4115

@purpleidea
Copy link
Contributor Author

Here is one patch I've made to work around etcd-io/etcd#4115 It doesn't affect any of the capnslog functionality anywhere else as it's only an additional formatter which nobody has to use. I think it could be useful for embedding etcd.

It's a bit of a hack, but it helps for now. You can use it with:

capnslog.SetFormatter(capnslog.NewSimpleFormatter(os.Stderr))

@purpleidea
Copy link
Contributor Author

FWIW I added a second patch with an even more elegant formatter for use in embedding etcd. Cheers

@@ -28,6 +29,28 @@ type Formatter interface {
Flush()
}

func NewSimpleFormatter(w io.Writer) *SimpleFormatter {
return &SimpleFormatter{
w: bufio.NewWriter(w),
Copy link
Contributor

Choose a reason for hiding this comment

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

Totally unused?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's unused in the lib, but useful to someone consuming it.

Copy link
Contributor

Choose a reason for hiding this comment

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

It's not exported, so no-one can consume it :-)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Why isn't it exported??

Copy link
Contributor

Choose a reason for hiding this comment

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

In Go, a name is exported if it begins with a capital letter.
When importing a package, you can refer only to its exported names. Any "unexported" names are not accessible from outside the package.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This I know, but either I misunderstand where the git comments are pointing to, or I don't see the lack of capitalization... Are we talking about: NewSimpleFormatter ? It looks capitalized to me.

Copy link
Contributor

Choose a reason for hiding this comment

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

Talking about the field w on the SimpleFormatter struct - e.g. this won't work:

import "cpnslog"
f := capnslog.NewSimpleFormatter(bytes.Buffer{})
fmt.Println(f.w) // can't access w from outside package!

Copy link
Contributor

Choose a reason for hiding this comment

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

and even if it IS exported I don't see what it provides to consumers..

@purpleidea purpleidea force-pushed the feat/simple-formatter branch from 675a83a to 9aa8a60 Compare April 5, 2016 18:49
@purpleidea
Copy link
Contributor Author

@jonboulle I believe I fixed all the issues, rebased and pushed them back. Annoying that GH looses context on old comments when you force push :P

}

// Format builds a log message for the LogFormatter. The LogLevel is ignored.
func (s *LogFormatter) Format(pkg string, l LogLevel, i int, entries ...interface{}) {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit, but can you change the receiver name to "l" or "f" or "lf" instead of "s"? here and for Flush()

Copy link
Contributor

Choose a reason for hiding this comment

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

Also consider (pkg string, _ logLevel, i int, entries ...interface{})

@jonboulle
Copy link
Contributor

Few final comments. Could you please also tweak the first line of your commit message?
capnslog: add formatter to simulate normal log package behaviour

or similar. Thanks!

This adds a more clever formatter that uses the logger call depth to
return accurate data about the logged message in question.

It can be called with:

capnslog.SetFormatter(
	capnslog.NewLogFormatter(os.Stderr, "<prefix> ", flags)
)
@purpleidea purpleidea force-pushed the feat/simple-formatter branch from e5093b5 to 68c9954 Compare April 18, 2016 14:09
@purpleidea
Copy link
Contributor Author

@jonboulle This was actually a really excellent review (even the nitpick) thanks! Can I get you as a reviewer on https://github.com/purpleidea/mgmt/ ?

I've rebased everything, and also fixed the StringFormatter return value in a separate patch. LMK.

@jonboulle
Copy link
Contributor

@purpleidea you can try CCing me on some reviews, but I can't promise I'll get to anything in a timely manner :-) (note the two week gap on this one).

Thanks for your patience - this LGTM.

@jonboulle jonboulle merged commit 057ebef into coreos:master Apr 18, 2016
@purpleidea
Copy link
Contributor Author

woo!

Mgmt will get more interesting as the etcd parts get more cleaned up and merged :)

Cheers

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.

importing etcdserver messes up log object
2 participants