Documentation
¶
Index ¶
- Constants
- Variables
- func Debug(ctx context.Context, args ...any)
- func DebugAttrs(ctx context.Context, message string, attrs ...slog.Attr)
- func Debugf(ctx context.Context, format string, args ...any)
- func Enabled(ctx context.Context, level slog.Level) bool
- func Error(ctx context.Context, args ...any)
- func ErrorAttrs(ctx context.Context, message string, attrs ...slog.Attr)
- func Errorf(ctx context.Context, format string, args ...any)
- func Info(ctx context.Context, args ...any)
- func InfoAttrs(ctx context.Context, message string, attrs ...slog.Attr)
- func Infof(ctx context.Context, format string, args ...any)
- func Log(ctx context.Context, level slog.Level, args ...any)
- func LogAttrs(ctx context.Context, level slog.Level, msg string, attrs ...slog.Attr)
- func Logf(ctx context.Context, level slog.Level, format string, args ...any)
- func Logger(ctx context.Context) *slog.Logger
- func MustParseLevel(s string) slog.Level
- func ParseLevel(s string) (slog.Level, error)
- func SetTreeLevel(ctx context.Context, level slog.Level) bool
- func StdLogger(ctx context.Context, level slog.Level) *stdLog.Logger
- func Trace(ctx context.Context, args ...any)
- func TraceAttrs(ctx context.Context, message string, attrs ...slog.Attr)
- func Tracef(ctx context.Context, format string, args ...any)
- func TreeEnabled(ctx context.Context, level slog.Level) bool
- func Warn(ctx context.Context, args ...any)
- func WarnAttrs(ctx context.Context, message string, attrs ...slog.Attr)
- func Warnf(ctx context.Context, format string, args ...any)
- func With(ctx context.Context, args ...any) context.Context
- func WithGroup(ctx context.Context, group string) context.Context
- func WithLogger(ctx context.Context, logger *slog.Logger) context.Context
- func WithTreeLevel(ctx context.Context, level slog.Level) context.Context
- type FormatHandler
- type LevelWithTrace
Examples ¶
Constants ¶
const LevelTrace = slog.LevelDebug - 4
LevelTrace is the most verbose log level. When used, the output will also contain file:line information.
Variables ¶
var LevelStrings = []string{
"ERROR", "WARN", "INFO", "DEBUG", "TRACE",
}
LevelStrings is the list of log level strings in order of increasing verbosity.
Functions ¶
func Debug ¶
Debug is similar to slog.Logger.DebugContext on the context logger. The first argument is converted using fmt.Sprint and used as the log message. Remaining args are handled according to slog.Logger.Log.
func DebugAttrs ¶
DebugAttrs is similar to slog.Logger.LogAttrs on the context logger, called with slog.LevelDebug.
func Debugf ¶
Debugf is similar to slog.Logger.DebugContext on the context logger, called with a message formatted with fmt.Sprintf.
func Error ¶
Error is similar to slog.Logger.ErrorContext on the context logger. The first argument is converted using fmt.Sprint and used as the log message. Remaining args are handled according to slog.Logger.Log.
func ErrorAttrs ¶
ErrorAttrs is similar to slog.Logger.LogAttrs on the context logger, called with slog.LevelError.
func Errorf ¶
Errorf is similar to slog.Logger.ErrorContext on the context logger, called with a message formatted with fmt.Sprintf.
func Info ¶
Info is similar to slog.Logger.InfoContext on the context logger. The first argument is converted using fmt.Sprint and used as the log message. Remaining args are handled according to slog.Logger.Log.
func InfoAttrs ¶
InfoAttrs is similar to slog.Logger.LogAttrs on the context logger, called with slog.LevelInfo.
func Infof ¶
Infof is similar to slog.Logger.InfoContext on the context logger, called with a message formatted with fmt.Sprintf.
Example ¶
package main
import (
"context"
"log/slog"
"os"
"time"
"github.com/telepresenceio/clog"
"github.com/telepresenceio/clog/internal"
)
func fakeTime() {
internal.TimeNow = func() time.Time { return time.Date(2026, 1, 2, 3, 4, 5, 678900000, time.UTC) }
}
func main() {
fakeTime()
lg := slog.New(slog.NewTextHandler(os.Stdout, nil))
ctx := clog.WithLogger(context.Background(), lg)
clog.Infof(ctx, "Hello, %s!", "world")
}
Output: time=2026-01-02T03:04:05.678Z level=INFO msg="Hello, world!"
Example (WithAttrsAndGroups) ¶
package main
import (
"context"
"log/slog"
"time"
"github.com/telepresenceio/clog"
"github.com/telepresenceio/clog/handler"
"github.com/telepresenceio/clog/internal"
)
func fakeTime() {
internal.TimeNow = func() time.Time { return time.Date(2026, 1, 2, 3, 4, 5, 678900000, time.UTC) }
}
func main() {
fakeTime()
lg := slog.New(handler.NewText(handler.TimeFormat("15:04:05.0000"), handler.EnabledLevel(slog.LevelInfo)))
topCtx := clog.WithLogger(context.Background(), lg)
clog.Debug(topCtx, "Hello, world!")
clog.Infof(topCtx, "Hello, %s!", "world")
ctx := clog.WithGroup(topCtx, "group")
clog.Infof(ctx, "Hello, %s!", "world")
clog.Info(topCtx, "Hello, world!", slog.Group("group", slog.Group("hello", "this", "value")))
clog.Info(topCtx, "Hello, world!", slog.GroupAttrs("group", slog.String("this", "value"), slog.Group("hello", "that", "thing", "is", `so "cool"`)))
clog.Info(topCtx, "Hello, world!", slog.GroupAttrs("group", slog.String("this", "value"), slog.Group("hello", slog.Group("that", "thing", "is cool"))))
clog.Info(clog.With(topCtx, "what", "world"), "Hello!")
clog.Info(topCtx, "Hello, world!", slog.String("this", "value"), slog.Group("hello", slog.Group("that", "thing", "is cool")))
clog.Infof(ctx, "Hello, world! %s", slog.GroupAttrs("group", slog.String("this", "value"), slog.Group("hello", slog.Group("that", "thing", `is "cool"`))))
clog.Infof(ctx, "Hello, world! %s", slog.Float64("value", 2.24))
clog.Infof(ctx, "Hello, world! value: %.3f", 2.24)
}
Output: 03:04:05.6789 INFO Hello, world! 03:04:05.6789 INFO group: Hello, world! 03:04:05.6789 INFO group/hello: Hello, world! : this=value 03:04:05.6789 INFO group: Hello, world! : this=value hello={that=thing is="so \"cool\""} 03:04:05.6789 INFO group: Hello, world! : this=value hello/that={thing="is cool"} 03:04:05.6789 INFO Hello! : what=world 03:04:05.6789 INFO Hello, world! : this=value hello/that={thing="is cool"} 03:04:05.6789 INFO group: Hello, world! group=[this=value hello=[that=[thing=is "cool"]]] 03:04:05.6789 INFO group: Hello, world! value=2.24 03:04:05.6789 INFO group: Hello, world! value: 2.240
func Log ¶
Log is similar to calling slog.Logger.Log on the context logger. The first argument is converted using fmt.Sprint and used as the log message. Remaining args are handled according to slog.Logger.Log.
func LogAttrs ¶
LogAttrs is similar to slog.Logger.LogAttrs on the context logger.
func Logf ¶
Logf is similar to slog.Logger.Log on the context logger, called with a message formatted with fmt.Sprintf.
func MustParseLevel ¶
MustParseLevel is like ParseLevel, but panics if the string is invalid.
func ParseLevel ¶
ParseLevel parses a log level string into a Level. It panics if the lowercased version of the string is not one of "trace", "debug", "info", "warn", "warning", or "error".
func SetTreeLevel ¶
SetTreeLevel sets the root slog.Level for the context where the WithTreeLevel was set, which might be the provided context itself or any parent of that context. Any children of context holding the root level is affected by this change.
func Trace ¶
Trace is similar to slog.Logger.Log on the context logger, called with slog.LevelTrace. The first argument is converted using fmt.Sprint and used as the log message. Remaining args are handled according to slog.Logger.Log.
func TraceAttrs ¶
TraceAttrs is similar to slog.Logger.LogAttrs on the context logger, called with slog.LevelTrace.
func Tracef ¶
Tracef is similar to slog.Logger.Log on the context logger, called with slog.LevelTrace and a message formatted with fmt.Sprintf.
func TreeEnabled ¶
TreeEnabled returns true if the root slog.Level is enabled for the given context. This function is suitable as an argument to a [handler.LevelEnabler] option.
func Warn ¶
Warn is similar to slog.Logger.WarnContext on the context logger. The first argument is converted using fmt.Sprint and used as the log message. Remaining args are handled according to slog.Logger.Log.
func WarnAttrs ¶
WarnAttrs is similar to slog.Logger.LogAttrs on the context logger, called with slog.LevelWarn.
func Warnf ¶
Warnf is similar to slog.Logger.WarnContext on the context logger, called with a message formatted with fmt.Sprintf.
func With ¶
With creates a clone of the context logger, adds the attributes, and assigns the clone to a child context which is returned.
func WithGroup ¶
WithGroup creates a clone of the context logger, adds the group, and assigns the clone to a child context which is returned.
func WithLogger ¶
WithLogger assigns the logger to a child context which is returned.
Types ¶
type FormatHandler ¶
type FormatHandler = internal.FormatHandler
type LevelWithTrace ¶
LevelWithTrace redefines slog.Level with an UnmarshalText to support the "trace" log level. It's suitable when used as a field that well be subjected to automatic parsing but shouldn't otherwise be used. Use `slog.Level` whenever possible.
func (LevelWithTrace) MarshalText ¶
func (l LevelWithTrace) MarshalText() ([]byte, error)
MarshalText implements encoding.TextMarshaler.
func (LevelWithTrace) String ¶
func (l LevelWithTrace) String() (s string)
func (*LevelWithTrace) UnmarshalText ¶
func (l *LevelWithTrace) UnmarshalText(value []byte) error
UnmarshalText will unmarshal a log level string into a Level. An error is returned if the string is not one of "TRACE", "DEBUG", "INFO", "WARN", or "ERROR" using case-insensitive comparison.