alog

package module
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 6, 2018 License: MIT Imports: 8 Imported by: 0

README

alog

"another logger!"

GoDoc

Alog is a logging package aiming to hit a sweet spot between API surface, flexibility, and structure.

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

Default is the the Logger the package-level Print functions use.

Functions

func AddTags

func AddTags(ctx context.Context, pairs ...string) context.Context

AddTags adds paired strings to the set of tags in the Context.

Any unpaired strings are ignored.

func Print

func Print(ctx context.Context, v ...interface{})

Print uses the Default Logger to print the supplied string.

func Printf

func Printf(ctx context.Context, f string, v ...interface{})

Printf uses the Default Logger to format and then print the supplied string.

Types

type Entry

type Entry struct {
	Time time.Time
	Tags [][2]string
	File string
	Line int
	Msg  string
}

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

func New(opts ...Option) *Logger

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

func (l *Logger) EmitText(w io.Writer) func(e *Entry)

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

func (l *Logger) Output(ctx context.Context, calldepth int, msg string)

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

func (l *Logger) Print(ctx context.Context, v ...interface{})

Print calls l.Output to emit a log entry. Arguments are handled like fmt.Print.

func (*Logger) Printf

func (l *Logger) Printf(ctx context.Context, f string, v ...interface{})

Printf calls l.Output to emit a log entry. Arguments are handled like fmt.Printf.

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

func To(w io.Writer) Option

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

func WithDateFormat(layout string) Option

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

func WithEmitter(f func(e *Entry)) Option

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

func WithPrefix(prefix string) Option

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.

func WithUTC

func WithUTC() Option

WithUTC sets timestamps to UTC.

This only effects the default text format.

Directories

Path Synopsis
emitter

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL