Documentation ¶
Overview ¶
Package alog provides a simple logger that has minimal structuring passed via context.Context.
Example (Levels) ¶
ctx := context.Background() l := New(WithEmitter(EmitterFunc(func(ctx context.Context, e *Entry) { for _, p := range e.Tags { if p[0] != "level" { continue } switch p[1] { case "error": fmt.Println("ERROR", e.Tags, e.Msg) fallthrough case "info": fallthrough case "debug": return } } }))) error := AddTags(ctx, "level", "error") info := AddTags(ctx, "level", "info") debug := AddTags(ctx, "level", "debug") l.Print(debug, "test") l.Print(info, "test") l.Print(error, "test")
Output: ERROR [[level error]] test
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var Default = New(WithEmitter(defaultEmitter))
Default is the the Logger the package-level Print functions use.
Functions ¶
func AddTags ¶
AddTags adds paired strings to the set of tags in the Context.
Any unpaired strings are ignored.
Types ¶
type Emitter ¶
Emitter is the interface that wraps the Emit method.
Emit handles a log entry in a customized way.
type EmitterFunc ¶
EmitterFunc is an adapter to allow the use of an ordinary function as an Emitter.
type Logger ¶
type Logger struct {
// contains filtered or unexported fields
}
Logger is a logging object that extracts tags from a context.Context and emits Entry structs.
A nil *Logger is valid to call methods on.
The default text format will have a newline appended if one is not present in the message.
func New ¶
New returns a configured *Logger, ready to use.
See the Option-returning functions in this package for configuration knobs.
func (*Logger) Output ¶
Output emits the supplied string while capturing the caller information "calldepth" frames back in the call stack. The value 2 is almost always what a caller wants. See also: runtime.Caller
func (*Logger) Print ¶
Print calls l.Output to emit a log entry. Arguments are handled like fmt.Print.
type Option ¶
type Option func(*Logger)
Option sets an option on a Logger.
Options are applied in the order specified, meaning if both To and WithEmitter are supplied in a call to New, the last one wins.
func OverrideTimestamp ¶
OverrideTimestamp sets the function that will be used to get the current time for each log entry.
This is primarily meant to be used for testing custom emitters. For example: OverrideTimestamp(func() time.Time { return time.Time{} })
func WithCaller ¶
func WithCaller() Option
WithCaller configures the logger to include the caller information in each log entry.
Example ¶
dumper := EmitterFunc(func(ctx context.Context, e *Entry) { fmt.Printf("%s:%d %s\n", filepath.Base(e.File), e.Line, e.Msg) }) ctx := context.Background() l := New(WithEmitter(dumper), WithCaller()) l.Print(ctx, "test")
Output: alog_test.go:61 test
func WithEmitter ¶
WithEmitter configures the logger to call e.Emit() every time it needs to emit a log line.
Calls are not synchronized.
Example ¶
dumper := EmitterFunc(func(ctx context.Context, e *Entry) { fmt.Printf("%v %s\n", e.Tags, e.Msg) }) ctx := context.Background() l := New(WithEmitter(dumper)) ctx = AddTags(ctx, "allthese", "tags") l.Print(ctx, "test")
Output: [[allthese tags]] test