Documentation
¶
Overview ¶
Example (Errors) ¶
Errors are passed to WithError(), populating the "error" field.
package main import ( "errors" "github.com/cv/slog" ) func main() { l := slog.New() err := errors.New("boom") l.WithError(err).Error("upload failed") }
Example (MultipleFields) ¶
Multiple fields can be set, via chaining, or WithFields().
package main import ( "github.com/cv/slog" ) func main() { l := slog.New() l.WithFields(slog.Fields{ "user": "Tobi", "file": "sloth.png", "type": "image/png", }).Info("upload") }
Example (Structured) ¶
Structured logging is supported with fields.
package main import ( "github.com/cv/slog" ) func main() { l := slog.New() l.WithField("user", "Tobo").Info("logged in") }
Example (Trace) ¶
Trace can be used to simplify logging of start and completion events, for example an upload which may fail.
package main import ( "github.com/cv/slog" ) func main() { var err error l := slog.New() defer l.Trace(slog.InfoLevel, "upload").Stop(&err) }
Index ¶
- Variables
- type Entry
- func (e *Entry) Debug(msg string)
- func (e *Entry) Error(msg string)
- func (e *Entry) Fatal(msg string)
- func (e *Entry) IfError(err error) Interface
- func (e *Entry) Info(msg string)
- func (e *Entry) Panic(msg string)
- func (e *Entry) Stop(err *error)
- func (e *Entry) Trace(level Level, msg string) *Entry
- func (e *Entry) Warn(msg string)
- func (e *Entry) WithError(err error) *Entry
- func (e *Entry) WithField(key string, value interface{}) *Entry
- func (e *Entry) WithFields(fields Fielder) *Entry
- func (e *Entry) Writer(level Level) PrefixWriteCloser
- type Fielder
- type Fields
- type Handler
- type HandlerFunc
- type Interface
- type Level
- type Logger
- func (l *Logger) Debug(msg string)
- func (l *Logger) Error(msg string)
- func (l *Logger) Fatal(msg string)
- func (l *Logger) IfError(err error) Interface
- func (l *Logger) Info(msg string)
- func (l *Logger) Panic(msg string)
- func (l *Logger) RegisterHandler(maxLevel Level, handler Handler) *Logger
- func (l *Logger) Trace(level Level, msg string) *Entry
- func (l *Logger) Warn(msg string)
- func (l *Logger) WithError(err error) *Entry
- func (l *Logger) WithField(key string, value interface{}) *Entry
- func (l *Logger) WithFields(fields Fielder) *Entry
- func (l *Logger) Writer(level Level) PrefixWriteCloser
- type PrefixWriteCloser
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var Nil = &Logger{}
Nil logger that satisfies zlog.Interface but sends all messages to the bit bucket
Functions ¶
This section is empty.
Types ¶
type Entry ¶
type Entry struct { Logger *Logger `json:"-"` Fields Fields `json:"fields"` Level Level `json:"level"` Time time.Time `json:"time"` Message string `json:"msg"` // contains filtered or unexported fields }
Entry represents a single log entry.
func (*Entry) Stop ¶
Stop should be used with Trace, to fire off the completion message. When an `err` is passed the "error" field is set, and the log level is error.
func (*Entry) Trace ¶
Trace returns a new entry with a Stop method to fire off a corresponding completion log, useful with defer.
func (*Entry) WithFields ¶
WithFields returns a new entry with `fields` set.
func (*Entry) Writer ¶
func (e *Entry) Writer(level Level) PrefixWriteCloser
Writer returns an io.WriteCloser where each line written to that writer will be printed using the handlers for the given Level. It is the caller's responsibility to close it.
type Fielder ¶
type Fielder interface {
Fields() Fields
}
Fielder is an interface for providing fields to custom types.
type Fields ¶
type Fields map[string]interface{}
Fields represents a map of entry level data used for structured logging.
type Handler ¶
Handler is used to handle log events, outputting them to stdio or sending them to remote services. See the "handlers" directory for implementations.
It is left up to Handlers to implement thread-safety.
type HandlerFunc ¶
The HandlerFunc type is an adapter to allow the use of ordinary functions as log handlers. If f is a function with the appropriate signature, HandlerFunc(f) is a Handler object that calls f.
type Interface ¶
type Interface interface { WithFields(fields Fielder) *Entry WithField(key string, value interface{}) *Entry WithError(err error) *Entry Debug(msg string) Info(msg string) Warn(msg string) Error(msg string) Fatal(msg string) Panic(msg string) IfError(error) Interface Trace(level Level, msg string) *Entry Writer(level Level) PrefixWriteCloser }
Interface represents the API of both Logger and Entry.
type Level ¶
type Level int
Level of severity.
func ParseLevel ¶
ParseLevel parses level string.
func (Level) MarshalJSON ¶
MarshalJSON returns the level string.
func (Level) MarshalText ¶
MarshalText implements encoding.TextMarshaler
func (*Level) UnmarshalText ¶
UnmarshalText implements encoding.TextUnmarshaler
type Logger ¶
type Logger struct {
// contains filtered or unexported fields
}
Logger represents a logger. It will not do anything useful unless a handler has been registered with RegisterHandler.
func (*Logger) RegisterHandler ¶
RegisterHandler adds a new Handler and specifies the maximum Level that the handler will be passed log entries for
func (*Logger) Trace ¶
Trace returns a new entry with a Stop method to fire off a corresponding completion log, useful with defer.
func (*Logger) WithFields ¶
WithFields returns a new entry with `fields` set.
func (*Logger) Writer ¶
func (l *Logger) Writer(level Level) PrefixWriteCloser
Writer returns an io.WriteCloser where each line written to that writer will be printed using the handlers for the given Level. It is the caller's responsibility to close it.
type PrefixWriteCloser ¶
type PrefixWriteCloser interface { io.WriteCloser Prefix(prefix string) PrefixWriteCloser }
PrefixWriteCloser is an io.WriteCloser that can be prefixed for every line it writes
Directories
¶
Path | Synopsis |
---|---|
_examples
|
|
json
command
|
|
logfmt
command
|
|
text
command
|
|
trace
command
|
|
handlers
|
|
discard
Package discard implements a no-op handler useful for benchmarks and tests.
|
Package discard implements a no-op handler useful for benchmarks and tests. |
json
Package json implements a JSON handler.
|
Package json implements a JSON handler. |
logfmt
Package logfmt implements a "logfmt" format handler.
|
Package logfmt implements a "logfmt" format handler. |
memory
Package memory implements an in-memory handler useful for testing, as the entries can be accessed after writes.
|
Package memory implements an in-memory handler useful for testing, as the entries can be accessed after writes. |
text
Package text implements textual handler suitable for development and production output.
|
Package text implements textual handler suitable for development and production output. |