func Helper()
Helper marks the calling function as a helper and skips it for source location information. It's the slog equivalent of testing.TB.Helper().
▹ Example
func Stdlib(ctx context.Context, l Logger, level Level) *log.Logger
Stdlib creates a standard library logger from the given logger.
All logs will be logged at the level set by the logger and the given ctx will be passed to the logger's Log 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.
▹ Example
func With(ctx context.Context, fields ...Field) context.Context
With 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.
▹ Example
Field represents a log field.
type Field struct { Name string Value interface{} }
func Error(err error) Field
Error is the standard key used for logging a Go error value.
func F(name string, value interface{}) Field
F is a convenience constructor for Field.
Level represents a log level.
type Level int
The supported log levels.
The default level is Info.
const ( // LevelDebug is used for development and debugging messages. LevelDebug Level = iota // LevelInfo is used for normal informational messages. LevelInfo // LevelWarn is used when something has possibly gone wrong. LevelWarn // LevelError is used when something has certainly gone wrong. LevelError // LevelCritical is used when when something has gone wrong and should // be immediately investigated. LevelCritical // LevelFatal is used when the process is about to exit due to an error. LevelFatal )
func (l Level) String() string
String implements fmt.Stringer.
Logger wraps Sink with a nice API to log entries.
Logger is safe for concurrent use.
type Logger struct {
// contains filtered or unexported fields
}
func Make(sinks ...Sink) Logger
Make creates a logger that writes logs to the passed sinks at LevelInfo.
func (l Logger) AppendSinks(s ...Sink) Logger
AppendSinks appends the sinks to the set sink targets on the logger.
func (l Logger) Critical(ctx context.Context, msg string, fields ...Field)
Critical logs the msg and fields at LevelCritical.
It will then Sync().
func (l Logger) Debug(ctx context.Context, msg string, fields ...Field)
Debug logs the msg and fields at LevelDebug.
func (l Logger) Error(ctx context.Context, msg string, fields ...Field)
Error logs the msg and fields at LevelError.
It will then Sync().
func (l Logger) Fatal(ctx context.Context, msg string, fields ...Field)
Fatal logs the msg and fields at LevelFatal.
It will then Sync() and os.Exit(1).
func (l Logger) Info(ctx context.Context, msg string, fields ...Field)
Info logs the msg and fields at LevelInfo.
func (l Logger) Leveled(level Level) Logger
Leveled returns a Logger that only logs entries equal to or above the given level.
▹ Example
func (l Logger) Log(ctx context.Context, e SinkEntry)
Log logs the given entry with the context to the underlying sinks.
It extends the entry with the set fields and names.
func (l Logger) Named(name string) Logger
Named appends the name to the set names on the logger.
▹ Example
func (l Logger) Sync()
Sync calls Sync on all the underlying sinks.
func (l Logger) Warn(ctx context.Context, msg string, fields ...Field)
Warn logs the msg and fields at LevelWarn.
func (l Logger) With(fields ...Field) Logger
With returns a Logger that prepends the given fields on every logged entry.
It will append to any fields already in the Logger.
Map represents an ordered map of fields.
type Map []Field
func M(fs ...Field) Map
M is a convenience constructor for Map
func (m Map) MarshalJSON() ([]byte, error)
MarshalJSON implements json.Marshaler.
It is guaranteed to return a nil error. Any error marshalling a field will become the field's value.
Every field value is encoded with the following process:
1. json.Marshaller is handled.
2. xerrors.Formatter is handled.
3. structs that have a field with a json tag are encoded with json.Marshal.
4. error and fmt.Stringer is handled.
5. slices and arrays go through the encode function for every element.
6. For values that cannot be encoded with json.Marshal, fmt.Sprintf("%+v") is used.
7. json.Marshal(v) is used for all other values.
Sink is the destination of a Logger.
All sinks must be safe for concurrent use.
type Sink interface { LogEntry(ctx context.Context, e SinkEntry) Sync() }
SinkEntry represents the structure of a log entry. It is the argument to the sink when logging.
type SinkEntry struct { Time time.Time Level Level Message string LoggerNames []string Func string File string Line int SpanContext trace.SpanContext Fields Map }
Name | Synopsis |
---|---|
.. | |
sloggers | |
sloghuman | Package sloghuman contains the slogger that writes logs in a human readable format. |
slogjson | Package slogjson contains the slogger that writes logs in JSON. |
slogstackdriver | Package slogstackdriver contains the slogger for google cloud's stackdriver. |
slogtest | Package slogtest contains the slogger for use with Go's testing package. |
assert | Package assert is a helper package for test assertions. |