update ln
This commit is contained in:
parent
92871b43ed
commit
4d05d9b7cd
|
@ -323,3 +323,5 @@ b6e1ae21643682ce023deb8d152024597b0e9bb4 golang.org/x/sys/unix
|
|||
8443e311d3925f5e20494496790670942ed48504 google.golang.org/grpc/status
|
||||
8443e311d3925f5e20494496790670942ed48504 google.golang.org/grpc/tap
|
||||
8443e311d3925f5e20494496790670942ed48504 google.golang.org/grpc/transport
|
||||
466e05b2ef3e48ce08a367b6aaac09ee29a124e5 github.com/Xe/ln
|
||||
2b3a18b5f0fb6b4f9190549597d3f962c02bc5eb github.com/pkg/errors
|
||||
|
|
|
@ -0,0 +1,11 @@
|
|||
package ln
|
||||
|
||||
// Action is a convenience helper for logging the "action" being performed as
|
||||
// part of a log line.
|
||||
//
|
||||
// It is a convenience wrapper for the following:
|
||||
//
|
||||
// ln.Log(ctx, fer, f, ln.Action("writing frozberry sales reports to database"))
|
||||
func Action(act string) Fer {
|
||||
return F{"action": act}
|
||||
}
|
|
@ -0,0 +1,38 @@
|
|||
package ln
|
||||
|
||||
import (
|
||||
"context"
|
||||
)
|
||||
|
||||
type ctxKey int
|
||||
|
||||
const (
|
||||
fKey = iota
|
||||
)
|
||||
|
||||
// WithF stores or appends a given F instance into a context.
|
||||
func WithF(ctx context.Context, f F) context.Context {
|
||||
pf, ok := FFromContext(ctx)
|
||||
if !ok {
|
||||
return context.WithValue(ctx, fKey, f)
|
||||
}
|
||||
|
||||
pf.Extend(f)
|
||||
|
||||
return context.WithValue(ctx, fKey, pf)
|
||||
}
|
||||
|
||||
// FFromContext fetches the `F` out of the context if it exists.
|
||||
func FFromContext(ctx context.Context) (F, bool) {
|
||||
fvp := ctx.Value(fKey)
|
||||
if fvp == nil {
|
||||
return nil, false
|
||||
}
|
||||
|
||||
f, ok := fvp.(F)
|
||||
if !ok {
|
||||
return nil, false
|
||||
}
|
||||
|
||||
return f, true
|
||||
}
|
|
@ -1,23 +1,24 @@
|
|||
package ln
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"sync"
|
||||
)
|
||||
|
||||
// Filter interface for defining chain filters
|
||||
type Filter interface {
|
||||
Apply(Event) bool
|
||||
Apply(ctx context.Context, e Event) bool
|
||||
Run()
|
||||
Close()
|
||||
}
|
||||
|
||||
// FilterFunc allows simple functions to implement the Filter interface
|
||||
type FilterFunc func(e Event) bool
|
||||
type FilterFunc func(ctx context.Context, e Event) bool
|
||||
|
||||
// Apply implements the Filter interface
|
||||
func (ff FilterFunc) Apply(e Event) bool {
|
||||
return ff(e)
|
||||
func (ff FilterFunc) Apply(ctx context.Context, e Event) bool {
|
||||
return ff(ctx, e)
|
||||
}
|
||||
|
||||
// Run implements the Filter interface
|
||||
|
@ -45,8 +46,8 @@ func NewWriterFilter(out io.Writer, format Formatter) *WriterFilter {
|
|||
}
|
||||
|
||||
// Apply implements the Filter interface
|
||||
func (w *WriterFilter) Apply(e Event) bool {
|
||||
output, err := w.Formatter.Format(e)
|
||||
func (w *WriterFilter) Apply(ctx context.Context, e Event) bool {
|
||||
output, err := w.Formatter.Format(ctx, e)
|
||||
if err == nil {
|
||||
w.Lock()
|
||||
w.Out.Write(output)
|
||||
|
@ -63,4 +64,4 @@ func (w *WriterFilter) Run() {}
|
|||
func (w *WriterFilter) Close() {}
|
||||
|
||||
// NilFilter is safe to return as a Filter, but does nothing
|
||||
var NilFilter = FilterFunc(func(e Event) bool { return true })
|
||||
var NilFilter = FilterFunc(func(_ context.Context, e Event) bool { return true })
|
||||
|
|
|
@ -2,6 +2,7 @@ package ln
|
|||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
@ -13,7 +14,7 @@ var (
|
|||
|
||||
// Formatter defines the formatting of events
|
||||
type Formatter interface {
|
||||
Format(Event) ([]byte, error)
|
||||
Format(ctx context.Context, e Event) ([]byte, error)
|
||||
}
|
||||
|
||||
// DefaultFormatter is the default way in which to format events
|
||||
|
@ -36,7 +37,7 @@ func NewTextFormatter() Formatter {
|
|||
}
|
||||
|
||||
// Format implements the Formatter interface
|
||||
func (t *TextFormatter) Format(e Event) ([]byte, error) {
|
||||
func (t *TextFormatter) Format(_ context.Context, e Event) ([]byte, error) {
|
||||
var writer bytes.Buffer
|
||||
|
||||
writer.WriteString("time=\"")
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package ln
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
|
@ -61,7 +62,7 @@ type Event struct {
|
|||
}
|
||||
|
||||
// Log is the generic logging method.
|
||||
func (l *Logger) Log(xs ...Fer) {
|
||||
func (l *Logger) Log(ctx context.Context, xs ...Fer) {
|
||||
event := Event{Time: time.Now()}
|
||||
|
||||
addF := func(bf F) {
|
||||
|
@ -78,6 +79,11 @@ func (l *Logger) Log(xs ...Fer) {
|
|||
addF(f.F())
|
||||
}
|
||||
|
||||
ctxf, ok := FFromContext(ctx)
|
||||
if ok {
|
||||
addF(ctxf)
|
||||
}
|
||||
|
||||
if os.Getenv("LN_DEBUG_ALL_EVENTS") == "1" {
|
||||
frame := callersFrame()
|
||||
if event.Data == nil {
|
||||
|
@ -88,19 +94,19 @@ func (l *Logger) Log(xs ...Fer) {
|
|||
event.Data["_filename"] = frame.filename
|
||||
}
|
||||
|
||||
l.filter(event)
|
||||
l.filter(ctx, event)
|
||||
}
|
||||
|
||||
func (l *Logger) filter(e Event) {
|
||||
func (l *Logger) filter(ctx context.Context, e Event) {
|
||||
for _, f := range l.Filters {
|
||||
if !f.Apply(e) {
|
||||
if !f.Apply(ctx, e) {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Error logs an error and information about the context of said error.
|
||||
func (l *Logger) Error(err error, xs ...Fer) {
|
||||
func (l *Logger) Error(ctx context.Context, err error, xs ...Fer) {
|
||||
data := F{}
|
||||
frame := callersFrame()
|
||||
|
||||
|
@ -116,20 +122,20 @@ func (l *Logger) Error(err error, xs ...Fer) {
|
|||
|
||||
xs = append(xs, data)
|
||||
|
||||
l.Log(xs...)
|
||||
l.Log(ctx, xs...)
|
||||
}
|
||||
|
||||
// Fatal logs this set of values, then exits with status code 1.
|
||||
func (l *Logger) Fatal(xs ...Fer) {
|
||||
func (l *Logger) Fatal(ctx context.Context, xs ...Fer) {
|
||||
xs = append(xs, F{"fatal": true})
|
||||
|
||||
l.Log(xs...)
|
||||
l.Log(ctx, xs...)
|
||||
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
// FatalErr combines Fatal and Error.
|
||||
func (l *Logger) FatalErr(err error, xs ...Fer) {
|
||||
func (l *Logger) FatalErr(ctx context.Context, err error, xs ...Fer) {
|
||||
xs = append(xs, F{"fatal": true})
|
||||
|
||||
data := F{}
|
||||
|
@ -146,7 +152,7 @@ func (l *Logger) FatalErr(err error, xs ...Fer) {
|
|||
}
|
||||
|
||||
xs = append(xs, data)
|
||||
l.Log(xs...)
|
||||
l.Log(ctx, xs...)
|
||||
|
||||
os.Exit(1)
|
||||
}
|
||||
|
@ -154,21 +160,21 @@ func (l *Logger) FatalErr(err error, xs ...Fer) {
|
|||
// Default Implementation
|
||||
|
||||
// Log is the generic logging method.
|
||||
func Log(xs ...Fer) {
|
||||
DefaultLogger.Log(xs...)
|
||||
func Log(ctx context.Context, xs ...Fer) {
|
||||
DefaultLogger.Log(ctx, xs...)
|
||||
}
|
||||
|
||||
// Error logs an error and information about the context of said error.
|
||||
func Error(err error, xs ...Fer) {
|
||||
DefaultLogger.Error(err, xs...)
|
||||
func Error(ctx context.Context, err error, xs ...Fer) {
|
||||
DefaultLogger.Error(ctx, err, xs...)
|
||||
}
|
||||
|
||||
// Fatal logs this set of values, then exits with status code 1.
|
||||
func Fatal(xs ...Fer) {
|
||||
DefaultLogger.Fatal(xs...)
|
||||
func Fatal(ctx context.Context, xs ...Fer) {
|
||||
DefaultLogger.Fatal(ctx, xs...)
|
||||
}
|
||||
|
||||
// FatalErr combines Fatal and Error.
|
||||
func FatalErr(err error, xs ...Fer) {
|
||||
DefaultLogger.FatalErr(err, xs...)
|
||||
func FatalErr(ctx context.Context, err error, xs ...Fer) {
|
||||
DefaultLogger.FatalErr(ctx, err, xs...)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue