Documentation
¶
Overview ¶
Package slog implements minimal structured logging.
Example (Test) ¶
package main import ( "io" "testing" "golang.org/x/xerrors" "go.coder.com/slog" "go.coder.com/slog/sloggers/slogtest" ) func main() { // Nil here but would be provided by the testing framework. var t testing.TB slogtest.Info(t, "my message here", slog.F("field_name", "something or the other"), slog.F("some_map", slog.Map( slog.F("nested_fields", "wowow"), )), slog.Error( xerrors.Errorf("wrap1: %w", xerrors.Errorf("wrap2: %w", io.EOF), ), ), ) // t.go:55: 2019-09-13 23:19:03.468 [INFO] <examples_test.go:43> my message here {"field_name": "something or the other", "some_map": {"nested_fields": "wowow"}} ... // "error": wrap1: // go.coder.com/slog_test.TestExample // /Users/nhooyr/src/cdr/slog/examples_test.go:49 // - wrap2: // go.coder.com/slog_test.TestExample // /Users/nhooyr/src/cdr/slog/examples_test.go:50 // - EOF }
Output:
Index ¶
- func Context(ctx context.Context, fields ...Field) context.Context
- func Encode(v interface{}) slogval.Value
- func JSON(v interface{}) interface{}
- func Stdlib(ctx context.Context, l Logger) *log.Logger
- type Entry
- type Field
- type Level
- type Logger
- func (l Logger) Critical(ctx context.Context, msg string, fields ...Field)
- func (l Logger) Debug(ctx context.Context, msg string, fields ...Field)
- func (l Logger) Error(ctx context.Context, msg string, fields ...Field)
- func (l Logger) Fatal(ctx context.Context, msg string, fields ...Field)
- func (l Logger) Helper()
- func (l Logger) Info(ctx context.Context, msg string, fields ...Field)
- func (l Logger) Named(name string) Logger
- func (l Logger) SetLevel(level Level)
- func (l Logger) Sync()
- func (l Logger) Warn(ctx context.Context, msg string, fields ...Field)
- func (l Logger) With(fields ...Field) Logger
- type Sink
- type Value
- type ValueFunc
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Context ¶ added in v0.1.0
Context returns a context that contains the given fields. Any logs written with the provided context will have the given logs prepended. It will append to any fields already in ctx.
func Encode ¶ added in v0.2.0
Encode encodes the interface to a structured and easily introspectable slogval.Value.
func JSON ¶ added in v0.2.0
func JSON(v interface{}) interface{}
JSON tells the encoder that the struct in v should be encoded obeying JSON struct tags. Field names can be adjusted and omitempty is obeyed.
func Stdlib ¶
Stdlib creates a standard library logger from the given logger.
All logs will be logged at the Info level and the given ctx will be passed to the logger's Info method, thereby logging all fields and tracing info in the context.
You can redirect the stdlib default logger with log.SetOutput to the Writer on the logger returned by this function. See the example.
Types ¶
type Entry ¶ added in v0.1.0
type Entry struct { Time time.Time Level Level Message string LoggerName string Func string File string Line int SpanContext trace.SpanContext Fields []Field }
Entry represents the structure of a log entry. It is the argument to the sink when logging.
type Field ¶
Field represents a log field.
type Logger ¶
type Logger struct {
// contains filtered or unexported fields
}
Logger allows logging a ordered slice of fields to an underlying set of sinks.
func (Logger) Helper ¶ added in v0.2.0
func (l Logger) Helper()
Helper marks the calling function as a helper and skips it for source location information.
func (Logger) Named ¶ added in v0.2.0
Named names the logger. If there is already a name set, it will be joined by ".". E.g. if the name is currently "my_component" and then later the name "my_pkg" is set, then the final component will be "my_component.my_pkg".
func (Logger) Sync ¶ added in v0.2.0
func (l Logger) Sync()
Sync calls Sync on the sinks underlying the logger. Used it to ensure all logs are flushed during exit.
Directories
¶
Path | Synopsis |
---|---|
internal
|
|
assert
Package assert is a helper package for asserting equality and generating diffs in tests.
|
Package assert is a helper package for asserting equality and generating diffs in tests. |
humanfmt
Package humanfmt contains the code to format slog.Entry into a human readable format.
|
Package humanfmt contains the code to format slog.Entry into a human readable format. |
syncwriter
Package syncwriter implements a concurrency safe io.Writer wrapper.
|
Package syncwriter implements a concurrency safe io.Writer wrapper. |
sloggers
|
|
sloghuman
Package sloghuman contains the slogger that writes logs in a human readable format.
|
Package sloghuman contains the slogger that writes logs in a human readable format. |
slogjson
Package slogjson contains the slogger that writes logs in a JSON format.
|
Package slogjson contains the slogger that writes logs in a JSON format. |
slogstackdriver
Package slogstackdriver contains the slogger for GCP.
|
Package slogstackdriver contains the slogger for GCP. |
slogtest
Package slogtest contains the slogger for use with Go's testing package.
|
Package slogtest contains the slogger for use with Go's testing package. |
Package slogval is used by the default sloggers to take a []slog.Field and convert it into a easily marshable slogval.Value.
|
Package slogval is used by the default sloggers to take a []slog.Field and convert it into a easily marshable slogval.Value. |