Skip to content

Commit

Permalink
Transferring log to github
Browse files Browse the repository at this point in the history
  • Loading branch information
Oleg Sklyar committed Oct 29, 2017
1 parent d6f4da7 commit 57b8b70
Show file tree
Hide file tree
Showing 16 changed files with 180 additions and 125 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.idea/
vendor/
Gopkg.lock
19 changes: 19 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
language: go

go:
- 1.8

before_install:
- go get
- touch coverage.txt
- pip install --user codecov

script:
- go test -coverprofile=coverage-log.txt -covermode=atomic github.com/teris-io/log && touch coverage-log.txt
- go test -coverprofile=coverage-log-apex.txt -covermode=atomic github.com/teris-io/log/apex && touch coverage-log-apex.txt
- go test -coverprofile=coverage-log-std.txt -covermode=atomic github.com/teris-io/log/std && touch coverage-log-std.txt
- cat coverage-log.txt coverage-log-apex.txt coverage-log-std.txt > coverage.txt

after_success:
- codecov

7 changes: 7 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[[constraint]]
name = "github.com/apex/log"
version = "1.0.0"

[[constraint]]
name = "github.com/pkg/errors"
version = "0.8.0"
18 changes: 18 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
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.
51 changes: 24 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
# Structured leveled log interface
[![Build status][buildimage]][build] [![Coverage][codecovimage]][codecov] [![GoReportCard][cardimage]][card] [![API documentation][docsimage]][docs]

Package `log` decouples the logger backend from your application. It defines
# Structured log interface

Package `log` provides the separation of the logging interface from its
implementation and decouples the logger backend from your application. It defines
a simple, lightweight and comprehensive `Logger` and `Factory` interfaces which
can be used through your applications without any knowledge of the particular
implemeting backend and can be configured at the application wiring point to
bind a particular backend, such as Go's standard logger, `apex/log`, `logrus`,
with ease.

To complement the facade, the package `code.teris.io/util/log/std` provides an
To complement the facade, the package `github.com/teris-io/log/std` provides an
implementation using the standard Go logger. The default log formatter for
this implementation uses colour coding for log levels and logs the date
leaving out the month and the year on the timestamp. However, the formatter
is fully configurable.

Similarly, the package `code.teris.io/util/log/apex` provides and implementation
Similarly, the package `github.com/teris-io/log/apex` provides and implementation
using the `apex/log` logger backend.


Expand All @@ -26,7 +29,7 @@ type Logger interface {
Level(lvl LogLevel) Logger
Field(k string, v interface{}) Logger
Fields(data map[string]interface{}) Logger
WithError(err error) Logger
Error(err error) Logger
Log(msg string) Tracer
Logf(format string, v ...interface{}) Tracer
}
Expand All @@ -52,19 +55,19 @@ The log can be used both statically by binding a particular logger factory:

```go
func init() {
std.Use(os.Stderr, log.Info, std.DefaultFmtFun)
std.Use(os.Stderr, log.InfoLevel, std.DefaultFmtFun)
}

// elsewhere
logger := log.Level(log.Info).Field("key", "value")
logger := log.Level(log.InfoLevel).Field("key", "value")
logger.Log("message")
```

and dynamically by always going via a factory:

```go
factory := std.NewFactory(os.Stderr, log.Info, std.DefaultFmtFun)
logger := factory.Level(log.Info).Field("key", "value")
factory := std.NewFactory(os.Stderr, log.InfoLevel, std.DefaultFmtFun)
logger := factory.Level(log.InfoLevel).Field("key", "value")
logger.Log("message")
```

Expand All @@ -76,7 +79,7 @@ To simplify debugging with execution time tracing, the `Log` and `Logf` methods
return a tracer that can be used to measure and log the execution time:

```go
logger := log.Level(log.Debug).Field("key", "value")
logger := log.Level(log.DebugLevel).Field("key", "value")

defer logger.Log("start").Trace()
// code to trace the execution time of
Expand All @@ -88,25 +91,19 @@ the selected `Debug` level (her for the default formatter of the `std` logger):
08 16:31:42.023798 DBG start {key: value}
08 16:31:45.127619 DBG traced {duration: 3.103725832}, {key: value}

# License and copyright
### License and copyright

Copyright (c) 2017. Oleg Sklyar and teris.io. MIT license applies. All rights reserved.

Copyright (c) 2017 Oleg Sklyar & teris.io

MIT License
[build]: https://travis-ci.org/teris-io/log
[buildimage]: https://travis-ci.org/teris-io/log.svg?branch=master

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:
[codecov]: https://codecov.io/github/teris-io/log?branch=master
[codecovimage]: https://codecov.io/github/teris-io/log/coverage.svg?branch=master

The above copyright notice and this permission notice shall be included in all copies
or substantial portions of the Software.
[card]: http://goreportcard.com/report/teris-io/log
[cardimage]: https://goreportcard.com/badge/github.com/teris-io/log

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.
[docs]: https://godoc.org/github.com/teris-io/log
[docsimage]: http://img.shields.io/badge/godoc-reference-blue.svg?style=flat
13 changes: 7 additions & 6 deletions apex/factory.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
// Copyright (c) 2017 Oleg Sklyar & teris.io.
// Copyright (c) 2017. Oleg Sklyar & teris.io. All rights reserved.
// See the LICENSE file in the project root for licensing information.

package apex

import (
"code.teris.io/util/log"
alog "github.com/apex/log"
"github.com/teris-io/log"
)

func NewFactory(root *alog.Logger) log.Factory {
return &factory{root: root, min: log.Unset}
return &factory{root: root, min: log.UnsetLevel}
}

func Use(root *alog.Logger) log.Factory {
Expand All @@ -19,15 +20,15 @@ func Use(root *alog.Logger) log.Factory {

type factory struct {
root *alog.Logger
min log.LogLevel
min log.LoggerLevel
}

var _ log.Factory = (*factory)(nil)

func (f *factory) New() log.Logger {
return &logger{factory: f, lvl: log.Unset, ctx: alog.NewEntry(f.root)}
return &logger{factory: f, lvl: log.UnsetLevel, ctx: alog.NewEntry(f.root)}
}

func (f *factory) Threshold(min log.LogLevel) {
func (f *factory) Threshold(min log.LoggerLevel) {
f.min = min
}
17 changes: 9 additions & 8 deletions apex/logger.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Copyright (c) 2017 Oleg Sklyar & teris.io
// Copyright (c) 2017. Oleg Sklyar & teris.io. All rights reserved.
// See the LICENSE file in the project root for licensing information.

// Package apex provides a logger implementation using the github.com/apex/log backends.
package apex
Expand All @@ -7,19 +8,19 @@ import (
"fmt"
"time"

"code.teris.io/util/log"
alog "github.com/apex/log"
"github.com/teris-io/log"
)

var _ log.Logger = (*logger)(nil)

type logger struct {
*factory
lvl log.LogLevel
lvl log.LoggerLevel
ctx *alog.Entry
}

func (l *logger) Level(lvl log.LogLevel) log.Logger {
func (l *logger) Level(lvl log.LoggerLevel) log.Logger {
return &logger{factory: l.factory, lvl: lvl, ctx: l.ctx}
}

Expand All @@ -35,18 +36,18 @@ func (l *logger) Fields(data map[string]interface{}) log.Logger {
return &logger{factory: l.factory, lvl: l.lvl, ctx: l.ctx.WithFields(fields)}
}

func (l *logger) WithError(err error) log.Logger {
func (l *logger) Error(err error) log.Logger {
return &logger{factory: l.factory, lvl: l.lvl, ctx: l.ctx.WithError(err)}
}

func (l *logger) Log(msg string) log.Tracer {
if l.lvl >= l.factory.min {
switch {
case l.lvl <= log.Debug:
case l.lvl <= log.DebugLevel:
l.ctx.Debug(msg)
case l.lvl == log.Info:
case l.lvl == log.InfoLevel:
l.ctx.Info(msg)
case l.lvl >= log.Error:
case l.lvl >= log.ErrorLevel:
l.ctx.Error(msg)
}
}
Expand Down
3 changes: 2 additions & 1 deletion doc.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Copyright (c) 2017 Oleg Sklyar & teris.io.
// Copyright (c) 2017. Oleg Sklyar & teris.io. All rights reserved.
// See the LICENSE file in the project root for licensing information.

// Package log defines the Logger interface for a structured leveled log, the Factory
// interface to create instances of the Logger and the Tracer interface used to trace
Expand Down
35 changes: 18 additions & 17 deletions logger.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
// Copyright (c) 2017 Oleg Sklyar & teris.io
// Copyright (c) 2017. Oleg Sklyar & teris.io. All rights reserved.
// See the LICENSE file in the project root for licensing information.

package log

// LogLevel defines level markers for log entries.
type LogLevel int
// LoggerLevel defines level markers for log entries.
type LoggerLevel int

const (
// Unset level should not be output by logger implementation.
Unset = iota - 2
// Debug level marks detailed output for design purposes.
Debug
// Info level is the default log output marker.
Info
// Error level marks an error output.
Error
// UnsetLevel should not be output by logger implementation.
UnsetLevel = iota - 2
// DebugLevel marks detailed output for design purposes.
DebugLevel
// InfoLevel is the default log output marker.
InfoLevel
// ErrorLevel marks an error output.
ErrorLevel
)

// Factory defines a utility to create new loggers and set the log level threshold.
Expand All @@ -22,25 +23,25 @@ type Factory interface {
//New creates a new logger.
New() Logger

// Threshold sets the minimum log level threshold for messages to be output.
Threshold(min LogLevel)
// Threshold sets the minimum logger level threshold for messages to be output.
Threshold(min LoggerLevel)
}

// Logger defines the logger interface.
type Logger interface {

// Level creates a new logger instance from the current one setting its log level to the value supplied.
Level(lvl LogLevel) Logger
Level(lvl LoggerLevel) Logger

// Field creates a new logger instance from the current one adding a new field value.
Field(k string, v interface{}) Logger

// Fields creates a new logger instance from the current one adding a collection of field values.
Fields(data map[string]interface{}) Logger

// WithError creates a new logger instance from the current one adding an error
// and setting the level to Error.
WithError(err error) Logger
// Error creates a new logger instance from the current one adding an error
// and setting the level to ErrorLevel.
Error(err error) Logger

// Log outputs the log structure along with a message if the logger level is above or matching
// the threshold set in the factory.
Expand Down
9 changes: 5 additions & 4 deletions noop.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Copyright (c) 2017 Oleg Sklyar & teris.io.
// Copyright (c) 2017. Oleg Sklyar & teris.io. All rights reserved.
// See the LICENSE file in the project root for licensing information.

package log

Expand All @@ -17,10 +18,10 @@ func (n *noop) New() Logger {
return &noop{}
}

func (n *noop) Threshold(lvl LogLevel) {
func (n *noop) Threshold(lvl LoggerLevel) {
}

func (n *noop) Level(lvl LogLevel) Logger {
func (n *noop) Level(lvl LoggerLevel) Logger {
return n
}

Expand All @@ -32,7 +33,7 @@ func (n *noop) Fields(data map[string]interface{}) Logger {
return n
}

func (n *noop) WithError(err error) Logger {
func (n *noop) Error(err error) Logger {
return n
}

Expand Down
11 changes: 6 additions & 5 deletions static.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Copyright (c) 2017 Oleg Sklyar & teris.io.
// Copyright (c) 2017. Oleg Sklyar & teris.io. All rights reserved.
// See the LICENSE file in the project root for licensing information.

package log

Expand All @@ -15,7 +16,7 @@ func New() Logger {
}

// Level returns a new logger instance from the factory setting its log level to the value supplied.
func Level(lvl LogLevel) Logger {
func Level(lvl LoggerLevel) Logger {
return factory.New().Level(lvl)
}

Expand All @@ -29,9 +30,9 @@ func Fields(data map[string]interface{}) Logger {
return factory.New().Fields(data)
}

// WithError returns a new logger instance from the factory setting the error as supplied.
func WithError(err error) Logger {
return factory.New().WithError(err)
// Error returns a new logger instance from the factory setting the error as supplied.
func Error(err error) Logger {
return factory.New().Error(err)
}

// Log constructs a new logger instance from the factory with no context and logs a message.
Expand Down
Loading

0 comments on commit 57b8b70

Please sign in to comment.