diff --git a/HISTORY.md b/HISTORY.md index 6aad96c..6e5a78f 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,6 +1,6 @@ -## Sa 15 August | v0.1.1 +## Sa 15 August | v0.1.2 -- `Logger.Child` accepts an `interface{}` instead of `string`. This way you can register children for pointers without forcing to naming them. If the key is string, then it's used as prefix (like always did). +- `Logger.Child` accepts an `interface{}` instead of `string`. This way you can register children for pointers without forcing to naming them. If the key is string or completes the `fmt.Stringer` interface, then it's used as prefix (like always did). ## Fr 14 August 2020 | v0.0.19 diff --git a/README.md b/README.md index 14b7626..b64da29 100644 --- a/README.md +++ b/README.md @@ -40,7 +40,7 @@ module your_project_name go 1.14 require ( - github.com/kataras/golog v0.1.1 + github.com/kataras/golog v0.1.2 ) ``` diff --git a/_examples/child/main.go b/_examples/child/main.go index a0acf2b..b2b9bb8 100644 --- a/_examples/child/main.go +++ b/_examples/child/main.go @@ -1,8 +1,6 @@ package main -import ( - "github.com/kataras/golog" -) +import "github.com/kataras/golog" func main() { @@ -15,16 +13,22 @@ func main() { var ( srvLogger = golog.Child("Server") app1Logger = srvLogger.Child("App1") + // Or use a pointer as child's key and append the prefix manually: app2 = newApp("App2") app2Logger = srvLogger.Child(app2). SetChildPrefix(app2.Name). SetLevel("debug") + + // Or use a pointer to a value which implements the fmt.Stringer: + app3 = newAppWithString("App3") + app3Logger = srvLogger.Child(app3) ) srvLogger.Infof("Hello Server") app1Logger.Infof("Hello App1") app2Logger.Debugf("Hello App2") + app3Logger.Warnf("Hello App3") } type app struct { @@ -34,3 +38,15 @@ type app struct { func newApp(name string) *app { return &app{Name: name} } + +type appWithString struct { + name string +} + +func newAppWithString(name string) *appWithString { + return &appWithString{name: name} +} + +func (app *appWithString) String() string { + return app.name +} diff --git a/doc.go b/doc.go index 45f76fe..95ba10c 100644 --- a/doc.go +++ b/doc.go @@ -36,7 +36,7 @@ Source code and other details for the project are available at GitHub: Current Version -0.0.19 +0.1.2 Installation @@ -390,4 +390,4 @@ Examples: package golog // import "github.com/kataras/golog" // Version is the version string representation of the "golog" package. -const Version = "0.1.1" +const Version = "0.1.2" diff --git a/logger.go b/logger.go index e64669d..cf341fe 100644 --- a/logger.go +++ b/logger.go @@ -526,9 +526,14 @@ func (m *loggerMap) getOrAdd(key interface{}, parent *Logger) *Logger { } logger = parent.Clone() - if prefix, ok := key.(string); ok { - logger.SetChildPrefix(prefix) + childPrefix := "" + switch v := key.(type) { + case string: + childPrefix = v + case fmt.Stringer: + childPrefix = v.String() } + logger.SetChildPrefix(childPrefix) m.mu.Lock() m.Items[key] = logger