mirror of https://github.com/ginuerzh/gost
4 changed files with 118 additions and 0 deletions
@ -0,0 +1,19 @@ |
|||
Copyright (c) 2017 Go Log |
|||
|
|||
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. |
|||
@ -0,0 +1,63 @@ |
|||
# Log [](https://godoc.org/github.com/go-log/log) |
|||
|
|||
Log is a logging interface for Go. That's it. Pass around the interface. |
|||
|
|||
## Rationale |
|||
|
|||
Users want to standardise logging. Sometimes libraries log. We leave the underlying logging implementation to the user |
|||
while allowing libraries to log by simply expecting something that satisfies the Logger interface. This leaves |
|||
the user free to pre-configure structure, output, etc. |
|||
|
|||
## Interface |
|||
|
|||
The interface is minimalistic on purpose |
|||
|
|||
```go |
|||
type Logger interface { |
|||
Log(v ...interface{}) |
|||
Logf(format string, v ...interface{}) |
|||
} |
|||
``` |
|||
|
|||
## Example |
|||
|
|||
Here's a logger that uses logrus and logs with predefined fields. |
|||
|
|||
```go |
|||
import ( |
|||
"github.com/go-log/log" |
|||
"github.com/sirupsen/logrus" |
|||
) |
|||
|
|||
type logrusLogger struct { |
|||
*logrus.Entry |
|||
} |
|||
|
|||
func (l *logrusLogger) Log(v ...interface{}) { |
|||
l.Entry.Print(v...) |
|||
} |
|||
|
|||
func (l *logrusLogger) Logf(format string, v ...interface{}) { |
|||
l.Entry.Printf(format, v...) |
|||
} |
|||
|
|||
func WithFields(f logrus.Fields) log.Logger { |
|||
return &logrusLogger{logrus.WithFields(f)} |
|||
} |
|||
``` |
|||
|
|||
The `WithFields` func returns a struct that satisfies the Logger interface. |
|||
|
|||
Pre-configure a logger using WithFields and pass it as an option to a library. |
|||
|
|||
```go |
|||
import "github.com/lib/foo" |
|||
|
|||
l := mylogger.WithFields(logrus.Fields{ |
|||
"library": "github.com/lib/foo", |
|||
}) |
|||
|
|||
f := foo.New( |
|||
foo.WithLogger(l), |
|||
) |
|||
``` |
|||
@ -0,0 +1,30 @@ |
|||
// Package log provides a log interface
|
|||
package log |
|||
|
|||
// Logger is a generic logging interface
|
|||
type Logger interface { |
|||
Log(v ...interface{}) |
|||
Logf(format string, v ...interface{}) |
|||
} |
|||
|
|||
var ( |
|||
// The global default logger
|
|||
DefaultLogger Logger = &noOpLogger{} |
|||
) |
|||
|
|||
// noOpLogger is used as a placeholder for the default logger
|
|||
type noOpLogger struct{} |
|||
|
|||
func (n *noOpLogger) Log(v ...interface{}) {} |
|||
|
|||
func (n *noOpLogger) Logf(format string, v ...interface{}) {} |
|||
|
|||
// Log logs using the default logger
|
|||
func Log(v ...interface{}) { |
|||
DefaultLogger.Log(v...) |
|||
} |
|||
|
|||
// Logf logs formatted using the default logger
|
|||
func Logf(format string, v ...interface{}) { |
|||
DefaultLogger.Logf(format, v...) |
|||
} |
|||
Loading…
Reference in new issue