Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
nrwiersma committed Feb 11, 2019
0 parents commit fcf369b
Show file tree
Hide file tree
Showing 19 changed files with 1,513 additions and 0 deletions.
Empty file added .gitignore
Empty file.
21 changes: 21 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
sudo: false

language: go
go:
- "1.11"
- "1.x"
env:
- GO111MODULE=on

before_install:
- go get -u golang.org/x/lint/golint
- go get github.com/mattn/goveralls

script:
- golint ./...
- go vet ./...
- go test -covermode=count -coverprofile=profile.cov ./...
- goveralls -coverprofile=profile.cov -service=travis-ci

notifications:
email: false
22 changes: 22 additions & 0 deletions LICENCE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Copyright (c) 2019 Nicholas Wiersma

MIT License

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include github.com/hamba/make/golang
38 changes: 38 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
![Logo](http://svg.wiersma.co.za/github/project?lang=go&title=logger&tag=fast%20Go%20logger)

[![Go Report Card](https://goreportcard.com/badge/github.com/hamba/logger)](https://goreportcard.com/report/github.com/hamba/logger)
[![Build Status](https://travis-ci.org/hamba/logger.svg?branch=master)](https://travis-ci.org/hamba/logger)
[![Coverage Status](https://coveralls.io/repos/github/hamba/logger/badge.svg?branch=master)](https://coveralls.io/github/hamba/logger?branch=master)
[![GoDoc](https://godoc.org/github.com/hamba/logger?status.svg)](https://godoc.org/github.com/hamba/logger)
[![GitHub release](https://img.shields.io/github/release/hamba/logger.svg)](https://github.com/hamba/logger/releases)
[![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://raw.githubusercontent.com/hamba/logger/master/LICENSE)

## Overview

Install with:

```shell
go get github.com/hamba/logger
```

## Examples

```go
// Composable handlers
h := logger.LevelFilterHandler(
logger.Info,
logger.StreamHandler(os.Stdout, logger.LogfmtFormat()),
)

// The logger can have an initial context
l := logger.New(h, "env", "prod")

// All messages can have a context
l.Warn("connection error", "redis", conn.Name(), "timeout", conn.Timeout())
```

Will log the message

```
lvl=warn msg="connection error" redis=dsn_1 timeout=0.500
```
59 changes: 59 additions & 0 deletions bench_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package logger_test

import (
"bytes"
"os"
"testing"

"github.com/hamba/logger"
)

func BenchmarkLogged_Logfmt(b *testing.B) {
buf := &bytes.Buffer{}
l := logger.New(logger.StreamHandler(buf, logger.LogfmtFormat()), "_n", "bench", "_p", 1)

b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
l.Error("some message", "key", 1, "key2", 3.141592, "key3", "string", "key4", false)
}
b.StopTimer()
}

func BenchmarkLogged_Json(b *testing.B) {
buf := &bytes.Buffer{}
l := logger.New(logger.StreamHandler(buf, logger.JSONFormat()), "_n", "bench", "_p", 1)

b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
l.Error("some message", "key", 1, "key2", 3.141592, "key3", "string", "key4", false)
}
b.StopTimer()
}

func BenchmarkLevelLogged_Logfmt(b *testing.B) {
buf := &bytes.Buffer{}
b.ResetTimer()
l := logger.New(logger.StreamHandler(buf, logger.LogfmtFormat()), "_n", "bench", "_p", os.Getpid())
for i := 0; i < b.N; i++ {
l.Debug("debug", "key", 1, "key2", 3.141592, "key3", "string", "key4", false)
l.Info("info", "key", 1, "key2", 3.141592, "key3", "string", "key4", false)
l.Warn("warn", "key", 1, "key2", 3.141592, "key3", "string", "key4", false)
l.Error("error", "key", 1, "key2", 3.141592, "key3", "string", "key4", false)
}
b.StopTimer()
}

func BenchmarkLevelLogged_Json(b *testing.B) {
buf := &bytes.Buffer{}
b.ResetTimer()
l := logger.New(logger.StreamHandler(buf, logger.JSONFormat()), "_n", "bench", "_p", os.Getpid())
for i := 0; i < b.N; i++ {
l.Debug("debug", "key", 1, "key2", 3.141592, "key3", "string", "key4", false)
l.Info("info", "key", 1, "key2", 3.141592, "key3", "string", "key4", false)
l.Warn("warn", "key", 1, "key2", 3.141592, "key3", "string", "key4", false)
l.Error("error", "key", 1, "key2", 3.141592, "key3", "string", "key4", false)
}
b.StopTimer()
}
19 changes: 19 additions & 0 deletions doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
Package logger implements a logging package.
logger implements github.com/hamba/pkg Logger interface.
Example usage:
// Composable handlers
h := logger.LevelFilterHandler(
logger.Info,
logger.StreamHandler(os.Stdout, logger.LogfmtFormat()),
)
// The logger can have an initial context
l := logger.New(h, "env", "prod")
// All messages can have a context
l.Error("connection error", "redis", conn.Name(), "timeout", conn.Timeout())
*/
package logger
59 changes: 59 additions & 0 deletions example_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package logger_test

import (
"os"
"time"

"github.com/hamba/logger"
)

func ExampleNew() {
h := logger.LevelFilterHandler(
logger.Info,
logger.StreamHandler(os.Stdout, logger.LogfmtFormat()),
)

l := logger.New(h, "env", "prod") // The logger can have an initial context

l.Info("redis connection", "redis", "some redis name", "timeout", 10)
}

func ExampleBufferedStreamHandler() {
h := logger.BufferedStreamHandler(os.Stdout, 2000, 1*time.Second, logger.LogfmtFormat())

l := logger.New(h, "env", "prod")

l.Info("redis connection", "redis", "some redis name", "timeout", 10)
}

func ExampleStreamHandler() {
h := logger.StreamHandler(os.Stdout, logger.LogfmtFormat())

l := logger.New(h, "env", "prod")

l.Info("redis connection", "redis", "some redis name", "timeout", 10)
}

func ExampleLevelFilterHandler() {
h := logger.LevelFilterHandler(
logger.Info,
logger.StreamHandler(os.Stdout, logger.LogfmtFormat()),
)

l := logger.New(h, "env", "prod")

l.Info("redis connection", "redis", "some redis name", "timeout", 10)
}

func ExampleFilterHandler() {
h := logger.FilterHandler(
func(msg string, lvl logger.Level, ctx []interface{}) bool {
return msg == "some condition"
},
logger.StreamHandler(os.Stdout, logger.LogfmtFormat()),
)

l := logger.New(h, "env", "prod")

l.Info("redis connection", "redis", "some redis name", "timeout", 10)
}
Loading

0 comments on commit fcf369b

Please sign in to comment.