Skip to content

Commit

Permalink
update to v0.1.1
Browse files Browse the repository at this point in the history
  • Loading branch information
kataras committed Aug 15, 2020
1 parent ac78aec commit 64143aa
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 22 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ after_script:
# examples
- cd ./_examples
- go get ./...
- go test -v -cover ./...
- go test -v -race -cover ./...
- cd ../
# benchmarks
- cd ./_benchmarks
Expand Down
4 changes: 4 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## Sa 15 August | v0.1.1

- `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).

## Fr 14 August 2020 | v0.0.19

- Use locks on hijacker.
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ module your_project_name
go 1.14

require (
github.com/kataras/golog v0.0.19
github.com/kataras/golog v0.1.1
)
```

Expand Down
22 changes: 22 additions & 0 deletions _examples/child/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,26 @@ func main() {
golog.Child("Router").Warnf("Route %s already exists, skipping second registration", "/mypath")

golog.Error("Something went wrong!")

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")
)

srvLogger.Infof("Hello Server")
app1Logger.Infof("Hello App1")
app2Logger.Debugf("Hello App2")
}

type app struct {
Name string
}

func newApp(name string) *app {
return &app{Name: name}
}
2 changes: 1 addition & 1 deletion doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.0.19"
const Version = "0.1.1"
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
github.com/kataras/pio v0.0.8 h1:6pX6nHJk7DAV3x1dEimibQF2CmJLlo0jWVmM9yE9KY8=
github.com/kataras/pio v0.0.8/go.mod h1:NFfMp2kVP1rmV4N6gH6qgWpuoDKlrOeYi3VrAIWCGsE=
github.com/kataras/pio v0.0.10 h1:b0qtPUqOpM2O+bqa5wr2O6dN4cQNwSmFd6HQqgVae0g=
github.com/kataras/pio v0.0.10/go.mod h1:gS3ui9xSD+lAUpbYnjOGiQyY7sUMJO+EHpiRzhtZ5no=
18 changes: 15 additions & 3 deletions golog.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,9 +211,21 @@ func Scan(r io.Reader) (cancel func()) {
}

// Child (creates if not exists and) returns a new child
// Logger based on the default package-level logger instance.
// Logger based on the current logger's fields.
//
// Can be used to separate logs by category.
func Child(name string) *Logger {
return Default.Child(name)
// If the "key" is string then it's used as prefix,
// which is appended to the current prefix one.
func Child(key interface{}) *Logger {
return Default.Child(key)
}

// SetChildPrefix same as `SetPrefix` but it does NOT
// override the existing, instead the given "s"
// is appended to the current one. It's useful
// to chian loggers with their own names/prefixes.
// It does add the ": " in the end of "s" if it's missing.
// It returns itself.
func SetChildPrefix(s string) *Logger {
return Default.SetChildPrefix(s)
}
55 changes: 41 additions & 14 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"io"
"os"
"strings"
"sync"
"time"

Expand Down Expand Up @@ -496,44 +497,70 @@ func (l *Logger) Clone() *Logger {
}

// Child (creates if not exists and) returns a new child
// Logger based on the "l"'s fields.
// Logger based on the current logger's fields.
//
// Can be used to separate logs by category.
func (l *Logger) Child(name string) *Logger {
return l.children.getOrAdd(name, l)
// If the "key" is string then it's used as prefix,
// which is appended to the current prefix one.
func (l *Logger) Child(key interface{}) *Logger {
return l.children.getOrAdd(key, l)
}

type loggerMap struct {
mu sync.RWMutex
Items map[string]*Logger
Items map[interface{}]*Logger
}

func newLoggerMap() *loggerMap {
return &loggerMap{
Items: make(map[string]*Logger),
Items: make(map[interface{}]*Logger),
}
}

func (m *loggerMap) getOrAdd(name string, parent *Logger) *Logger {
func (m *loggerMap) getOrAdd(key interface{}, parent *Logger) *Logger {
m.mu.RLock()
logger, ok := m.Items[name]
logger, ok := m.Items[key]
m.mu.RUnlock()
if ok {
return logger
}

logger = parent.Clone()
prefix := name

// if prefix doesn't end with a whitespace, then add it here.
if lb := name[len(prefix)-1]; lb != ' ' {
prefix += ": "
if prefix, ok := key.(string); ok {
logger.SetChildPrefix(prefix)
}

logger.SetPrefix(prefix)
m.mu.Lock()
m.Items[name] = logger
m.Items[key] = logger
m.mu.Unlock()

return logger
}

// SetChildPrefix same as `SetPrefix` but it does NOT
// override the existing, instead the given "prefix"
// is appended to the current one. It's useful
// to chian loggers with their own names/prefixes.
// It does add the ": " in the end of "prefix" if it's missing.
// It returns itself.
func (l *Logger) SetChildPrefix(prefix string) *Logger {
if prefix == "" {
return l
}

// if prefix doesn't end with a whitespace, then add it here.
if !strings.HasSuffix(prefix, ": ") {
prefix += ": "
}

l.mu.Lock()
if l.Prefix != "" {
if !strings.HasSuffix(l.Prefix, " ") {
l.Prefix += " "
}
}
l.Prefix += prefix
l.mu.Unlock()

return l
}

0 comments on commit 64143aa

Please sign in to comment.