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

Move ConfigureLoggers to the Context struct. #36

Merged
merged 1 commit into from
May 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,3 +205,21 @@ func (c *Context) ResetWriters() {
defer c.writersMutex.Unlock()
c.writers = make(map[string]Writer)
}

// ConfigureLoggers configures loggers according to the given string
// specification, which specifies a set of modules and their associated
// logging levels. Loggers are colon- or semicolon-separated; each
// module is specified as <modulename>=<level>. White space outside of
// module names and levels is ignored. The root module is specified
// with the name "<root>".
//
// An example specification:
// `<root>=ERROR; foo.bar=WARNING`
func (c *Context) ConfigureLoggers(specification string) error {
config, err := ParseConfigString(specification)
if err != nil {
return err
}
c.ApplyConfig(config)
return nil
}
10 changes: 9 additions & 1 deletion context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func (*ContextSuite) TestNewContextRootLevel(c *gc.C) {
level: loggo.Level(42),
expected: loggo.WARNING,
}} {
c.Log("%d: %s", i, test.level)
c.Logf("%d: %s", i, test.level)
context := loggo.NewContext(test.level)
cfg := context.Config()
c.Check(cfg, gc.HasLen, 1)
Expand Down Expand Up @@ -152,6 +152,14 @@ func (*ContextSuite) TestNewLoggerAddsConfig(c *gc.C) {
})
}

func (*ContextSuite) TestConfigureLoggers(c *gc.C) {
context := loggo.NewContext(loggo.INFO)
err := context.ConfigureLoggers("testing.module=debug")
c.Assert(err, gc.IsNil)
expected := "<root>=INFO;testing.module=DEBUG"
c.Assert(context.Config().String(), gc.Equals, expected)
Copy link
Member

Choose a reason for hiding this comment

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

should you be testing that the configured levels actually mean something (eg, debug messages are included for testing.module but not for a different logger?)
I'm slightly worried about user confusion (understanding that each context has its own logger config, etc.) but it does fit the issue we are trying to address (have explicitly different logging config for each model).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There are other tests that test that the configured loggers output the right levels, so I don't think it is necessary here.

Yes, I think there is the potential for confusion. The docs are pretty clear though.

}

func (*ContextSuite) TestApplyNilConfig(c *gc.C) {
context := loggo.NewContext(loggo.DEBUG)
context.ApplyConfig(nil)
Expand Down
21 changes: 8 additions & 13 deletions global.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,20 +66,15 @@ func RemoveWriter(name string) (Writer, error) {
return defaultContext.RemoveWriter(name)
}

// ConfigureLoggers configures loggers according to the given string
// specification, which specifies a set of modules and their associated
// logging levels. Loggers are colon- or semicolon-separated; each
// module is specified as <modulename>=<level>. White space outside of
// module names and levels is ignored. The root module is specified
// with the name "<root>".
// ConfigureLoggers configures loggers on the default context according to the
// given string specification, which specifies a set of modules and their
// associated logging levels. Loggers are colon- or semicolon-separated; each
// module is specified as <modulename>=<level>. White space outside of module
// names and levels is ignored. The root module is specified with the name
// "<root>".
//
// An example specification:
// `<root>=ERROR; foo.bar=WARNING`
// `<root>=ERROR; foo.bar=WARNING`
func ConfigureLoggers(specification string) error {
config, err := ParseConfigString(specification)
if err != nil {
return err
}
defaultContext.ApplyConfig(config)
return nil
return defaultContext.ConfigureLoggers(specification)
}