Documentation ¶
Overview ¶
Package alog provides a simple logger that has minimal structuing passed via context.Context.
Example ¶
ctx := context.Background() l := New(To(os.Stdout)) l.Print(ctx, "test") ctx = AddTags(ctx, "more", "context") l.Print(ctx, "test") ctx = AddTags(ctx, "most", "context") l.Print(ctx, "test")
Output: test [more=context] test [more=context most=context] test
Example (Levels) ¶
ctx := context.Background() l := New(WithEmitter(func(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(To(os.Stderr), WithShortFile(), WithDateFormat(time.RFC3339), WithUTC())
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 Entry ¶
Entry is the struct passed to user-supplied formatters.
The File and Line members are only populated if the WithFile() Option was passed to New.
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.
Example ¶
ctx := context.Background() l := New(To(os.Stdout), WithPrefix("Example "), WithShortFile()) ctx = AddTags(ctx, "example", "true") l.Print(ctx, "Examples have") l.Print(ctx, "weird line numbers")
Output: Example alog_test.go:62 [example=true] Examples have Example alog_test.go:63 [example=true] weird line numbers
func (*Logger) EmitText ¶
EmitText is the default text format emitter.
It closes over the supplied io.Writer and returns a function suitable to pass to WithEmitter.
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
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 To ¶
To configures a Logger to output log lines in a human-readable format to the supplied io.Writer.
It's equivalent to
var w io.Writer l := New(func(l *Logger) { WithEmitter(l.EmitText(w))(l) })
with additional guarentees that every entry generates a single Write call, and calls are serialized.
func WithDateFormat ¶
WithDateFormat sets the string format for timestamps using a layout string like the time package would take.
This only effects the default text format.
func WithEmitter ¶
WithEmitter configures the logger to call f every time it needs to emit a log line.
Calls to f are not synchronized.
Example ¶
dumper := func(e *Entry) { fmt.Printf("%v %s\n", e.Tags, e.Msg) } ctx := context.Background() l := New(WithEmitter(dumper), WithFile()) ctx = AddTags(ctx, "allthese", "tags") l.Print(ctx, "test")
Output: [[allthese tags]] test
func WithFile ¶
func WithFile() Option
WithFile collects call information on each log line, like the log package's Llongfile flag.
func WithPrefix ¶
WithPrefix adds a set prefix to all lines.
This only effects the default text format.
func WithShortFile ¶
func WithShortFile() Option
WithShortFile is like WithFile, but only prints the file name instead of the entire path.
This only effects the default text format.
Directories ¶
Path | Synopsis |
---|---|
emitter
|
|
gkelog/traceextractors/oc
Module
|
|
gkelog/traceextractors/otel
Module
|