Skip to content

Commit

Permalink
Merge branch 'master' into v2
Browse files Browse the repository at this point in the history
  • Loading branch information
inconshreveable committed Dec 6, 2014
2 parents d56b9c8 + 9d1cc30 commit 7cf5571
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 27 deletions.
48 changes: 22 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
![obligatory xkcd](http://imgs.xkcd.com/comics/standards.png)

# log15
# log15 [![godoc reference](https://godoc.org/gopkg.in/inconshreveable/log15.v2?status.png)](https://godoc.org/gopkg.in/inconshreveable/log15.v2)

Package log15 provides an opinionated, simple toolkit for best-practice logging that is both human and machine readable. It is modeled after the standard library's io and net/http packages.

Expand All @@ -14,49 +14,45 @@ Package log15 provides an opinionated, simple toolkit for best-practice logging
- Built-in support for logging to files, streams, syslog, and the network
- Support for forking records to multiple handlers, buffering records for output, failing over from failed handler writes, + more

## Documentation

The package documentation is extensive and complete. Browse on godoc:

#### [log15 API Documentation](https://godoc.org/gopkg.in/inconshreveable/log15.v2)

## Versioning
The API of the master branch of log15 should always be considered unstable. Using a stable version
of the log15 package is supported by gopkg.in. Include your dependency like so:

import log "gopkg.in/inconshreveable/log15.v2"

You can also vendor log15 with a tool like Godep.

## Examples

// all loggers can have key/value context
srvlog := log.New("module", "app/server")
```go
// all loggers can have key/value context
srvlog := log.New("module", "app/server")

// all log messages can have key/value context
srvlog.Warn("abnormal conn rate", "rate", curRate, "low", lowRate, "high", highRate)
// all log messages can have key/value context
srvlog.Warn("abnormal conn rate", "rate", curRate, "low", lowRate, "high", highRate)

// child loggers with inherited context
connlog := srvlog.New("raddr", c.RemoteAddr())
connlog.Info("connection open")
// child loggers with inherited context
connlog := srvlog.New("raddr", c.RemoteAddr())
connlog.Info("connection open")

// lazy evaluation
connlog.Debug("ping remote", "latency", log.Lazy(pingRemote))
// lazy evaluation
connlog.Debug("ping remote", "latency", log.Lazy(pingRemote))

// flexible configuration
srvlog.SetHandler(log.MultiHandler(
log.StreamHandler(os.Stderr, log.LogfmtFormat()),
log.LvlFilterHandler(
log.LvlError,
log.Must.FileHandler("errors.json", log.JsonHandler())))
// flexible configuration
srvlog.SetHandler(log.MultiHandler(
log.StreamHandler(os.Stderr, log.LogfmtFormat()),
log.LvlFilterHandler(
log.LvlError,
log.Must.FileHandler("errors.json", log.JsonHandler())))
```

## FAQ

### The varargs style is brittle and error prone! Can I have type saftey please?
Yes. Use log.Ctx:

srvlog := log.New(log.Ctx{"module": "app/server"})
srvlog.Warn("abnormal conn rate", log.Ctx{"rate": curRate, "low": lowRate, "high": highRate})
```go
srvlog := log.New(log.Ctx{"module": "app/server"})
srvlog.Warn("abnormal conn rate", log.Ctx{"rate": curRate, "low": lowRate, "high": highRate})
```

## License
Apache
19 changes: 19 additions & 0 deletions log15_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,25 @@ func TestLogContext(t *testing.T) {
}
}

func TestMapCtx(t *testing.T) {
t.Parallel()

l, _, r := testLogger()
l.Crit("test", Ctx{"foo": "bar"})

if len(r.Ctx) != 2 {
t.Fatalf("Wrong context length, got %d, expected %d", len(r.Ctx), 2)
}

if r.Ctx[0] != "foo" {
t.Fatalf("Wrong context key, got %s expected %s", r.Ctx[0], "foo")
}

if r.Ctx[1] != "bar" {
t.Fatalf("Wrong context value, got %s expected %s", r.Ctx[1], "bar")
}
}

func TestLvlFilterHandler(t *testing.T) {
t.Parallel()

Expand Down
2 changes: 1 addition & 1 deletion logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func newContext(prefix []interface{}, suffix []interface{}) []interface{} {
normalizedSuffix := normalize(suffix)
newCtx := make([]interface{}, len(prefix)+len(normalizedSuffix))
n := copy(newCtx, prefix)
copy(newCtx[n:], suffix)
copy(newCtx[n:], normalizedSuffix)
return newCtx
}

Expand Down

0 comments on commit 7cf5571

Please sign in to comment.