package slog
import "github.com/sagikazarmark/slog-shim"
Index ¶
- Constants
- func Debug(msg string, args ...any)
- func DebugContext(ctx context.Context, msg string, args ...any)
- func Error(msg string, args ...any)
- func ErrorContext(ctx context.Context, msg string, args ...any)
- func Info(msg string, args ...any)
- func InfoContext(ctx context.Context, msg string, args ...any)
- func Log(ctx context.Context, level Level, msg string, args ...any)
- func LogAttrs(ctx context.Context, level Level, msg string, attrs ...Attr)
- func NewLogLogger(h Handler, level Level) *log.Logger
- func SetDefault(l *Logger)
- func Warn(msg string, args ...any)
- func WarnContext(ctx context.Context, msg string, args ...any)
- type Attr
- func Any(key string, value any) Attr
- func Bool(key string, v bool) Attr
- func Duration(key string, v time.Duration) Attr
- func Float64(key string, v float64) Attr
- func Group(key string, args ...any) Attr
- func Int(key string, value int) Attr
- func Int64(key string, value int64) Attr
- func String(key, value string) Attr
- func Time(key string, v time.Time) Attr
- func Uint64(key string, v uint64) Attr
- type Handler
- type HandlerOptions
- type JSONHandler
- type Kind
- type Level
- type LevelVar
- type Leveler
- type LogValuer
- type Logger
- type Record
- type Source
- type TextHandler
- type Value
- func AnyValue(v any) Value
- func BoolValue(v bool) Value
- func DurationValue(v time.Duration) Value
- func Float64Value(v float64) Value
- func GroupValue(as ...Attr) Value
- func Int64Value(v int64) Value
- func IntValue(v int) Value
- func StringValue(value string) Value
- func TimeValue(v time.Time) Value
- func Uint64Value(v uint64) Value
Constants ¶
const ( // TimeKey is the key used by the built-in handlers for the time // when the log method is called. The associated Value is a [time.Time]. TimeKey = slog.TimeKey // LevelKey is the key used by the built-in handlers for the level // of the log call. The associated value is a [Level]. LevelKey = slog.LevelKey // MessageKey is the key used by the built-in handlers for the // message of the log call. The associated value is a string. MessageKey = slog.MessageKey // SourceKey is the key used by the built-in handlers for the source file // and line of the log call. The associated value is a string. SourceKey = slog.SourceKey )
Keys for "built-in" attributes.
const ( KindAny = slog.KindAny KindBool = slog.KindBool KindDuration = slog.KindDuration KindFloat64 = slog.KindFloat64 KindInt64 = slog.KindInt64 KindString = slog.KindString KindTime = slog.KindTime KindUint64 = slog.KindUint64 KindGroup = slog.KindGroup KindLogValuer = slog.KindLogValuer )
The following list is sorted alphabetically, but it's also important that KindAny is 0 so that a zero Value represents nil.
Functions ¶
func Debug ¶
Debug calls Logger.Debug on the default logger.
func DebugContext ¶
DebugContext calls Logger.DebugContext on the default logger.
func Error ¶
Error calls Logger.Error on the default logger.
func ErrorContext ¶
ErrorContext calls Logger.ErrorContext on the default logger.
func Info ¶
Info calls Logger.Info on the default logger.
func InfoContext ¶
InfoContext calls Logger.InfoContext on the default logger.
func Log ¶
Log calls Logger.Log on the default logger.
func LogAttrs ¶
LogAttrs calls Logger.LogAttrs on the default logger.
func NewLogLogger ¶
NewLogLogger returns a new log.Logger such that each call to its Output method dispatches a Record to the specified handler. The logger acts as a bridge from the older log API to newer structured logging handlers.
func SetDefault ¶
func SetDefault(l *Logger)
SetDefault makes l the default Logger. After this call, output from the log package's default Logger (as with log.Print, etc.) will be logged at LevelInfo using l's Handler.
func Warn ¶
Warn calls Logger.Warn on the default logger.
func WarnContext ¶
WarnContext calls Logger.WarnContext on the default logger.
Types ¶
type Attr ¶
An Attr is a key-value pair.
func Any ¶
Any returns an Attr for the supplied value. See [Value.AnyValue] for how values are treated.
func Bool ¶
Bool returns an Attr for a bool.
func Duration ¶
Duration returns an Attr for a time.Duration.
func Float64 ¶
Float64 returns an Attr for a floating-point number.
func Group ¶
Group returns an Attr for a Group Value. The first argument is the key; the remaining arguments are converted to Attrs as in [Logger.Log].
Use Group to collect several key-value pairs under a single key on a log line, or as the result of LogValue in order to log a single value as multiple Attrs.
func Int ¶
Int converts an int to an int64 and returns an Attr with that value.
func Int64 ¶
Int64 returns an Attr for an int64.
func String ¶
String returns an Attr for a string value.
func Time ¶
Time returns an Attr for a time.Time. It discards the monotonic portion.
func Uint64 ¶
Uint64 returns an Attr for a uint64.
type Handler ¶
A Handler handles log records produced by a Logger..
A typical handler may print log records to standard error, or write them to a file or database, or perhaps augment them with additional attributes and pass them on to another handler.
Any of the Handler's methods may be called concurrently with itself or with other methods. It is the responsibility of the Handler to manage this concurrency.
Users of the slog package should not invoke Handler methods directly. They should use the methods of Logger instead.
type HandlerOptions ¶
type HandlerOptions = slog.HandlerOptions
HandlerOptions are options for a TextHandler or JSONHandler. A zero HandlerOptions consists entirely of default values.
type JSONHandler ¶
type JSONHandler = slog.JSONHandler
JSONHandler is a Handler that writes Records to an io.Writer as line-delimited JSON objects.
func NewJSONHandler ¶
func NewJSONHandler(w io.Writer, opts *HandlerOptions) *JSONHandler
NewJSONHandler creates a JSONHandler that writes to w, using the given options. If opts is nil, the default options are used.
type Kind ¶
Kind is the kind of a Value.
type Level ¶
A Level is the importance or severity of a log event. The higher the level, the more important or severe the event.
const ( LevelDebug Level = slog.LevelDebug LevelInfo Level = slog.LevelInfo LevelWarn Level = slog.LevelWarn LevelError Level = slog.LevelError )
Level numbers are inherently arbitrary, but we picked them to satisfy three constraints. Any system can map them to another numbering scheme if it wishes.
First, we wanted the default level to be Info, Since Levels are ints, Info is the default value for int, zero.
Second, we wanted to make it easy to use levels to specify logger verbosity. Since a larger level means a more severe event, a logger that accepts events with smaller (or more negative) level means a more verbose logger. Logger verbosity is thus the negation of event severity, and the default verbosity of 0 accepts all events at least as severe as INFO.
Third, we wanted some room between levels to accommodate schemes with named levels between ours. For example, Google Cloud Logging defines a Notice level between Info and Warn. Since there are only a few of these intermediate levels, the gap between the numbers need not be large. Our gap of 4 matches OpenTelemetry's mapping. Subtracting 9 from an OpenTelemetry level in the DEBUG, INFO, WARN and ERROR ranges converts it to the corresponding slog Level range. OpenTelemetry also has the names TRACE and FATAL, which slog does not. But those OpenTelemetry levels can still be represented as slog Levels by using the appropriate integers.
Names for common levels.
type LevelVar ¶
A LevelVar is a Level variable, to allow a Handler level to change dynamically. It implements Leveler as well as a Set method, and it is safe for use by multiple goroutines. The zero LevelVar corresponds to LevelInfo.
type Leveler ¶
A Leveler provides a Level value.
As Level itself implements Leveler, clients typically supply a Level value wherever a Leveler is needed, such as in HandlerOptions. Clients who need to vary the level dynamically can provide a more complex Leveler implementation such as *LevelVar.
type LogValuer ¶
A LogValuer is any Go value that can convert itself into a Value for logging.
This mechanism may be used to defer expensive operations until they are needed, or to expand a single value into a sequence of components.
type Logger ¶
A Logger records structured information about each call to its Log, Debug, Info, Warn, and Error methods. For each call, it creates a Record and passes it to a Handler.
To create a new Logger, call New or a Logger method that begins "With".
func Default ¶
func Default() *Logger
Default returns the default Logger.
func New ¶
New creates a new Logger with the given non-nil Handler.
func With ¶
With calls Logger.With on the default logger.
type Record ¶
A Record holds information about a log event. Copies of a Record share state. Do not modify a Record after handing out a copy to it. Call NewRecord to create a new Record. Use [Record.Clone] to create a copy with no shared state.
func NewRecord ¶
NewRecord creates a Record from the given arguments. Use [Record.AddAttrs] to add attributes to the Record.
NewRecord is intended for logging APIs that want to support a Handler as a backend.
type Source ¶
Source describes the location of a line of source code.
type TextHandler ¶
type TextHandler = slog.TextHandler
TextHandler is a Handler that writes Records to an io.Writer as a sequence of key=value pairs separated by spaces and followed by a newline.
func NewTextHandler ¶
func NewTextHandler(w io.Writer, opts *HandlerOptions) *TextHandler
NewTextHandler creates a TextHandler that writes to w, using the given options. If opts is nil, the default options are used.
type Value ¶
A Value can represent any Go value, but unlike type any, it can represent most small values without an allocation. The zero Value corresponds to nil.
func AnyValue ¶
AnyValue returns a Value for the supplied value.
If the supplied value is of type Value, it is returned unmodified.
Given a value of one of Go's predeclared string, bool, or (non-complex) numeric types, AnyValue returns a Value of kind String, Bool, Uint64, Int64, or Float64. The width of the original numeric type is not preserved.
Given a time.Time or time.Duration value, AnyValue returns a Value of kind KindTime or KindDuration. The monotonic time is not preserved.
For nil, or values of all other types, including named types whose underlying type is numeric, AnyValue returns a value of kind KindAny.
func BoolValue ¶
BoolValue returns a Value for a bool.
func DurationValue ¶
DurationValue returns a Value for a time.Duration.
func Float64Value ¶
Float64Value returns a Value for a floating-point number.
func GroupValue ¶
GroupValue returns a new Value for a list of Attrs. The caller must not subsequently mutate the argument slice.
func Int64Value ¶
Int64Value returns a Value for an int64.
func IntValue ¶
IntValue returns a Value for an int.
func StringValue ¶
StringValue returns a new Value for a string.
func TimeValue ¶
TimeValue returns a Value for a time.Time. It discards the monotonic portion.
func Uint64Value ¶
Uint64Value returns a Value for a uint64.
Source Files ¶
attr.go handler.go json_handler.go level.go logger.go record.go text_handler.go value.go
- Version
- v0.1.0 (latest)
- Published
- Sep 10, 2023
- Platform
- linux/amd64
- Imports
- 5 packages
- Last checked
- 9 hours ago –
Tools for package owners.