log

package
v1.7.0-dev-20190926 Latest Latest
Warning

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

Go to latest
Published: Sep 25, 2019 License: Apache-2.0 Imports: 13 Imported by: 530

Documentation

Index

Constants

View Source
const (
	// NoSeverity is the option to disable the printing of the severity.
	NoSeverity = SeverityStyle(iota)
	// SeverityShort is the option to display the severity as a single character.
	SeverityShort
	// SeverityLong is the option to display the severity in its full name.
	SeverityLong
)
View Source
const (
	// NoValues is the option to disable the printing of values.
	NoValues = ValueStyle(iota)
	// ValuesSingleLine is the option to display all values on a single line.
	ValuesSingleLine
	// ValuesMultiLine is the option to display each value on a separate line.
	ValuesMultiLine
)

Variables

View Source
var (
	// Raw is a style that only prints the text of the message.
	Raw = Style{
		Name:      "raw",
		Timestamp: false,
		Tag:       false,
		Trace:     false,
		Process:   false,
		Severity:  NoSeverity,
		Values:    NoValues,
	}

	// Brief is a style that only prints the text and short severity of the
	// message.
	Brief = Style{
		Name:      "brief",
		Timestamp: false,
		Tag:       false,
		Trace:     false,
		Process:   false,
		Severity:  SeverityShort,
		Values:    NoValues,
	}

	// Normal is a style that prints the timestamp, tag, trace, process,
	// short severity.
	Normal = Style{
		Name:      "normal",
		Timestamp: true,
		Tag:       true,
		Trace:     true,
		Process:   true,
		Severity:  SeverityShort,
		Values:    NoValues,
	}

	// Detailed is a style that prints the timestamp, tag, trace, process,
	// long severity and multi-line values.
	Detailed = Style{
		Name:      "detailed",
		Timestamp: true,
		Tag:       true,
		Trace:     true,
		Process:   true,
		Severity:  SeverityLong,
		Values:    ValuesMultiLine,
	}
)
View Source
var (
	// NoClock is a Clock that disables printing of the time.
	NoClock = FixedClock(time.Time{})
)

Functions

func D

func D(ctx context.Context, fmt string, args ...interface{})

D logs a debug message to the logging target.

func E

func E(ctx context.Context, fmt string, args ...interface{})

E logs a error message to the logging target.

func Enter

func Enter(ctx context.Context, name string) context.Context

Enter returns a new context with the trace-stack pushed by name.

func Err

func Err(ctx context.Context, cause error, msg string) error

Err creates a new error that wraps cause with the current logging information.

func Errf

func Errf(ctx context.Context, cause error, fmt string, args ...interface{}) error

Errf creates a new error that wraps cause with the current logging information.

func F

func F(ctx context.Context, stopProcess bool, fmt string, args ...interface{})

F logs a fatal message to the logging target. If stopProcess is true then the message indicates the process should stop.

func GetProcess

func GetProcess(ctx context.Context) string

GetProcess returns the process assigned to ctx.

func GetTag

func GetTag(ctx context.Context) string

GetTag returns the Tag assigned to ctx.

func GetTrace

func GetTrace(ctx context.Context) []string

GetTrace returns the trace-stack.

func HHMMSSsss

func HHMMSSsss(t time.Time) string

HHMMSSsss prints the time as a HH:MM:SS.sss

func I

func I(ctx context.Context, fmt string, args ...interface{})

I logs a info message to the logging target.

func PutClock

func PutClock(ctx context.Context, c Clock) context.Context

PutClock returns a new context with the Clock assigned to w.

func PutFilter

func PutFilter(ctx context.Context, w Filter) context.Context

PutFilter returns a new context with the Filter assigned to w.

func PutHandler

func PutHandler(ctx context.Context, w Handler) context.Context

PutHandler returns a new context with the Handler assigned to w.

func PutProcess

func PutProcess(ctx context.Context, w string) context.Context

PutProcess returns a new context with the process assigned to w.

func PutStacktracer

func PutStacktracer(ctx context.Context, s Stacktracer) context.Context

PutStacktracer returns a new context with the Stacktracer assigned to w.

func PutTag

func PutTag(ctx context.Context, w string) context.Context

PutTag returns a new context with the tag assigned to w.

func RegisterStyle

func RegisterStyle(s Style)

RegisterStyle registers the style s. The list of registered styles can be displayed in command line flags.

func SubTest added in v1.2.0

func SubTest(ctx context.Context, t delegate) context.Context

SubTest returns the context with the TestHandler replaced with t. This is intended to be used for sub-tests. For example:

func TestExample(t *testing.T) {
  ctx := log.Testing(t)
  for _, test := range tests {
    t.Run(test.name, func(t *testing.T) {
      test.run(log.SubTest(ctx, t))
    }
  }
}

func Testing

func Testing(t delegate) context.Context

Testing returns a default context with a TestHandler installed.

func W

func W(ctx context.Context, fmt string, args ...interface{})

W logs a warning message to the logging target.

Types

type Broadcaster

type Broadcaster struct {
	// contains filtered or unexported fields
}

Broadcaster forwards all messages to all supplied handlers. Broadcaster implements the Handler interface.

func Broadcast

func Broadcast(handlers ...Handler) *Broadcaster

Broadcast forwards all messages sent to Broadcast to all supplied handlers. Additional handlers can be added with Listen.

func (*Broadcaster) Close

func (b *Broadcaster) Close()

Close calls Close on all the listening handlers and removes them from the broadcaster.

func (*Broadcaster) Count

func (b *Broadcaster) Count() int

Count returns the number of registered handlers.

func (*Broadcaster) Handle

func (b *Broadcaster) Handle(m *Message)

Handle broadcasts the page to all the listening handlers.

func (*Broadcaster) Listen

func (b *Broadcaster) Listen(h Handler) (unlisten func())

Listen calls adds h to the list of handlers that are informed of each log message passed to Handle.

type Clock

type Clock interface {
	Time() time.Time
}

Clock is the interface implemented by types that tell the time.

func GetClock

func GetClock(ctx context.Context) Clock

GetClock returns the Clock assigned to ctx.

type Filter

type Filter interface {
	// ShowSeverity returns true if the message of severity s should be shown.
	ShowSeverity(s Severity) bool
}

Filter is the filter of log messages.

func GetFilter

func GetFilter(ctx context.Context) Filter

GetFilter returns the Filter assigned to ctx.

type FixedClock

type FixedClock time.Time

FixedClock is a Clock that returns a fixed time.

func (FixedClock) Time

func (c FixedClock) Time() time.Time

Time returns the fixed clock time.

type Handler

type Handler interface {
	Handle(*Message)
	Close()
}

Handler is the handler of log messages.

func Channel

func Channel(to Handler, size int) Handler

Channel is a log handler that passes log messages to another Handler through a chan. This makes this Handler safe to use from multiple threads.

func GetHandler

func GetHandler(ctx context.Context) Handler

GetHandler returns the Handler assigned to ctx.

func NewHandler

func NewHandler(handle func(*Message), close func()) Handler

NewHandler returns a Handler that calls handle for each message and close when the handler is closed. close can be nil.

func OnClosed added in v0.6.2

func OnClosed(h Handler, closed func()) Handler

OnClosed returns a handler that forwards all messages on to h, but also calls closed after the returned handler is closed.

func TestHandler

func TestHandler(t delegate, s Style) Handler

TestHandler is a Writer that uses the style to write records to t's using the style s.

type Indirect added in v0.6.2

type Indirect struct {
	// contains filtered or unexported fields
}

Indirect is a Handler that can be dynamically retarget to another logger.

func (*Indirect) Close added in v0.6.2

func (i *Indirect) Close()

func (*Indirect) Handle added in v0.6.2

func (i *Indirect) Handle(m *Message)

func (*Indirect) IsDefault added in v1.2.0

func (i *Indirect) IsDefault() bool

IsDefault returns whether the current handler is considered the default handler (typically logging to stdout).

func (*Indirect) SetTarget added in v0.6.2

func (i *Indirect) SetTarget(l Handler, isDefault bool) (Handler, bool)

SetTarget assigns the handler target to l, returning the old target.

func (*Indirect) Target added in v0.6.2

func (i *Indirect) Target() Handler

Target returns the target handler.

type Logger

type Logger struct {
	// contains filtered or unexported fields
}

Logger provides a logging interface.

func Bind

func Bind(ctx context.Context, v V) *Logger

Bind returns a new Logger from the context ctx with the additional values in v.

func From

func From(ctx context.Context) *Logger

From returns a new Logger from the context ctx.

func (*Logger) D

func (l *Logger) D(fmt string, args ...interface{})

D logs a debug message to the logging target.

func (*Logger) E

func (l *Logger) E(fmt string, args ...interface{})

E logs a error message to the logging target.

func (*Logger) Err

func (l *Logger) Err(cause error, msg string) error

Err creates a new error that wraps cause with the current logging information.

func (*Logger) Errf

func (l *Logger) Errf(cause error, fmt string, args ...interface{}) error

Errf creates a new error that wraps cause with the current logging information.

func (*Logger) F

func (l *Logger) F(fmt string, stopProcess bool, args ...interface{})

F logs a fatal message to the logging target. If stopProcess is true then the message indicates the process should stop.

func (*Logger) Fatal

func (l *Logger) Fatal(args ...interface{})

Fatal implements the standard go logger interface.

func (*Logger) Fatalf

func (l *Logger) Fatalf(format string, args ...interface{})

Fatalf implements the standard go logger interface.

func (*Logger) Fatalln

func (l *Logger) Fatalln(args ...interface{})

Fatalln implements the standard go logger interface.

func (*Logger) I

func (l *Logger) I(fmt string, args ...interface{})

I logs a info message to the logging target.

func (*Logger) Log

func (l *Logger) Log(s Severity, stopProcess bool, f string)

Log logs a message at severity s to the logging target.

func (*Logger) Logf

func (l *Logger) Logf(s Severity, stopProcess bool, fmt string, args ...interface{})

Logf logs a printf-style message at severity s to the logging target.

func (*Logger) Message

func (l *Logger) Message(s Severity, stopProcess bool, text string) *Message

Message returns a new Message with the given severity and text.

func (*Logger) Messagef

func (l *Logger) Messagef(s Severity, stopProcess bool, text string, args ...interface{}) *Message

Messagef returns a new Message with the given severity and text.

func (*Logger) Print

func (l *Logger) Print(args ...interface{})

Print implements the standard go logger interface.

func (*Logger) Printf

func (l *Logger) Printf(format string, args ...interface{})

Printf implements the standard go logger interface.

func (*Logger) Println

func (l *Logger) Println(args ...interface{})

Println implements the standard go logger interface.

func (*Logger) SetFilter added in v1.3.1

func (l *Logger) SetFilter(f Filter) *Logger

SetFilter sets the filter for the logger

func (*Logger) W

func (l *Logger) W(fmt string, args ...interface{})

W logs a warning message to the logging target.

func (*Logger) Writer

func (l *Logger) Writer(s Severity) io.WriteCloser

Writer returns an io.WriteCloser that writes lines to to the logger at the specified severity.

type Message

type Message struct {
	// The message text.
	Text string

	// The time the message was logged.
	Time time.Time

	// The severity of the message.
	Severity Severity

	// StopProcess is true if the message indicates the process should stop.
	StopProcess bool

	// The tag associated with the log record.
	Tag string

	// The name of the process that created the record.
	Process string

	// The callstack at the time the message was logged.
	Callstack []*SourceLocation

	// The stack of enter() calls at the time the message was logged.
	Trace Trace

	// The key-value pairs of extra data.
	Values Values
}

type Severity

type Severity int32

Severity defines the severity of a logging message.

const (
	// Verbose indicates extremely verbose level messages.
	Verbose Severity = 0
	// Debug indicates debug-level messages.
	Debug Severity = 1
	// Info indicates minor informational messages that should generally be ignored.
	Info Severity = 2
	// Warning indicates issues that might affect performance or compatibility, but could be ignored.
	Warning Severity = 3
	// Error indicates non terminal failure conditions that may have an effect on results.
	Error Severity = 4
	// Fatal indicates a fatal error.
	Fatal Severity = 5
)

The values must be identical to values in gapis/service/service.proto

func (*Severity) Choose

func (s *Severity) Choose(c interface{})

Choose allows *Severity to be used as a command line flag.

func (*Severity) Chooser

func (s *Severity) Chooser() flags.Chooser

Chooser returns a chooser for the set of severities.

func (Severity) Short

func (s Severity) Short() string

Short returns the severity string with a single character.

func (Severity) String

func (s Severity) String() string

type SeverityFilter

type SeverityFilter Severity

SeverityFilter implements the Filter interface which filters out any messages below the severity value.

func (SeverityFilter) ShowSeverity

func (f SeverityFilter) ShowSeverity(s Severity) bool

ShowSeverity returns true if the message of severity s should be shown.

type SeverityStacktracer

type SeverityStacktracer Severity

SeverityStacktracer implements the Stacktracer interface which adds a stacktrace to messages equal to or more than the severity level.

func (SeverityStacktracer) ShouldStacktrace

func (s SeverityStacktracer) ShouldStacktrace(m *Message) bool

ShouldStacktrace returns true if the message of severity s should include a stacktrace.

type SeverityStyle

type SeverityStyle int

SeverityStyle is an enumerator of ways that severities can be printed.

type SourceLocation

type SourceLocation struct {
	// The file name
	File string
	// 1-based line number
	Line int32
}

type Stacktracer

type Stacktracer interface {
	ShouldStacktrace(m *Message) bool
}

Stacktracer is the interface that controls when a stacktrace is taken.

func GetStacktracer

func GetStacktracer(ctx context.Context) Stacktracer

GetStacktracer returns the Stacktracer assigned to ctx.

type Style

type Style struct {
	Name      string        // Name of the style.
	Timestamp bool          // If true, the timestamp will be printed if part of the message.
	Tag       bool          // If true, the tag will be printed if part of the message.
	Trace     bool          // If true, the trace will be printed if part of the message.
	Process   bool          // If true, the process will be printed if part of the message.
	Severity  SeverityStyle // How the severity of the message will be printed.
	Values    ValueStyle    // How the values of the message will be printed.
}

Style provides customization for printing messages.

func (*Style) Choose

func (s *Style) Choose(v interface{})

Choose sets the style to the supplied choice.

func (*Style) Chooser

func (s *Style) Chooser() flags.Chooser

Chooser returns a chooser for the set of registered styles

func (Style) Handler

func (s Style) Handler(w Writer) Handler

Handler returns a new Handler configured to write to out and err with the given style.

func (Style) Print

func (s Style) Print(msg *Message) string

Print returns the message msg printed with the style s.

func (Style) String

func (s Style) String() string

type Trace

type Trace []string

func (Trace) String

func (t Trace) String() string

type V

type V map[string]interface{}

V is a map of key-value pairs. It can be associated with a context with Bind().

func (V) Bind

func (v V) Bind(ctx context.Context) context.Context

Bind returns a new context with V attached.

type Value

type Value struct {
	Name  string
	Value interface{}
}

type ValueStyle

type ValueStyle int

ValueStyle is an enumerator of ways that values can be printed.

type Values

type Values []*Value

func (Values) Len

func (v Values) Len() int

func (Values) Less

func (v Values) Less(i, j int) bool

func (Values) Swap

func (v Values) Swap(i, j int)

type Writer

type Writer func(text string, severity Severity)

Writer is a function that writes out a formatted log message.

func Buffer

func Buffer() (Writer, *bytes.Buffer)

Buffer returns a Writer that writes to the returned buffer.

func Std

func Std() Writer

Std returns a Writer that writes to stdout if the message severity is less than an error, otherwise it writes to stderr.

func Stdout

func Stdout() Writer

Stdout returns a Writer that writes to stdout for all severities.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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