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/status
|
||||||
8443e311d3925f5e20494496790670942ed48504 google.golang.org/grpc/tap
|
8443e311d3925f5e20494496790670942ed48504 google.golang.org/grpc/tap
|
||||||
8443e311d3925f5e20494496790670942ed48504 google.golang.org/grpc/transport
|
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
|
package ln
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"io"
|
"io"
|
||||||
"sync"
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Filter interface for defining chain filters
|
// Filter interface for defining chain filters
|
||||||
type Filter interface {
|
type Filter interface {
|
||||||
Apply(Event) bool
|
Apply(ctx context.Context, e Event) bool
|
||||||
Run()
|
Run()
|
||||||
Close()
|
Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
// FilterFunc allows simple functions to implement the Filter interface
|
// 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
|
// Apply implements the Filter interface
|
||||||
func (ff FilterFunc) Apply(e Event) bool {
|
func (ff FilterFunc) Apply(ctx context.Context, e Event) bool {
|
||||||
return ff(e)
|
return ff(ctx, e)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Run implements the Filter interface
|
// Run implements the Filter interface
|
||||||
|
@ -45,8 +46,8 @@ func NewWriterFilter(out io.Writer, format Formatter) *WriterFilter {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Apply implements the Filter interface
|
// Apply implements the Filter interface
|
||||||
func (w *WriterFilter) Apply(e Event) bool {
|
func (w *WriterFilter) Apply(ctx context.Context, e Event) bool {
|
||||||
output, err := w.Formatter.Format(e)
|
output, err := w.Formatter.Format(ctx, e)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
w.Lock()
|
w.Lock()
|
||||||
w.Out.Write(output)
|
w.Out.Write(output)
|
||||||
|
@ -63,4 +64,4 @@ func (w *WriterFilter) Run() {}
|
||||||
func (w *WriterFilter) Close() {}
|
func (w *WriterFilter) Close() {}
|
||||||
|
|
||||||
// NilFilter is safe to return as a Filter, but does nothing
|
// 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 (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
@ -13,7 +14,7 @@ var (
|
||||||
|
|
||||||
// Formatter defines the formatting of events
|
// Formatter defines the formatting of events
|
||||||
type Formatter interface {
|
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
|
// DefaultFormatter is the default way in which to format events
|
||||||
|
@ -36,7 +37,7 @@ func NewTextFormatter() Formatter {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Format implements the Formatter interface
|
// 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
|
var writer bytes.Buffer
|
||||||
|
|
||||||
writer.WriteString("time=\"")
|
writer.WriteString("time=\"")
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package ln
|
package ln
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"os"
|
"os"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
@ -61,7 +62,7 @@ type Event struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Log is the generic logging method.
|
// 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()}
|
event := Event{Time: time.Now()}
|
||||||
|
|
||||||
addF := func(bf F) {
|
addF := func(bf F) {
|
||||||
|
@ -78,6 +79,11 @@ func (l *Logger) Log(xs ...Fer) {
|
||||||
addF(f.F())
|
addF(f.F())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ctxf, ok := FFromContext(ctx)
|
||||||
|
if ok {
|
||||||
|
addF(ctxf)
|
||||||
|
}
|
||||||
|
|
||||||
if os.Getenv("LN_DEBUG_ALL_EVENTS") == "1" {
|
if os.Getenv("LN_DEBUG_ALL_EVENTS") == "1" {
|
||||||
frame := callersFrame()
|
frame := callersFrame()
|
||||||
if event.Data == nil {
|
if event.Data == nil {
|
||||||
|
@ -88,19 +94,19 @@ func (l *Logger) Log(xs ...Fer) {
|
||||||
event.Data["_filename"] = frame.filename
|
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 {
|
for _, f := range l.Filters {
|
||||||
if !f.Apply(e) {
|
if !f.Apply(ctx, e) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Error logs an error and information about the context of said error.
|
// 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{}
|
data := F{}
|
||||||
frame := callersFrame()
|
frame := callersFrame()
|
||||||
|
|
||||||
|
@ -116,20 +122,20 @@ func (l *Logger) Error(err error, xs ...Fer) {
|
||||||
|
|
||||||
xs = append(xs, data)
|
xs = append(xs, data)
|
||||||
|
|
||||||
l.Log(xs...)
|
l.Log(ctx, xs...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fatal logs this set of values, then exits with status code 1.
|
// 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})
|
xs = append(xs, F{"fatal": true})
|
||||||
|
|
||||||
l.Log(xs...)
|
l.Log(ctx, xs...)
|
||||||
|
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
// FatalErr combines Fatal and Error.
|
// 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})
|
xs = append(xs, F{"fatal": true})
|
||||||
|
|
||||||
data := F{}
|
data := F{}
|
||||||
|
@ -146,7 +152,7 @@ func (l *Logger) FatalErr(err error, xs ...Fer) {
|
||||||
}
|
}
|
||||||
|
|
||||||
xs = append(xs, data)
|
xs = append(xs, data)
|
||||||
l.Log(xs...)
|
l.Log(ctx, xs...)
|
||||||
|
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
@ -154,21 +160,21 @@ func (l *Logger) FatalErr(err error, xs ...Fer) {
|
||||||
// Default Implementation
|
// Default Implementation
|
||||||
|
|
||||||
// Log is the generic logging method.
|
// Log is the generic logging method.
|
||||||
func Log(xs ...Fer) {
|
func Log(ctx context.Context, xs ...Fer) {
|
||||||
DefaultLogger.Log(xs...)
|
DefaultLogger.Log(ctx, xs...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Error logs an error and information about the context of said error.
|
// Error logs an error and information about the context of said error.
|
||||||
func Error(err error, xs ...Fer) {
|
func Error(ctx context.Context, err error, xs ...Fer) {
|
||||||
DefaultLogger.Error(err, xs...)
|
DefaultLogger.Error(ctx, err, xs...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fatal logs this set of values, then exits with status code 1.
|
// Fatal logs this set of values, then exits with status code 1.
|
||||||
func Fatal(xs ...Fer) {
|
func Fatal(ctx context.Context, xs ...Fer) {
|
||||||
DefaultLogger.Fatal(xs...)
|
DefaultLogger.Fatal(ctx, xs...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// FatalErr combines Fatal and Error.
|
// FatalErr combines Fatal and Error.
|
||||||
func FatalErr(err error, xs ...Fer) {
|
func FatalErr(ctx context.Context, err error, xs ...Fer) {
|
||||||
DefaultLogger.FatalErr(err, xs...)
|
DefaultLogger.FatalErr(ctx, err, xs...)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue